473,382 Members | 1,165 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

outputting numbers in ascending order?

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 2815
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
Ok thanks heaps for that.
I'll see how I go with it.
Thanks again.
May 29 '07 #3
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
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

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

Similar topics

0
by: renster | last post by:
I have been looking at implementing the following but with a difference. http://forums.devshed.com/t47653/s.html This example uses a link which works with some code shown on the page. I have...
2
by: Alpay Eno | last post by:
Hello all... I'm using asp to get records from an access database, very similar to the way datagrid would work. The title of each column in my table is a link that alternates the sort order between...
2
by: Christopher M. Pieper | last post by:
I want to generate a resultset that is just a series of numbers in ascending order or perhaps a series of dates.. What I mean is, is there a way to generate a temporary table of dates given an...
6
by: sriram | last post by:
Hi, I have been seing a weird problem with db2look tool in db2 8.2 on Windows 2000 platform. When i spool the DDL using db2look, it spools the DDL in the ascending order of database objects...
5
by: M.Stanley | last post by:
Hi, I'm attempting to create a query that will combine 2 columns of numbers into one. The followng comes from 1 table with 4 fields (A,B,C,D) A B RESULT 700 000 700000 700 001 ...
3
by: cess | last post by:
If the user input three numbers, say (1,3,2), how it will become 1,2,3 or in ascending order?? there is something wrong/lacking with my code, help plzzzz!! import java.io.*; public class sign {...
4
by: xXmeeeeeXx | last post by:
Hi Everyone, I am trying to create a program that arranges numbers in ascending order. I do not understand how to do this. To start, think that I would need to include several if and else if...
5
by: bigmac | last post by:
I need to order four numbers and print in ascending order. I'm having troubles coming up with an algorithm to solve this. I should be able to write the code if I got something to work off of. Thanks.
1
by: pratimapaudel | last post by:
Can anyone help me to do this problem? Question goes like below: Use C or Borne shell. Take two integer command line arguments. The script should display the numbers between the two integers...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.