MENU

hi guys can you help me to my project i got lot to do so i dont know wer to start..one of my project is to write a menu driven program that allows the user to fill anarray of 50 integers with random numbers in the range of 1-999,sort it,and then search it to determine if a given random number was generated.the menu is shown below:

MENU
[F] Fill array with random numbers
[P] Print the array
[S] sort the array (selection Sort)
[Q] Quuery the array (Binary Search)
[T] terminate the program

@ each time the fill option is executed, it will fill the array wth a new number series.
@use any of the sort algorithms
@if the query locates the number ,it will print the messege that the num was locte and the ndx loction wer it was fund
@if d query does not locte d num it will prnt d numb was not on the list and then print d value and ndx location of d largest num less than d target and d smallest value graeter than d requestet number.
Hi there,

This has a wiff of "homework" to me.
I don't mind helping you to do this, but you're going to be the one doing the work.

Best thing to do is take all the functionalities of the program apart and implement each of them one by one.

Showing the menu seems like a good start.
For this you will need to print text to the screen (using std::cout) and accept input from the keyboard (using std::cin).

You can find all the necessary info on how to accomplish this simple task here:
http://cplusplus.com/doc/tutorial/basic_io/

Get back to us with your code and we'll help you with the next step.

All the best,
NwN

# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define MAX 50
# define TRUE 0
# define FALSE 1

int main(void)
{
int a[50]={MAX};
int array [50]={MAX};
int x;
int Print;
int Sort;
int Query;
int Terminate;
int left = 0;
int right = 50;
int middle = 0;
int number = 0;
int bsearch = FALSE;
int i = 0;


for(x=0; x<50; x++)

if (array[5] == Print)
printf("P\n");
else
if (array[50] == Sort)
printf("S\n");

srand(time(NULL));
for (i=1;i<=MAX;i++)
printf("%d\t",1+(rand()%999));

printf("ARRAY: \n");
for(i = 1; i <=50; i++)
printf("%d\t ", i);

printf("\nSearch for Number: ");
scanf("%d", &number);

while(bsearch == FALSE && left <= right) {
middle = (left + right) / 2;

if(number == array[middle]) {
bsearch = TRUE;
printf("** Number Found **\n");
} else {
if(number < array[middle]) right = middle - 1;
if(number > array[middle]) left = middle + 1;
}
}

if(bsearch == FALSE)
printf("-- Number was not in the list --\n");
printf("MENU\n\n");
printf("F.Fill\nP.Print\nS.Sort\nQ.Query\nZ.Terminate\n");
printf("Please enter the option you selected: ");
scanf("%d\n", &Print,&Sort,&Query,&Terminate);
printf("The option you selected is %d\n ");

printf("The random number is %d\n",x);
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
# define MAX 50
# define TRUE 0
# define FALSE 1

int main(void)
{
    int a[50]={MAX};
    int array [50]={MAX};
    int x;
    int Print;
    int Sort;
    int Query;
    int Terminate;
    int left = 0;
    int right = 50;
    int middle = 0;
    int number = 0;
    int bsearch = FALSE;
    int i = 0;


    for(x=0; x<50; x++)

    if (array[5] == Print)
        printf("P\n");
    else
    if (array[50] == Sort)
        printf("S\n");

    srand(time(NULL));
    for (i=1;i<=MAX;i++)
        printf("%d\t",1+(rand()%999));

    printf("ARRAY: \n");
    for(i = 1; i <=50; i++)
        printf("%d\t ", i);

    printf("\nSearch for Number: ");
    scanf("%d", &number);

    while(bsearch == FALSE && left <= right) {
        middle = (left + right) / 2;

    if(number == array[middle]) {
        bsearch = TRUE;
        printf("** Number Found **\n");
    } else {
    if(number < array[middle]) right = middle - 1;
    if(number > array[middle]) left = middle + 1;
    }
    }

    if(bsearch == FALSE)
    printf("-- Number was not in the list --\n");
    printf("MENU\n\n");
    printf("F.Fill\nP.Print\nS.Sort\nQ.Query\nZ.Terminate\n");
    printf("Please enter the option you selected: ");
    scanf("%d\n", &Print,&Sort,&Query,&Terminate);
    printf("The option you selected is %d\n ");

    printf("The random number is %d\n",x);
    return 0;
} 



What is the function of this block at the beginning?
1
2
3
4
5
6
7
for(x=0; x<50; x++)

    if (array[5] == Print)
        printf("P\n");
    else
    if (array[50] == Sort)
        printf("S\n");


You know that Print and Sort haven't been declared yet, right? And they're most probably gonna be big, ugly numbers.


Okay, you're using the random number generator correctly but you're not really stroing anythign in the array, you're just printing numbers to the screen
1
2
3
srand(time(NULL));
    for (i=1;i<=MAX;i++)
        printf("%d\t",1+(rand()%999));



Same with this
1
2
3
printf("ARRAY: \n");
    for(i = 1; i <=50; i++)
        printf("%d\t ", i);



- You didn't sort the array.

One note, when using an if with its else inside a loop, make sure you wrap it with braces.


EDIT:
1
2
# define TRUE 0
# define FALSE 1 

Are you sure you don't want TRUE to be 1 and FALSE to be 0?
Last edited on
yeah i know that,, that's y i need help about it...i don't know how to fix it...like what i said i got lot of homework to do..
Hi there,

Please remain polite, Mr. ToniAz was only trying to help and he made some good points.

First off, I would replace printf() with std::cout << and scanf() with std::cin >> the former are C functions, the latter are C++ objects and are thus preferred.

So, you have displayed your menu, that's a start.
I'm just going to focus on that for now, we'll do the rest from the ground up so you understand what we're doing.

So, after some modification,this is what we have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
# define MAX 50  //max array size

int main()
{
    int arr[MAX];
    char choice;
    std::cout << "MENU\n\n";
    std::cout <<"F.Fill\nP. Print\nS. Sort\nQ. Query\nZ. Terminate\n";
    std::cout <<"Please enter the option you selected: ";
    std::cin >>char;

    return 0;
}


Now we have to evaulate the user's choice, which is now stored in char.
This is best done using a switch-statement ( http://cplusplus.com/doc/tutorial/control/#switch ) like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
switch (choice)
{
    case 'F':
        //do Filling here
        break;

    case 'P':
        //do printing here
        break;

    case 'Q':
        //do querying here
        break;

    case 'Z':
        //exit here
        break;

    default: //if choice was none of the above
        std::cout << "\nInvalid input - please try again.";
        //show the menu again
}


The best way to continue from that is to start with making a separate function that will fill an array, which is then called like so in the switch statement:

1
2
3
case 'F':
        fill_array(int& array[]);
        break;


So, try and put that switch statement in the main function above and writing that fill function.
Once it is filled we can print and query it, so that's half the work done.

On a sidenote, defining TRUE and FALSE is really unnecessary, you can just use true and false in your code.

All the best,
NwN
tnx NwN?..
Hi there,

How are you getting on? :)

All the best,
NwN
are u sure that we need to replace the printf and scanf?
You don't have to. printf and scanf are difficult to use, prone to error, unsafe and harder to read. cout and cin are not. The choice is yours.
for me now it is easy to use printf but to print the menu with dr function is difficult 2me
How are printf and scanf easier to use ? I'd recommend using cout and cin over them, but it's your choice anyway.
thinks for the advice..you know it is easier to me couz printf and scanf is usually use in our discussion so that's why it is easier for me
Topic archived. No new replies allowed.