# Writing a C program for class....

• 11-12-2007
Bizmark
Writing a C program for class....
Hi, I'm a computer science student in college right now and I'm having trouble with a program I'm writing. The goal of the program is to:

"Write a menu-driven program that allows the user to fill an array of 50 integers with random numbers in the range 1...999, sort it, and then search it to determine if a given random number was generated. The menu is shown below
Select one of the following options:
F. Fill array with a random number series
P. Print the array
S. Sort the array
Q. Query the array
Z. Terminate the program

-Each time the fill option is executed, it will fill the array with a new random number series.
-You may use any of the sorts discussed int he text.
-If the query locates the number, it will print a message that the number was located and the index location where it was found.
-If the query does not locate the number, it will print a message that the number was not in the list and then print the value and index location of the largest number less than the target and the smallest value greater than the requested number. Note that the first and last elements will not have both a smaller and larger value.
-If the array has been sorted, the query will use a binary search. If it is not sorted, it will use the sequential search.

To test the program:
a. Fill (F) the array.
b. Print (P) the unsorted array.
c. Search (Q) the first, last, and a middle element.
d. Refill (F) the array.
e. Sort (S) the array.
f. Print (P) the sorted array.
g. Search (Q) the first, last, and a middle element.
h. Search (Q) for a number less than the first, greater than the last, and not found in the middle of the list."

So yeah, it took forever to type that, anyway heres what I've done so far.

Code:

```//Exercise 56  p. 555 #include <stdio.h> #include <time.h> #include <stdlib.h> void fillArray(int i, double *x[]); void printArray(int i, double x[]); main() {       char choice[1];       double myArray[50];       int exit = 0;       myArray[1] = 9999999;                   while(exit == 0)       {                 system("cls");                 printf("Here are your options: \n");                 printf("F. Fill an array with a random number series\n");                 printf("P. Print the array\n");                 printf("S. Sort the array\n");                 printf("Q. Query the array\n");                 printf("Z. Terminate the program\n");                 printf("Selection: ");                 scanf("%c", &choice[0]);                       switch(choice[0])                 {                     case 'f': case 'F': fillArray(50, &myArray);                                         printf("\n");                                         system("pause");                                         break;                     case 'p': case 'P': if(myArray[1] != 9999999)                                         {                                                       printArray(50, myArray);                                                       printf("\n");                                                       system("pause");                                                       break;                                         }                                         else                                         {                                                       printf("You must first fill the array!\n");                                                       system("pause");                                                       break;                                         }                                                                           case 'z': case 'Z': exit = 1;                 }       }       system("pause");             } void fillArray(int i, double *x[]) {     int b = 0;     srand(time(NULL));         while(b < i)     {             *x[b] = rand()%999+1;             b++;     } } void printArray(int i, double x[]) {     int b = 0;         while(b<i);     {                 printf("Array %d: %.0lf\n", b+1, x[]);                 b++;     } }```
The error I'm getting is coming up here:
Code:

`case 'f': case 'F': fillArray(50, &myArray);`
This is the first case in my switch. And heres the error that I'm getting:
"[Warning] passing arg 2 of `fillArray' from incompatible pointer type"

I've checked the code and have no idea why it's saying that. Anyone have an idea?
• 11-12-2007
Bizmark
Ok so I figured out the problem. Turned out I had to put a "b" right here:
Code:

`printf("Array &#37;d: %.0lf\n", b+1, x[]); // a "b" needed to be placed in "x[]", so its "x[b]"`
Now it works, but when I choose the "f" option in my switch the debugger pops up with an error. So there's something wrong with my fillArray(). Anyone see anything wrong with it?
• 11-12-2007
matsp
Code:

```                    case 'p': case 'P': if(myArray[1] != 9999999)                                         {                                                       printArray(50, myArray);                                                       printf("\n");                                                       system("pause");                                                       break;                                         }                                         else                                         {                                                       printf("You must first fill the array!\n");                                                       system("pause");                                                       break;                                         }                                         break;```
I prefer to put break OUTSIDE if-else - so remove the red and use the blue version. That way it's much "safer" that you don't miss out a break for some reason.

You don't use warnings with your compiler, do you?

Code:

```void fillArray(int i, double *x[]) ...             *x[b] = rand()%999+1; ...```
That expects an array of pointers to double. You are passing an array of double's. You then proceed to take the address in the array [whcih by the way is undefinded] and put a random number there - so you are filling some "random" memory location with your random number. Instant segmentation fault

Also, your random numbers are integers. Why are you using "double"?

And if you call fill twice in a row in short succesion (the same minute or so) you will probably get the same random numbers - at least close. Call srand() once in the beginning of main instead of in fill.

--
Mats
• 11-12-2007
Bizmark
Thank you for the reply Mats. I'm not really sure what is wrong with my fillArray(). Could you elaborate a little more? Also, the error I got before all of a sudden started again, and I didn't change anything it says "[Warning] passing arg 2 of `fillArray' from incompatible pointer type" for this code:
Code:

` case 'f': case 'F': fillArray(50, &myArray);`
And I have no idea why.
• 11-12-2007
matsp
That is because you are passing a pointer to the array - but the function expects an array of pointers. There is a subtle difference.

--
Mats
• 11-12-2007
Bizmark
Could you give me an example of some code that would work? My teacher has code setup exactly as I do that works fine. But for some reason I must be overlooking a difference between his and my code.

**Edit: Ok it just clicked. I need to setup the while loop outside of fillArray(). and then only have fillArray(int *x[]). and have no while loop inside, that should do it.

***Edit: Ok the above edit didn't work. And now I'm starting to get really discouraged a ........ed off. I have no idea what the problem is.
• 11-12-2007
matsp
Just remove the * in front of x in the fillArray function, and the & in your call to fillArray.

--
Mats
• 11-12-2007
Bizmark
Ok I did the above. Only problem is now printArray() will not print anything to the screen. I created fillArray() so that it would return random numbers to myArray, but without pointers that won't work will it?
• 11-12-2007
Aia
Quote:

Originally Posted by Bizmark
Ok I did the above. Only problem is now printArray() will not print anything to the screen.

printfArray() is not printing anything because you have an empty subscript as the second argument to printf.

Code:

```    while(b<i);     {                 printf("Array %d: %.0lf\n", b+1, x[b]); /* missing the reference in the subscript */                 b++;     }```
Quote:

Originally Posted by Bizmark
I created fillArray() so that it would return random numbers to myArray, but without pointers that won't work will it?

fillArray() doesn't return a random number. It's void. However the values in the passed array will change since it will happen by reference.
Also, when you pass an array in a function, a pointer to the first element of the array is what's passed.
• 11-13-2007
Salem
The ; at the end of the while can't be helping either.
Why are you using a while loop to simulate a for loop?
• 11-13-2007
Bizmark
Quote:

Originally Posted by Salem
The ; at the end of the while can't be helping either.
Why are you using a while loop to simulate a for loop?

The reason that I prefer using while loops over for loops is that if necessary I can control where the increment for the condition being tested is located. For instance say I did this:

Code:

```main() {         int x = 0, t = 5;         while(x <= 10)         {                   if(t == 5)                   {                         x++;                   }                   else                   {                         printf("I now have other factors like if statments that can control the increment!  Rather than in a for loop where no matter what the increment happens on every run through!");                   }           } system("pause"); }```
But anyway, I figured out everything that was wrong, took my test that we had today and aced it. Thanks to everyone who helped me out.