-
February 2nd, 2007, 01:33 AM
#1
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:utput()
{
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...
Last edited by Reon; February 2nd, 2007 at 03:00 AM.
Reason: updation needed
-
February 2nd, 2007, 01:37 AM
#2
Re: C++ ascending and descending???
Please show us your code.
Before post, make an effort yourself, try googling or search here.
When posting, give a proper description of your problem, include code* and error messages.
*All code should include code tags
-
February 2nd, 2007, 01:55 AM
#3
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
}
- Sreehari
"Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us."
" Everybody is sent to Earth on a purpose. I am so Lagging behind that i won't die." – Calvin
-
February 2nd, 2007, 02:10 AM
#4
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.
-
February 2nd, 2007, 02:17 AM
#5
Re: C++ ascending and descending???
Cilu you're too fast!
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;
}
-
February 2nd, 2007, 06:17 AM
#6
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 2nd, 2007, 01:35 PM
#7
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....
Originally Posted by Mybowlcut
Cilu you're too fast!
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;
}
-
February 2nd, 2007, 01:52 PM
#8
Re: C++ ascending and descending???
i only know conio,iostream & fstream..i s there any probability to complete the program in that way....
Originally Posted by Mybowlcut
Cilu you're too fast!
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;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|