how can sort array using do while it should get sorted only on entering 0?

how can i do this using do while i dont want user to put no. of elements user should enter numbers and only get sorted when he enter 0.please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include<iostream>
using namespace std;

	int main()
{
	int a[20],i,j,num,n;
	
	cout<<"enter number of elements"<<endl;
	cin>>n;
	cout<<"enter the elements one by one"<<endl;
	for(i=0;i<n;i++)
	cin>>a[i];
	for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[j]>a[i])
{
num=a[j];
a[j]=a[i];
a[i]=num;
}
}
}
	
	cout<<"the sorted array in descending order is";
	for(int b=0;b<n;b++)
		cout<<endl<<a[b];
return 0;
}
Instead of line 8/9 set n=0; // Important!
In your first loop: Ad an if -> a[i] == 0 -> break; othewise n++;
Topic archived. No new replies allowed.