# Array's Question sorting numbers!

Printable View

• 06-15-2011
Jomoka
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() { }```
• 06-15-2011
anduril462
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.
• 06-15-2011
Jomoka
Thanks anduril462, i will look into all of this and get back to you all
• 06-15-2011
Adak
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.
• 06-16-2011
Jomoka
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]); } }```
• 06-16-2011
claudiu
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.
• 06-16-2011
Jomoka
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
• 06-16-2011
claudiu
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.