| New Reply |
numbers in Ascending Order in C++ |
Share Thread | Thread Tools |
| Apr28-08, 03:11 AM | #1 |
|
|
numbers in Ascending Order in C++
i have to write such a program that takes 10 integers from user and show them in ascending order.
My attempt was --- #include<iostream.h> #include<conio.h> int main() { int a[10]; for(int i=0; i<10; i++) //this loop is used for getting the 10 integers from user cin>>a[i]; ...................... // here I'm stuck. I couldn't find how should i sort them. I can sort // number using 'if..else if' statement which makes the program lengthy // for this propblem. getch(); return 0; } So plz help me if any one knows how to sort 10 integers so that it doesn't make the codes lengthy. thanks in advance |
| Apr28-08, 03:47 AM | #2 |
|
|
Why not just use qsort? Or are you required to actually write the low-level sorting algorithm directly? If so, I'd look at the Wikipedia pages on qsort and heapsort for some ideas; they have pretty good explanations and psuedocode.
|
| Apr28-08, 04:45 AM | #3 |
|
Mentor
Blog Entries: 9
|
A bubble sort is quick and easy to code. Simple go through your list swaping adjacent numbers if they are out of order. Repeat until there are no swaps.
|
| Apr28-08, 06:57 AM | #4 |
|
|
numbers in Ascending Order in C++
If you are allowed to use the stl and the vector container, try sort().
|
| Apr28-08, 09:00 AM | #5 |
|
|
thanks to all replied...Actually I'm newbie in c++. I haven't studied qsort, heapsort or vector container..
swapping technique is suitable for my level...I used it in the for loop for(int p=0; p<9; p++) { for(int i=0; i<9; i++) { if(a[i]>a[i+1]) // comparison between two adjacent array elements { holder=a[i]; // holder holds the value temporarily a[i]=a[i+1]; a[i+1]=holder; } } } |
| Apr28-08, 10:23 AM | #6 |
|
Recognitions:
|
Depends what sort of class this is !
If you are supposed to write the sort then bubble sort is easiest - justify your choice over a qsort by saying that the speed difference is small for only 10numbers ! If this is a real programming class then use. vector<int>a; for( .... a.push_back( input ) } sort(a.begin(),a.end()); |
| Aug13-11, 01:21 AM | #7 |
|
|
obviously it vl not show the output coz nobody written cout<<a;
line anywhere i tried to write it at d end of all for loops bt it shows address of a integer |
| Aug13-11, 03:12 AM | #8 |
|
Recognitions:
|
|
| Aug13-11, 08:11 PM | #9 |
|
|
mdnazmulh, do not use the header iostream.h. It is deprecated, and not part of standard C++.
Please use the following instead. Code:
#include <iostream> Code:
using namespace std; |
| Jun22-12, 11:26 PM | #10 |
|
|
Code:
#include <iostream.h>
void main()
{
const int max=50; //sets the maximum size of the array
int num[max]; //the array can be initialized from here num={2,3,6,4,3,7,8,,4.....n};
int holder;
//if array is initialized then no need for this for loop
for(int m=0; m<max; m++)
{
cout<<"Enter number "<<(m+1)<<" : ";
cin>>num[m];
}
for(int p=0; p<(max-1); p++)
{
for(int i=0; i<(max-1); i++)
{
if(num[i]>num[i+1]) // comparison between two adjacent array elements
{
holder=num[i]; // holder holds the value temporarily
num[i]=num[i+1];
num[i+1]=holder;
}
}
}
//now to print the sorted array
for(int j=0; j<max; j++)
{
cout<<" "<<num[j]; //This prints the array out in ascending order on the same line
//<<endl; could be placed at the end to print on different line
}
}
|
| Jul17-12, 07:48 AM | #11 |
|
|
#include <iostream.h>
#include<conio.h> void main ( ) { int arr[100] , size , hold ; START : cout << "\n Enter the size of the array ( not more than 100 ) : " ; cin >> size ; if ( size > 100 ) { cout << "\n Size is more than 100 ... Please re enter .. " ; getch ( ) ; clrscr ( ) ; goto START ; } cout << "\n Enter " << size << "elements ... \n" ; for ( int i = 0 ; i < size ; i ++ ) cin >> arr[i]; for ( i = 0 ; i < (size-1) ; i++ ) { for ( int j = i+1 ; j < size ; j++ ) { if ( arr[j] < arr[i] ) { hold = arr[j] ; arr[j] = arr[i] ; arr[i] = hold; } } } cout << "\n The sorted array : \n "; for ( i=0 ; i < size ; i++ ) cout << arr[size] << " "; getch ( ) ; } |
| New Reply |
| Thread Tools | |
Similar Threads for: numbers in Ascending Order in C++
|
||||
| Thread | Forum | Replies | ||
| Solving a PDE : 2 order in time, 4 order in space, mixed derivatives | Differential Equations | 20 | ||
| under damp ing spring, eigen, 2-nd order eqn as 2 1-st order in matrix form. | Calculus & Beyond Homework | 1 | ||
| Ascending to a higher state of being. | General Discussion | 1 | ||