array in descending order doesn't work in C
#include <stdio.h>
int main()
{
int a[100],i,n,j,p;
printf("Enter number of elements:\n ");
scanf("%d",&n);
printf("Enter array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(a[i]>a[j])
{
p=a[j];
a[j]=a[i];
a[i]=p;
}
}
}
printf("The array looks :\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
return 0;
}
This works smooth except the smallest element is still the first :( When I
run it it has no errors, but when I enter numbers for ex. 1,2,3,4,5 and it
turns out 1 5 4 3 2.
No comments:
Post a Comment