470,817 Members | 1,392 Online

# outputting numbers in ascending order? 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
6 2521 gpraghuram
1,275 Expert 1GB
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
flyaway888
35 Ok thanks heaps for that.
I'll see how I go with it.
Thanks again.
May 29 '07 #3
flyaway888
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
844 Expert 512MB
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
flyaway888
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
844 Expert 512MB
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

 reply views Thread by renster | last post: by 2 posts views Thread by Alpay Eno | last post: by 2 posts views Thread by Christopher M. Pieper | last post: by 6 posts views Thread by sriram | last post: by 5 posts views Thread by M.Stanley | last post: by 3 posts views Thread by cess | last post: by 4 posts views Thread by xXmeeeeeXx | last post: by 5 posts views Thread by bigmac | last post: by 1 post views Thread by pratimapaudel | last post: by reply views Thread by ryjfgjl | last post: by reply views Thread by AlexandraMT | last post: by reply views Thread by prepawan | last post: by 1 post views Thread by DANILIN | last post: by reply views Thread by tracyyun | last post: by reply views Thread by MarkDoronin | last post: by reply views Thread by xykovyci | last post: by reply views Thread by patriciajames | last post: by 1 post views Thread by Stoney L | last post: by