Thread: Help with menu program

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    8

    Help with menu program

    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?

    This is what I have so far.
    Code:
    #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;
            }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Set a flag when you call sort.
    Clear the flag when you call fill (or any other function which modifies the array)
    Test the flag to determine which function to call.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. menu driven program
    By tgarrisoniv in forum C Programming
    Replies: 14
    Last Post: 03-06-2012, 01:47 PM
  2. Menu for Restaurant Program.
    By francozb92 in forum C Programming
    Replies: 4
    Last Post: 04-03-2011, 09:49 AM
  3. Need big help with a menu program
    By angelofmyst in forum C Programming
    Replies: 7
    Last Post: 12-18-2010, 07:34 PM
  4. Menu program
    By DerrickakaDRoC in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 05:09 PM
  5. A Menu Program
    By Aidanjames86 in forum C++ Programming
    Replies: 6
    Last Post: 03-27-2006, 06:44 AM