# sorting numbers

This is a discussion on sorting numbers within the C++ Programming forums, part of the General Programming Boards category; Hello! I want to make a program to arrange numbers in ascending order using arrays. Here is what I wrote ...

1. ## 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'?

2. 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;
}```

3. 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...

4. 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.

5. Originally Posted by Sky_Daughter
while (found = 1) {
You should read up on the differences between assignment and comparison.

6. 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.

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