454,385 Members | 1,737 Online Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

# outputting numbers in ascending order?

 P: 35 Hey, I have a function that compares 2 arrays with 6 numbers in each and outputs the matching numbers. I have written a function that does this but I need to output the numbers in ascending order and I can't figure out how. This is the function... Expand|Select|Wrap|Line Numbers void numbers (int draw[], int entry[]) {     for(int i=0; i
6 Replies

 Expert 100+ P: 1,275 HI, After calling the function u have written sort the array using qsort which is available in stdio.h. You may have to write a compare function for it. After thet print the array which will be printed in Ascending order. Thanks Raghuram May 29 '07 #2

 P: 35 Ok thanks heaps for that. I'll see how I go with it. Thanks again. May 29 '07 #3

 P: 35 We havn't learnt qsort yet so I probably shouldn't use it. Is there any other way to do it inside my function? Thanks May 30 '07 #4

 Expert 100+ P: 844 We havn't learnt qsort yet so I probably shouldn't use it. Is there any other way to do it inside my function? Thanks You can write your own sorting function. If speed isn't an issue use something simple like the bubble sort or selection sort. Check out these articles: Bubble Sort Selection Sort They are really easy to use. May 30 '07 #5

 P: 35 I tried the selection sort and my own kind of version of it and both only change the output so the largest number is at the end but all the others stay the same. So I got 5 4 3 2 1 6 instead of 1 2 3 4 5 6 These are the two things i tried... Expand|Select|Wrap|Line Numbers void swap (int draw[], int from, int to) {      int temp = draw[from];      draw[from] = draw[to];      draw[to] = temp; }   void numbers (int draw[], int entry[]) {     int temp = 0;     for(int i=0; i draw[i+1])               {                  swap(draw[i], draw[i+1]);               }            }             cout << draw[i] << " ";       }      cout << endl; } and.... Expand|Select|Wrap|Line Numbers void numbers (int draw[], int entry[]) {     int temp = 0;     for(int i=0; i draw[i+1])               {                  temp = draw[i];                  draw[i] = draw[i+1];                  draw[i+1] = temp;                 }            }             cout << draw[i] << " ";       }      cout << endl; } Any idea why it's only changing the one number? Thanks May 30 '07 #6

 Expert 100+ P: 844 I tried the selection sort and my own kind of version of it and both only change the output so the largest number is at the end but all the others stay the same. So I got 5 4 3 2 1 6 instead of 1 2 3 4 5 6 These are the two things i tried... Expand|Select|Wrap|Line Numbers void swap (int draw[], int from, int to) {      int temp = draw[from];      draw[from] = draw[to];      draw[to] = temp; }   void numbers (int draw[], int entry[]) {     int temp = 0;     for(int i=0; i draw[i+1])               {                  swap(draw[i], draw[i+1]);               }            }             cout << draw[i] << " ";       }      cout << endl; } and.... Expand|Select|Wrap|Line Numbers void numbers (int draw[], int entry[]) {     int temp = 0;     for(int i=0; i draw[i+1])               {                  temp = draw[i];                  draw[i] = draw[i+1];                  draw[i+1] = temp;                 }            }             cout << draw[i] << " ";       }      cout << endl; } Any idea why it's only changing the one number? Thanks You should have a separate function for your sort. You should probably get your final array and then sort it after you are done. It is hard to tell how it will sort with your code in between. Write a function like: Expand|Select|Wrap|Line Numbers void selectionsort(int array[], int size);  //or whatever algorithm you want to use   May 30 '07 #7 