Thread: Writing a C program for class....

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    26

    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
    MENU
    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?

  2. #2
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    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?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    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.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    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.
    Last edited by Bizmark; 11-12-2007 at 04:25 PM.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just remove the * in front of x in the fillArray function, and the & in your call to fillArray.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    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?

  9. #9
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    Quote Originally Posted by Bizmark View Post
    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 View Post
    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.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The ; at the end of the while can't be helping either.
    Why are you using a while loop to simulate a for loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    26
    Quote Originally Posted by Salem View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 11:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 07:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 09:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 01:56 AM