C++ ascending and descending???
IN c++... I get output on 10 numbers after i entered sufficient data, i want to make that numbers either in ascending or in descending orders ..
here is my code but is asks for 10 numbers to print thats ok.. but i want to displayed all the numbers in different locations and want to print like ... number is 2.4.7,9,etc in ascendin orders....
#include<iostream.h>
#include<conio.h>
class loops
{
int num;
int times;
public:
void display()
{
times=0;
for(;;times++)
{
if(times>=10)
{
break;
}
cout<<endl<<"Enter a number";
cin>>num;
num=num;
}
}
void output();
};
void loops::output()
{
num=0;
for(;;num++)
{
if(num>=10)
{
break;
}
cout<<endl<<"Number is :"<<num;
}
}
void main()
{
clrscr();
loops l1;
l1.display();
l1.output();
getch();
}
Would anybody pls know help me to solve this...
Re: C++ ascending and descending???
Please show us your code.
Re: C++ ascending and descending???
There are a lot of sortng methods. An easy and straigh forward one would be
Code:
smallest = number 1
outerloop
{
inner loop
{
if number2 > smallest then
smallest = number2
else
do the same with next number ( number3..4..and so on ) in innerloop
}
print smallest
exclude the first smallest number
return to outerloop to find second smallest
}
Re: C++ ascending and descending???
Search the web for sorting methods:
- bubblesort
- shakesort
- shellsort
- quicksort
- radix sort
Well, you can always use std::sort from <algorithms>
Code:
int array[10]; // fit it in
std::sort(&array[0], &array[9]);
or
Code:
std::vector<int> array; // fill it in
std::sort(array.begin(), array.end());
To decide the order, ascending or descending, use a boolean predicate as the third parameter. Look in MSDN for the std::sort documentation.
Re: C++ ascending and descending???
Cilu you're too fast! :lol:
My suggestion is to use vectors! :) Here's my suggestion's code anyways...
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int>::const_iterator iter;
vector<int> listOfNums;
for(int i = 0; i < 10; ++i)
{
cout << "\n\n"<<i + 1<<". Enter a number: ";
cin >> number;
listOfNums.push_back(number);
}
sort(listOfNums.begin(), listOfNums.end());
for(iter = listOfNums.begin(); iter != listOfNums.end(); ++iter)
{
cout << *iter << endl;
}
return 0;
}
Re: C++ ascending and descending???
As for the predicate, you can use less<T> (which I think is the default) and greater<T> for std::sort.
Re: C++ ascending and descending???
I'm not familiar with vector and so.. i only know conio,iostream & fstream..i s there any probability to complete the program in that way....
Quote:
Originally Posted by Mybowlcut
Cilu you're too fast! :lol:
My suggestion is to use vectors! :) Here's my suggestion's code anyways...
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int>::const_iterator iter;
vector<int> listOfNums;
for(int i = 0; i < 10; ++i)
{
cout << "\n\n"<<i + 1<<". Enter a number: ";
cin >> number;
listOfNums.push_back(number);
}
sort(listOfNums.begin(), listOfNums.end());
for(iter = listOfNums.begin(); iter != listOfNums.end(); ++iter)
{
cout << *iter << endl;
}
return 0;
}
Re: C++ ascending and descending???
i only know conio,iostream & fstream..i s there any probability to complete the program in that way....
Quote:
Originally Posted by Mybowlcut
Cilu you're too fast! :lol:
My suggestion is to use vectors! :) Here's my suggestion's code anyways...
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<int>::const_iterator iter;
vector<int> listOfNums;
for(int i = 0; i < 10; ++i)
{
cout << "\n\n"<<i + 1<<". Enter a number: ";
cin >> number;
listOfNums.push_back(number);
}
sort(listOfNums.begin(), listOfNums.end());
for(iter = listOfNums.begin(); iter != listOfNums.end(); ++iter)
{
cout << *iter << endl;
}
return 0;
}