Thread: Help with sort program

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

    Help with sort program

    Hello, I think I'm almost finished with this program, it just needs a few things changed. When I choose any option from the menu, it begins spamming the screen. Anyone know what's wrong with it?

    Code:
    /* This program prompts user to choose an option from the menu
    and performs the action on an array. */
    
    #include <stdbool.h>
    #include <stdio.h>
    int fillArray (int range);
    void printArray ();
    int binarySearch (int list[], int end, int target, int *location);
    int seqSearch (int list[], int last, int target, int *location);
    void sortArray (int list[], int last);
    int main()
    {
        bool sorted;
        int option = -1;
        int getOption;
        int range;
        int i;
        int ary[50];
        int list[50];
        int last;
        int target;
        int *location;
        int end;
    
         while (option != 5)
        {
            printf("1- Fill Array\n2- Print Array\n3- Search Array\n4- Sort Array\n5- Quit\n");
            scanf("%l", &option);
            switch (option)
            {
               case 1:
                   fillArray(range);
                   sorted = false;
                   break;
    
               case 2:
                  printArray();
                  break;
    
               case 3:
                  if (sorted)
                    binarySearch(list[50], end, target, *location);
                  else
                    seqSearch(list[50], last, target, *location);
                  break;
    
              case 4:
                 sortArray(list[50], last);
                 sorted = true;
                 break;
    
              default:
                 printf("%c is an invalid choice.\n");
            }
        }
        return 0;
    }
    int fillArray (int range)
    {
        srand(time(NULL));
        range = (1, 999);
    
    }
    void printArray ()
    {
        int i;
        int ary[50];
        for(i=0; i<50; i++);
        printf("%d", ary[i]);
    
        return 0;
    }
    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];
    }
    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;
    
    }
    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;
            }
    return;
    }
    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Initialize the variables, sorted, target, end etc., defined in main() before using or passing them to other functions.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    Code:
    scanf("%l", &option);
    What type are you trying to read in with the "%l" format?

    scanf - C++ Reference

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    Quote Originally Posted by itCbitC View Post
    Initialize the variables, sorted, target, end etc., defined in main() before using or passing them to other functions.
    What do I initialize them with?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by leroyjenkens View Post
    What do I initialize them with?
    With whatever you need them to be for the program to work, or anything valid (such as zero) if you plan on giving them more meaningful values before using them in equations or passing them to functions.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    8
    I don't know, I'm the worst in the world at this so random garbage shows up in my code constantly.

    Ok I initialized them all, now my program will fill the array and then print one random number. How do I make it print 50 random numbers?

    And it shuts down when I try option 3.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    What do you think this line of code does?
    Because, I think you expect a different result then what really happens.

    Code:
    range = (1, 999);
    You code in fillArray does NOT fill an array!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    " int i;
    int ary[50];
    for(i=0; i<50; i++);
    printf("%d", ary[i]);"

    The set of instructions won't be able to identify the iterations since there's no identifier in that set.
    ˙uıɐƃɐ ʎɐq-ǝ ɯoɹɟ pɹoqʎǝʞ ɹǝɥʇouɐ ƃuıʎnq ʇou ɯı

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The semi colon at end of for line is very unlikely to be correct.

    Tim S.

    Code:
    for(i=0; i<50; i++);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Word Sort Program
    By daggerhunt in forum C Programming
    Replies: 9
    Last Post: 03-20-2012, 01:01 PM
  2. quick sort program
    By rakeshkool27 in forum C Programming
    Replies: 7
    Last Post: 05-21-2010, 02:29 AM
  3. another sort useless program
    By mrsirpoopsalot in forum C Programming
    Replies: 21
    Last Post: 09-18-2006, 02:35 AM
  4. Help On A Quick Sort Program
    By nick4 in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2004, 10:51 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 08:05 PM