# sorting numbers

• 10-15-2011
Sky_Daughter
sorting numbers
Hello!
I want to make a program to arrange numbers in ascending order using arrays.
Here is what I wrote :
Code:

``` #include <iostream> using namespace std; int main() {     int i,n,a[100],aux,found;     cout << "n = " ;     cin >> n;     for (i = 0; i < n; i++) {       cout << "a[" << i+1 << "]=";       cin >> a[i];       found = 0;     if (a[i] > a[i+1] ) {     aux = a[i];     a[i] = a[i+1];     a[i+1] = aux;     found = 1;   }   }   while (found = 1) {     for (i = 1; i < n;i++) {     cout << a[i];   }   } return 0; }```
Something is wrong in my program . What is it? The 'while'?
• 10-15-2011
manasij7479
Understand the algorithm first.
What you're doing is somewhat jumbled up(trying to sort while taking the input and so on..)
If this is supposed to be bubble sort, read the wikipedi article on it.

If you aren't required to write the sort function yourself ; do something like this :
Code:

```#include<algorithm> #include<array> #include<iostream> using namespace std; bool comp(int x,int y){return x<y;} int main() {         array<int, 5> foo = {4,67,22,5,7};         sort(foo.begin(),foo.end(),comp);         for(auto x:foo)cout<<x<<endl;         return 0; }```
• 10-15-2011
Sky_Daughter
Thank you very much for your reply but I want to know why my program doesn't work and what am I supposed to do to fix the errors.
Actually,I have to find out how many ascending sequence are in the n numbers of the array and I don't know how to do this...
• 10-15-2011
manasij7479
Quote:

what am I supposed to do to fix the errors
Make up your mind. Do you want to sort or "find out how many ascending sequence are in the n numbers of the array" .

case 1: Read up on sorting algorithms and implement one of the basic ones correctly.

case 2: Think about the problem; how you'd find it out in your mind. Make a flow chart. After that attempt writing the code.
• 10-15-2011
MWAAAHAAA
Quote:

Originally Posted by Sky_Daughter
while (found = 1) {

You should read up on the differences between assignment and comparison.
• 10-15-2011
Elysia
Quote:

Originally Posted by manasij7479
Understand the algorithm first.
What you're doing is somewhat jumbled up(trying to sort while taking the input and so on..)
If this is supposed to be bubble sort, read the wikipedi article on it.

If you aren't required to write the sort function yourself ; do something like this :
Code:

```#include<algorithm> #include<array> #include<iostream> using namespace std; bool comp(int x,int y){return x<y;} int main() {         array<int, 5> foo = {4,67,22,5,7};         sort(foo.begin(),foo.end(),comp);         for(auto x:foo)cout<<x<<endl;         return 0; }```

There should be no need for a comparator function here since you are using integers.
• 10-15-2011
cyberfish
Quote:

Something is wrong in my program . What is it? The 'while'?
May be a good idea to post the errors?