Reputation: 640
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a, b, c[5], d;
cout << "enter any five number\n";
for(a=0; a<5; a++)
{
cin>>c[a];
}
for(a=0; a<5; a++)
{
for(b=++a; b<5; b++)
{
if(c[b] < c[a])
{
d = c[b];
c[b] = c[a];
c[a] = d;
}
}
}
cout << "\nhere is the entered numbers in order\n";
for(a=0; a<5; a++)
{
cout << c[a];
cout << endl;
}
getch();
return 3;
}
I am desk checking this program and I expect the program to sort numbers in ascending order but I am getting the wrong output help please.
Upvotes: 0
Views: 101
Reputation: 100
for(a=0; a<5; a++)
{
for(b=++a; b<5; b++)
{
if(c[b] < c[a])
{
d = c[b];
c[b] = c[a];
c[a] = d;
}
}
}
this should be
for(a=0; a<4; a++)
{
for(b=a+1; b<5; b++)
{
if(c[b] < c[a])
{
d = c[b];
c[b] = c[a];
c[a] = d;
}
}
}
Upvotes: 0
Reputation: 5905
for(a=0; a<5-1; a++){
for(b=0; b<5-a-1; b++){
if(c[b] < c[a]){
d = c[b];
c[b] = c[a];
c[a] = d;
}
}
}
Upvotes: 0
Reputation: 33506
for(a=0;a<5;a++)
and for(b=++a;..)
causes a
to be incremented twice.
Did you mean for(b=a+1;b<5;b++)
?
Upvotes: 1