By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
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
  1. void numbers (int draw[], int entry[])
  2. {
  3.     for(int i=0; i<MAXSIZE; i++)
  4.     {
  5.         for(int j=0; j<MAXSIZE; j++)
  6.         {
  7.             if(draw[i]==entry[j])
  8.             cout << draw[i] << " ";
  9.         }
  10.  
  11.     }
  12.     cout << endl;
  13. }
So can you help me to output the numbers in ascending order?
Thanks
May 29 '07 #1
Share this Question
Share on Google+
6 Replies


gpraghuram
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

ilikepython
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
  1. void swap (int draw[], int from, int to)
  2. {
  3.      int temp = draw[from];
  4.      draw[from] = draw[to];
  5.      draw[to] = temp;
  6. }
  7.  
  8. void numbers (int draw[], int entry[])
  9. {
  10.     int temp = 0;
  11.     for(int i=0; i<MAXSIZE; i++)
  12.     {
  13.         for(int j=0; j<MAXSIZE; j++)
  14.         {
  15.             if(draw[i]==entry[j])
  16.              if (draw[i] > draw[i+1])
  17.               {
  18.                  swap(draw[i], draw[i+1]);
  19.               } 
  20.  
  21.         }
  22.  
  23.           cout << draw[i] << " ";  
  24.     }
  25.  
  26.    cout << endl;
  27. }
and....

Expand|Select|Wrap|Line Numbers
  1. void numbers (int draw[], int entry[])
  2. {
  3.     int temp = 0;
  4.     for(int i=0; i<MAXSIZE; i++)
  5.     {
  6.         for(int j=0; j<MAXSIZE; j++)
  7.         {
  8.             if(draw[i]==entry[j])
  9.              if (draw[i] > draw[i+1])
  10.               {
  11.                  temp = draw[i];
  12.                  draw[i] = draw[i+1];
  13.                  draw[i+1] = temp;
  14.  
  15.               } 
  16.  
  17.         }
  18.  
  19.           cout << draw[i] << " ";  
  20.     }
  21.  
  22.    cout << endl;
  23. }
Any idea why it's only changing the one number?
Thanks
May 30 '07 #6

ilikepython
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
  1. void swap (int draw[], int from, int to)
  2. {
  3.      int temp = draw[from];
  4.      draw[from] = draw[to];
  5.      draw[to] = temp;
  6. }
  7.  
  8. void numbers (int draw[], int entry[])
  9. {
  10.     int temp = 0;
  11.     for(int i=0; i<MAXSIZE; i++)
  12.     {
  13.         for(int j=0; j<MAXSIZE; j++)
  14.         {
  15.             if(draw[i]==entry[j])
  16.              if (draw[i] > draw[i+1])
  17.               {
  18.                  swap(draw[i], draw[i+1]);
  19.               } 
  20.  
  21.         }
  22.  
  23.           cout << draw[i] << " ";  
  24.     }
  25.  
  26.    cout << endl;
  27. }
and....

Expand|Select|Wrap|Line Numbers
  1. void numbers (int draw[], int entry[])
  2. {
  3.     int temp = 0;
  4.     for(int i=0; i<MAXSIZE; i++)
  5.     {
  6.         for(int j=0; j<MAXSIZE; j++)
  7.         {
  8.             if(draw[i]==entry[j])
  9.              if (draw[i] > draw[i+1])
  10.               {
  11.                  temp = draw[i];
  12.                  draw[i] = draw[i+1];
  13.                  draw[i+1] = temp;
  14.  
  15.               } 
  16.  
  17.         }
  18.  
  19.           cout << draw[i] << " ";  
  20.     }
  21.  
  22.    cout << endl;
  23. }
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
  1. void selectionsort(int array[], int size);  //or whatever algorithm you want to use
  2.  
May 30 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.