Thread: Array's Question sorting numbers!

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    10

    Array's Question sorting numbers!

    Hi everyone, just self studying C Programming for the summer. I was wondering if you could give me some advice on what to do here. In the book, one of the challenges ask me to as the user to input 10 numbers, and then sort and print the 10 numbers in ascending or descending order.

    I am not sure how to sort the arrays in order.

    My Check() function was just to experiment with how arrays work.
    I plan to put the code for sorting the stored numbers in function store()

    Sincerely,
    Jomoka


    Code:
    #include <stdio.h>
    
    //function
    void check();
    int sort(int, int);
    
    //Global Variables
    int x;
    int number[10];
    
    main() {
    
        int iStore = 0;
        int iResponse = 0;
    
        printf("\nWelcome to the number storage thing!\n");
    
        printf("\n You are allowed to store 10 numbers\n");
    
    //For loop that goes through each array and ask the user to input a number to 
    //store in each 10 arrays from address 0 to 9
           for (x = 0; x < 10; x++) {
    
        printf("Enter number to store in storage %d: \t", x);
        scanf("%d", &iStore);
        number[x] = iStore;
    
        }//End For loop
        check();
    }
    
    
    //Function Definition
    
    check() {
    
    //The variables below is to understand arrays, checking to see 
    //if the numbers stored will actually show the element it's stored in
        int iValue;
        int iFound = -1;
    
    
    //The code to check what element this value is located in
        printf("Find value to search for: ");
        scanf("%d", &iValue);
    
        for (x = 0; x < 10; x++) {
    
        if (number[x] == iValue) {
    
        iFound = x;
        break;
    
    }
    
    }
    
    if (iFound > -1)
    
        printf("\nI found your search value in element %d\n", iFound);
    else
        printf("\nSorry, your search value was not found\n");
    
    }
    
    //Function Definition
    sort() {
    
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First things first:
    1. Global variables are evil, nasty, dirty things. Avoid them at all costs. Learn to pass parameters to your funcitons.
    2. It's int main(void) and return an int at the end, usually 0.
    3. You need to give explicit return types and parameters for all of your functions (definition and prototype). Check out our function tutorial and Google some more.


    Also, you could have Googled "sorting algorithms in C", or searched this forum for the hundreds of previous sorting questions we've had, but I'll save you the effort. This is one of the easier sort routines to implement in C, and one of the first that most people learn: Bubble sort - Wikipedia, the free encyclopedia.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Thanks anduril462, i will look into all of this and get back to you all

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sorting routines generally use two loops, usually nested one inside the other (except one weird one called Gnome sort, which is well, weird and slow).

    Code:
    for(i = 0; i < 10-1; i++) {
       for(j = i+1; j < 10; j++) {
          make your comparisons in here
          and switch if a number is out of order
          to make the switch, you'll want a temp variable to hold a value for a bit.
       }
    }
    Don't get too comfortable with the global variables - they're a crutch for the most part, and can be quite troublesome, later on.

    It's always int main, never just "main", and then return 0 at the end of your main function.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    It works! thanks guys! That was awesome. Thanks for all your help. I should probably study algorithms for C too besides just following the book.

    Code:
    #include <stdio.h>
    
    /**
    Reference websites:
    
    http://en.wikipedia.org/wiki/Bubble_sort
    **/
    
    //function
    void check();
    void sort();
    
    int number[10];
    
    int main(void) {
    
        int x;
        int iStore = 0;
    
        printf("\nWelcome to the number storage thing!\n");
    
        printf("\n You are allowed to store 10 numbers\n");
    
    //For loop that goes through each array and ask the user to input a number to store in each 10 arrays from address 0 to 9
           for (x = 0; x < 10; x++) {
    
        printf("Enter number to store in storage %d: \t", x);
        scanf("%d", &iStore);
        number[x] = iStore;
    
        }//End For loop
    
        //check();
    
        sort();
        return 0;
    }
    
    
    //Function Definition
    void check() {
    //The variables below is to understand arrays, checking to see if the numbers stored will actually show the element it's stored in
        int x;
        int iValue;
        int iFound = -1;
    
    
    //The code to check what element this value is located in
        printf("Find value to search for: ");
        scanf("%d", &iValue);
    
        for (x = 0; x < 10; x++) {
    
        if (number[x] == iValue) {
    
        iFound = x;
        break;
    
    }
    
    }
    
    if (iFound > -1)
    
        printf("\nI found your search value in element %d\n", iFound);
    else
        printf("\nSorry, your search value was not found\n");
    
    }
    
    //Function Definition
    void sort() {
    
    int x;
    int y;
    int temp;
    
    //procedure bubbleSort( number : list of sortable items )
    
      for (x = 0; x < 10; x++) {
    
         for (y = 10-1; y > x; y--){
    
            if (number[y-1] > number[y]) {
    
            temp = number[y];
            number[y] = number[y-1];
            number[y-1] = temp;
    
    }
    
    }
            printf("\n%d\n", number[x]);
    }
    
    }
    Last edited by Jomoka; 06-16-2011 at 09:11 AM.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Welcome to the forum Jomoka,

    You should try to study algorithms in general not necessarily their C implementation. It is very rare for an algorithm to depend too much on the language it is implemented in, and such cases only occur when the language syntax is very narrowly guided towards a specific use.

    This might actually provide a good challenge to you and help your learning curve: to implement known algorithms you read in pseudo-code, in C.

    EDIT: I might sound a little pedantic here but int main() should really be int main(void). There is no difference in semantics in this case, but I will leave to you to figure out why that is.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    10
    Claudiu, or anyone have Algorithm books to recommend? I plan to go to the library sometime this week.

    Thanks Claudiu, and i appreciate all your help guys
    Last edited by Jomoka; 06-16-2011 at 11:24 AM.

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Jomoka,

    I don't know how much your library would help although they should have at least basic things about algorithms regardless of where you live. I would start by looking at sorting and searching algorithms, moving forward to learning a bit about data structures, and then maybe studying a little bit of graph theory and related algorithms. I think that should provide a solid foundation that you can then build on.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting the numbers
    By minyoungan in forum C Programming
    Replies: 3
    Last Post: 09-29-2010, 11:10 AM
  2. help with sorting numbers
    By artistunknown in forum C Programming
    Replies: 12
    Last Post: 02-07-2010, 12:00 AM
  3. sorting an array of numbers in C
    By JVincent08 in forum C Programming
    Replies: 2
    Last Post: 03-14-2008, 01:42 PM
  4. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 03:41 AM
  5. Sorting numbers in a file
    By pxleyes in forum C Programming
    Replies: 19
    Last Post: 04-15-2004, 06:17 AM