Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help with menu driven search and sort program

  1. Jun 15, 2012 #1
    Hello, this is sort of a big program and I wrote out the program, but I'm unsure about a lot of things.

    The program is supposed to give the user a menu of choices:
    1) Fill the array with random numbers.
    2) Print the array.
    3) Search the array.
    4) Sort the array.
    5) Quit.

    Option one is supposed to fill an array with 50 random numbers. Option 2 prints the array. Option 3 searches the array. But it needs to use two different search methods depending on if the array is sorted or not. If it's not sorted, I'm supposed to use sequential searching, if it's sorted, then I'm supposed to use binary searching. Then 4 sorts the array. I chose bubble sorting arbitrarily.

    I'm not sure how to make the program do one search method over another, so I just made two different functions for the two different search methods. How do I make it do the sequential search if it's unsorted and the binary search if it's sorted?


    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int getOption (void)
    {
        int option;

        printf("Fill Array With Random Numbers\n1- Print the Array\n2- Search the Array\n3- Sort the Array\n4- Quit\n\n");
        scanf("%d", &option);

            if (option<=0 || option>4)
        printf("\nError: That number is not an option.\n");

            if (option=4)
        printf("Bye");

      return option;
    }
    int fillArray (int option)
    {
        int range;
        int getOption (void);
        srand(time(NULL));
        range = (999 - 0) + 1;
    }
    int printArray (int range)
    {

        printf("%d", rand() % range);

    }
    bool seqSearch (int list[], int last, int target, int* location)
    {
        int looker;
        bool found;

        looker = 0;
        while (looker < last && target != list[looker])
            looker++;

        *location = looker;
        found = (target == list[looker]);
        return found;

    }
    bool binarySearch (int list[], int end, int target, int* location)
    {
        int first;
        int mid;
        int last;
       
        first = 0;
        last = end;
        while (first <= last)
            {
                mid = (first + last) / 2;
                if (target > list[mid])
                    first = mid + 1;
                else if (target < list[mid])
                    last = mid - 1;
                else
                    first = last + 1;
            }
        *location = mid;
        return target == list[mid];
    }
    void sortArray (int list[], int last)
    {  
        int temp;
       
        for(int current = 0; current < last; current++)
            {
                for (int walker = last; walker > current; walker--)
                    if (list[walker] < list[walker - 1])
                        temp = list[walker];
                        list[walker] = list[walker - 1];
                        list[walker - 1] = temp;
            }
    }
     

    Thanks.
     
  2. jcsd
  3. Jun 15, 2012 #2

    DavidSnider

    User Avatar
    Gold Member

    You need a variable to keep track of whether or not the list has been sorted or not. When you choose 'fill the array with random numbers' the flag will be false and when you choose 'sort the array' it will be true.
     
  4. Jun 15, 2012 #3
    Thanks for the response. How would I do that? Would I use an "if" statement? And which function would I put it in?
     
  5. Jun 15, 2012 #4
    Do you have a main() function? This is where you will define the flow of your program, but I don't see it.
     
  6. Jun 15, 2012 #5
    I didn't make it yet because I wasn't sure what to put in it. Is that where I put all the function names that I'm going to use throughout the program? I'm not really clear on what main is for. I used to just make a main function and put everything in it. Now that I'm required to make more than one function, I'm not really sure what main is for.

    Thanks for the response.
     
  7. Jun 15, 2012 #6

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    "main" is where you program starts execiting. Different programming languages and operating systems have different conventions to say where your program starts, but in C (at least for the sort of programs you are currently writing) you must always write a function called "main". Often the "main" function doesn't do much itself, except declare any variables that will be used "everywhere" in the program, and call other functions to do the "real work".

    To start this off the same way as my other answer:
    Code (Text):

    int main()
    {
        int sorted;
        int option;

        loop for ever
        {
            prompt_for_a_menu_item();
            read_a_menu_item();
            if (option == 1)
                Fill_the_array_with_random_numbers();
                sorted = false;
            if (option == 2}
                Print_the_array();
            if (option == 3)
                if (sorted = true) Search_the_sorted_array();
                else Search_the_unsorted_array();
            if (option == 4)
                Sort_the_array();
                sorted = true;
            if (option == 5)
                Quit();
        }
    }
     
    Writing programs is easy, if the programming language has all the "magic commands" you need to do what your program needs to do (like the above example).

    But you can make up your own "magic commands" - they are called "functions"....

    Actually, there's something missing from my example. The user can (presumably) pick optiosn from the menu in any order, but it doesn't make any sense do options 2 3 or 4 before doing option 1. So you really need another "true or false" variable to say if the data has been read in, and test it before you try to do the other operations.
     
    Last edited: Jun 15, 2012
  8. Jun 15, 2012 #7

    jtbell

    User Avatar

    Staff: Mentor

    When I write a program like this, I always start with main(), similar to what AlephZero wrote. I write "stubs" (short versions) for all the functions that don't do anything except display a message like "Sorry, I don't know how to sort yet." At this point I have something that should compile and run. I can exercise the menu, even though it doesn't really do anything.

    Then I would write the function that fills the array with random numbers, and the function that prints (displays) the array. I can test them simply by choosing the appropriate menu items.

    Then I would write a sort function and use the already-working parts of the program to test it. Then include the logic to sort only if the array isn't sorted already, etc.
     
  9. Jun 16, 2012 #8
    Ok I did the first two options, but I can't get the search option to work. I tried implementing it into the program, but it doesn't work. Is there a simpler way to search than what I wrote in the code in my first post? Because that's a lot of text and it just ends up giving me lots of errors.

    Here's what I have so far:

    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main()
    {
        int sorted;
        int option;
        int range;
        int getOption (void);

        do
        {printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
        scanf("%d", &option);

        if (option<=0 || option>5)
            printf("\nError: That number is not an option.\n");

        if (option==1)
            {
            srand(time(NULL));
            range = (999 - 0) + 1;
            }
        if (option==2)
            printf("%d\n", rand() % range);
        }
        while(option!=5);
    }
    Thanks for the replies.
     
  10. Jun 16, 2012 #9

    Mark44

    Staff: Mentor

    I assume you're talking about doing a sequential search. We can see the code for your search function, but you don't show the code in main where you are calling this function. Your seqSearch function returns a Boolean value that indicates whether it found the requested item in the array. If the item was found, when seqSearch returns, the index of the item's position in the array is also made available, so your main function needs to have logic to use this information. Show us that code and the list of errors you're getting, and I'm sure we can figure it out.

    jtbell makes some good points in post #7. Based on what I see here and what I saw in your thread on the calendar program, you are trying to write the program from the bottom up, rather than from the top down. By not writing the main function first you are creating the building blocks of your programs without having a clear idea of how the blocks need to go together.
     
  11. Jun 16, 2012 #10
    Yes, I'm trying to do a seqSearch first, since the array will be unsorted at first.

    Here's the error I get.
    error: expected '=', ',', ';', 'asm' or '__attribute__' before 'seqSearch'

    And here's the code. I took the sequential search code from the book.
    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    bool seqSearch (int list[], int last, int target, int* location);
    int main()
    {
        int sorted;
        int option;
        int range;
        int getOption (void);

        do
        {printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
        scanf("%d", &option);

        if (option<=0 || option>5)
            printf("\nError: That number is not an option.\n");

        if (option==1)
            {
            srand(time(NULL));
            range = (999 - 0) + 1;
            }

        if (option==2)
            printf("%d\n", rand() % range);

        if (option==3)
            {
                int looker;
                bool found;

                looker = 0;
                while (looker < last && target != list[looker])
                    looker++;

                *location = looker;
                found = (target == list[looker]);
                return found;

            }

        }
        while(option!=5);
    }
     
    I just figured I'd put everything in one function right now and then separate it into separate functions later. I don't really need functions for it to work, right? The assignment demands that I do use separate functions, so I'll have to separate them after I find out if it works.
     
  12. Jun 16, 2012 #11

    Mark44

    Staff: Mentor

    You have changed the structure of your program, so I believe the compiler or linker is now confused.

    In post #1 you had a bunch of function definitions, but no main. In the latest iteration, you have main but no function definitions. (There is the prototype for seqSearch, but no definition for it.) You have taken the code out of seqSearch and stuck it in main.

    You have also gotten rid of your other functions, and seem to be writing the code inline in main.

    main should be short and sweet. Aside from the declarations of things it needs to pass to the function, the only logic present in main should be a loop. In the loop you call getOption and then decide from the value returned by getOption which function to call (fillArray, printArray, seqSearch, binSearch).

    As already mentioned, you'll need a variable in main to keep track of whether the array is sorted. You'll also need a variable to keep track of whether the array has been created. You don't want to print or sort the array if it hasn't been filled yet.

    Start thinking about main as being a manager who delegates tasks to the people would report to him or her. The manager (main) has to keep track of the overall project, but leaves the details of the tasks that make up the project to the workers (functions).

    The manager usually has to provide some information to the workers so that they can complete their assigned task, and when they are finished, the report some information back to the manager. In your program, when main calls a function, it passes some information in the argument list of the function, and uses the value returned by the function.

    Code (Text):

    int main(int argc, char** argv)
    {
      // pseudocode
      while input choice != 5
      {
          getOption


    }
     
     
  13. Jun 16, 2012 #12
    So should I get rid of the "do while" loop and separate each option into separate functions? And then put getOption in a loop with the printf that prints the menu?
    So far I have this in main...
    Code (Text):
    {
        do
        {
            printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
            scanf("%d", &option);
            getOption
        }
        while()
    }
    How do I use getOption to pick which function I want to choose?
    And what do I put in the argument of "while()"?
    Do I use bool to keep track of whether it has been sorted? Because I can never get bool to work.
    I'm slowly starting to understand. Slowly.
    Ok so the function will be like this...

    int function (something here)

    The "something here" is the information the main passes to the function? And then the main gets back some information from the function?
     
  14. Jun 16, 2012 #13

    Mark44

    Staff: Mentor

    No, don't get rid of the loop. You want to keep asking the user to select an option. When the user enters 5, that's the option to quit.
    Yes, but there will need to be some other logic in the loop. For starters, the user is allowed to enter 1, 2, 3, 4, or 5 and that's all. If the user enters one of these values, call a function to perform what the user is asking for.

    As pseudocode, it's a start.
    You don't. The only thing getOption needs to do is print the menu and return the choice. In your code in post #1 you have it looking for 0, 1, 2, 3, or 4. I would have it look for 1, 2, 3, 4, or 5. If the user chose one of these numbers, return that number. If the user chooses a number outside that range, I would have the function return 0.

    In main the code would look something like this:
    Code (Text):

    .
    .
    .
    result = getOption();
    if (result == 0)
    {
       // User entered a bad value, so run loop again.
    }
    else
    {
      // Call appropriate function to fill array, print array, etc.
    }
     
    How about while (option != 5)
    I don't know what C implementation you're using. bool was not part of earlier C offerings, so what people did was to set boolean type variables to either 1 (for true) or 0 (for false). If your compiler recognizes true and false, then you can set boolean variables to these values, and test them with if statements.
    Code (Text):
    if (expr == true) <do something>;
    else <do something else);
     
    Good!
    I don't know why you're asking this. In post #1 you have definitions of the functions you need. Some of them still need work. For example, you have fillArray and sortArray as functions that return an int value, but in the definitions you don't actually return anything. If they don't return a value, make them void functions.

    Void functions cause something to happen.
    Nonvoid functions evaluate to something.
    They are called in different ways.
    Code (Text):

    voidFunc();
    result = intFunc();
     
    A function that returns a value can have its return value stored in a variable (as above) or be a part of an expression, such as
    printf("Return value is ", intFunc());
    or as option + intFunc()

    Hope that helps...
     
  15. Jun 16, 2012 #14
    Code (Text):
    int main (result,option)
    {
        result = getOption();
        do
        {
            printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
            scanf("%d", &option);

            if (option==0 || option>5)
                printf("Invalid Option");

            else if (option==1)
                fillArray(option);

            else if (option==2)
                printArray(range);

            else if (option==3)
               



        }
        while(option!=5);
    }
    Under the "else if" statements, am I doing that right? I'm trying to get the thing to use the function I created that fills the array.
     
  16. Jun 16, 2012 #15

    Mark44

    Staff: Mentor

    More like this:
    Code (Text):

    [STRIKE]int main (result,option)[/STRIKE]
    int main(int argc, char ** argv)
    {
       // declarations all variables used in main here
       // following is a mix of C and pseudocode
        do
        {
          result = getOption();
          [STRIKE]if (option==0 || option>5)[/STRIKE]
          // Have getOption return 0 for any bad input (too big or too little)
          // and otherwise return 1, 2, 3, 4, or 5
          if (result == 0)
          {
             printf("Invalid option");
             continue;
          }

          else if (result==1)
             fillArray(option);
             // Why are you passing option to fillArray?
             // Think about the information that fillArray needs to create an array,
             // like a parameter for the array and another one for its size.
             // After the array is filled set an isFilled boolean variable to true.
             // The initial value of isFilled should be false.

          // Here you need more logic. The program should not print the array if
          // it isn't filled. The condition to print the array is result == 2 AND isFilled == true.
          // Also, what information does a function that needs to print an array need?
          else if (result==2)
             printArray(range);

          // As before, you shouldn't be able to search an array if it hasn't been filled.
          // Also, you have two kinds of seach capability - sequential or binary.
          // If the array has already been sorted use the binary search.
          // If the array has not been sorted, use the sequential search.
          // Keep track of the array status with another boolean variable, isSorted.
          // At the start, isSorted should be set to false. When the array has been
          // sorted, set this variable to true.
          // In summary when result == 3, you need to check two other
          // things: isFilled and isSorted.
          // Note that both of your seach functions return a value, so
          // plan to do something with that value.
          else if (result==3)
             search the array

          // Check first to see if the array has been filled.
          // After the array is sorted, set the boolean variable isSorted to true.
          else if (result == 4)
             sort the array

          else if (result == 5)
             print goodbye message
        }
        while(option!=5);
       return 0;
    }
     
    Looking back at the code you posted in post #1, there are some problems with it, particularly with fillArray and printArray, neither of which actually has an array type parameter, nor does anything with an array. These two functions will require more work.
     
  17. Jun 17, 2012 #16
    I can't find anything in my book or online on how to use "getOption". That one is new to me. Is there an alternate way?
    We've also never done anything with the "int argc, char ** argv" before. This is the first I've seen that.

    Does this look good so far?
    Code (Text):
    int option, array;
        bool isFilled;
        bool isSorted = false;



        do
        {
            isFilled = false;
            result = getOption();
            printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
            scanf("%d", &option);

            if (result == 0)
          {
             printf("Invalid option");
             continue;
          }

            else if (result==1)
             {fillArray(0-999, 50);
                isFilled = true;
             }

            else if (result==2 && isFilled = true)
                for(int i=0;i<5;, i++);
                    printf("%d", array[i])

            else if (result==3 && isSorted = true)
                {
                    seqSearch();
                    else
                        binarySearch();
                }
            else if (result==4 && isFilled = true)
                    sortArray();

            else if (result==5)
                    printf("Goodbye");
    }
        while(option!=5);
    }
     
    Thanks
     
  18. Jun 17, 2012 #17

    Mark44

    Staff: Mentor

    You write getOption.
    That's a pretty standard way of writing the arguments to main. If you haven't seen it before, you can omit them.
    Code (Text):

    int main()
    {
       ...
    }
    BTW, your code below is supposed to be the main function but you have omitted the header for it.
    There are a lot of mistakes. I'll summarize them at the end of my post.
    Major errors
    1. Your calls to seqSearch, binSearch, and sortArray have no arguments. In your first post you show definitions for these functions, all of which have parameters. You will not be able to get this program to compile (let alone run) until you have a clear understanding of how to pass arguments to a function.
    2. You are calling seqSearch and binarySearch as if they were void functions. They are not, based on the definitions for them that you showed in post #1. I'm guessing that these functions were given during class, because they look like they were written by someone who understands argument passing. I'm also guessing that you wrote the definitions for fillArray and printArray - they are pretty much useless as they are currently written, since they do not have the right parameters and they don't do anything with an array, despite their names. These functions need to be completely rewritten.
    The functions don't need to be fully implemented, but they need to be callable from main, which means that the argument list needs to be fleshed out so that main knows what information to pass to the function body.
    3. main needs to define the array that will be used by several of the functions it calls. You have a variable named array, but it is a scalar variable (capable of holding one value) not an array.
    4. Inside your loop you call getOption() and then print the menu. The whole reason for getOption is to print the menu and return the user's choice. The loop should not be displaying the menu options.
    5. Your call to fillArray looks like this --
    fillArray(0-999, 50);
    I believe you think you are passing a range of values, but 0 - 999 is a subtraction expression that evaluates to -999. As already noted, fillArray needs to be completely rewritten. To start you need to determine what information needs to be passed to this function in the argument list.
    6. For option 2 (print the array), you have this code:
    for(int i=0;i<5;, i++);
    printf("%d", array)
    You should be calling printArray to print the array, not doing it directly in main.
    Also, the loop here runs 5 times. Why do you think there are 5 values in the array?
    7. This logic is incorrect -
    seqSearch();
    else
    binarySearch();
    You have an else without a preceding if.

    Other errors
    1. Initialize isFilled to false at the top of main, not inside your loop. Doing so in the loop causes it to be reset each iteration of the loop.
    2. After you call sortArray, set isSorted to true. That's the variable you need to use to determine which of your sort functions to call.
    If the array has not been sorted, call seqSearch.
    If the array has been sorted, call binarySearch.

    There might be more problems, but these will give you something to do.
     
  19. Jun 17, 2012 #18
    My problem is I can never tell in coding examples whether a phrase like "getOption" specifically does something, or if it's just a variable worded like that for my sake, so I know where it goes in the program.

    Another reason I asked was because I looked it up and it kept bringing up "getOpt", and I wasn't sure if that was the same thing.
    I know, I just posted it like that to see if I'm headed in the right direction.

    Here's the whole thing of what I've changed. I'll list what I don't understand at the bottom.
    Code (Text):
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    int main ()
    {
        int result;
        int option;
        int array[50];
        bool isFilled = false;
        bool isSorted = false;

            isFilled = false;
            result = getOption();
            printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
            scanf("%d", &option);


        do
        {

            if (result == 0)
                {
                printf("Invalid option");
                continue;
                }
            else if (result==1)
             fillArray();
             

            else if (result==2 && isFilled = true)
                printArray(array[i]);

            else if (result==3 && isSorted = true)
                {
                    if
                        seqSearch(list[50],last,target,*location);
                    else
                        binarySearch(list[50],end,target,*location);
                }
            else if (result==4 && isFilled = true)
                    {sortArray(list[50],last);
                    isSorted = true;
                    }

            else if (result==5)
                    printf("Goodbye");



        }
        while(option!=5);
        return result;
    }
    int getOption (void)
    {
       
    }
    int fillArray (int option)
    {
        int range;
        int getOption (void);
        srand(time(NULL));
        range = (999 - 0) + 1;
    }
    int printArray (int range)
    {

        for(int i=0; i<50; i++);
        printf("%d", array[i]);

    }
    int seqSearch (int list[], int last, int target, int* location)
    {
        int looker;
        int found;

        looker = 0;
        while (looker < last && target != list[looker])
            looker++;

        *location = looker;
        found = (target == list[looker]);
        return found;

    }
    int binarySearch (int list[], int end, int target, int* location)
    {
        int first;
        int mid;
        int last;

        first = 0;
        last = end;
        while (first <= last)
            {
                mid = (first + last) / 2;
                if (target > list[mid])
                    first = mid + 1;
                else if (target < list[mid])
                    last = mid - 1;
                else
                    first = last + 1;
            }
        *location = mid;
        return target == list[mid];
    }
    void sortArray (int list[], int last)
    {
        int temp;
        int walker;
        for(int current = 0; current < last; current++)
            {
                for (int walker = last; walker > current; walker--)
                    if (list[walker] < list[walker - 1])
                        temp = list[walker];
                        list[walker] = list[walker - 1];
                        list[walker - 1] = temp;
            }
    }
     
    This thing never lets me use "bool". I can use "int" and "char" and "float" just fine, but the moment I try to use "bool" it gives me an error saying it's undeclared.

    I'm still not sure exactly what I do with getOption. Like I said above, I'm not sure if it's like printf and scanf and it does its own thing that the program sees and knows it does a specific thing. But some things like "isFilled" from my program don't do anything that has to do with the name. The program doesn't recognize the words "is filled" and then knows that it has to do with filling an array. The name is only the way it is so that I recognize what it's there for. You know what I mean? This is one of the things about programming that might be keeping my brain from understanding it. Because right now my brain is refusing to understand this.

    Well I changed everything, I think. It's only giving me errors about undefined things, like "bool" and "i" and all this other stuff.

    Thanks.
     
  20. Jun 17, 2012 #19

    rcgldr

    User Avatar
    Homework Helper

    "bool" is not a native C type. You could just use "int" instead, or you could typedef "bool" to "int". You didn't delcare "i" in the functions where you use it.
     
  21. Jun 17, 2012 #20

    Mark44

    Staff: Mentor

    You're making some progress... What you have is much better than the previous version.

    Comments
    1.
    bool isFilled = false;
    bool isSorted = false;

    Change these to int isFilled = 0; and similar for the other one.
    Also, remove the line just below that says isFilled = false;
    That's redundant.
    2. In this code,
    ....result = getOption();
    ....printf("1- Fill Array With Random Numbers\n2- Print the Array\n3- Search the Array\n4- Sort the Array\n5- Quit\n\n");
    ....scanf("%d", &option);
    The call to getOption should be inside the loop.
    The printf and scanf statements should be inside the getOption function.
    A question that you've asked before and I answered before still has you puzzled. getOption is not a standard library function like printf and scanf - it is a function that you write. printf and scanf and a bunch of others are already written for you. Your program can access them by including the header that has their prototypes, stdio.h.

    3. The first few lines in your loop should look like this:
    Code (Text):
        do
        {
            result = getOption();
            if (result == 0)
            {
                printf("Invalid option");
                continue;
            }
            else if (result==1)
            // and so forth
     
    4. You have this in the next else clause:
    Code (Text):

           else if (result==1)
             fillArray();
     
    As already noted, fillArray needs to be rewritten. At minimum main needs to pass the array as a parameter, and should also pass the size of the array. Any other information needed by fillArray (such as the range of values) should also be passed.

    Also, after you call fillArray(...), set isFilled to 1, signifying that the array has been initialized.
    5. The next else clause should have this logic:
    Code (Text):

           else if (result==3)
           {
              if the array hasn't been filled, print an error message and continue
              otherwise, if the array hasn't been sorted, call seqSearch
              otherwise, call binarySearch
           }
     
    Note that this logic is different from what I suggested before.
    6. Here's the logic you need for the next else clause
    Code (Text):

          else if (result==4)
          {
              if the array hasn't been filled, print an error message and continue
              otherwise, call sortArray and then set isSorted to 1.
          }
     
    Note that this logic is also different from what I advised earlier.

    7. fillArray
    This is what you currently have:
    Code (Text):

    int fillArray (int option)
    {
        int range;
        int getOption (void);
        srand(time(NULL));
        range = (999 - 0) + 1;
    }
     
    a. Passing option to this function makes no sense.
    b. getOption should not be called in this function.
    c. "range" is really two numbers - a minimum value and a maximum value. Both should be parameters whose values get passed in a call to this function.
    d. The parameters to this function should be - the array, the size of the array, the min value of each element of the array, the maximum value of each element of the array.
    e. The last line of your code sets range to 1000 - not what you want.
    As a start for this function, just have it fill an array with every number the same. Work on the details of getting random values after you are able to call the function from main.

    8. printArray
    Here's your code:
    Code (Text):

    int printArray (int range)
    {

        for(int i=0; i<50; i++);
        printf("%d", array[i]);

    }
    a. The range parameter makes no sense. The information that this function needs is the array itself and the size of the array. Nothing else.
    b. You have the function returning an int, but you don't actually return anything. This function seems an ideal candidate for being a void function.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Need help with menu driven search and sort program
Loading...