Here you're wrong:
for(i=0;i<5;i++) //the correct code should have been: for(i=0;i<4;i++)
{
for(j=1;j<5;j++) //the correct code should have been: for(j=i+1;j<5;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
I hope it's helpful for you.You need understand why the code is like this.You
need know why the algorithm can work.Please Google.
To: [email protected]
From: [email protected]
Date: Thu, 26 Aug 2010 06:03:34 +0000
Subject: [c-prog] Help!!! :-(
please help me for this program. i'm a beginner. For the below program tell me
why it doesn't give the right output. Is there any problem in logic or loop i
given.
please please please help me! :-( :-( :-(
/*write a program to sort five numbers in ascending order*/
/*********************************************************/
#include<iostream.h>
#include<conio.h>
void main()
{
int array[5],i,j,temp=0;
clrscr();
cout<<"Enter any five number:\n";
for(i=0;i<5;i++)
{
cin>>array[i];
}
for(i=0;i<5;i++)
{
for(j=1;j<5;j++)
{
if(array[i]>array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
cout<<"The sorted array is:";
for(i=0;i<5;i++)
{
cout<<"\n"<<array[i];
}
getch();
}
[Non-text portions of this message have been removed]
[Non-text portions of this message have been removed]