0

What's the error in ascending order program

#include<stdio.h> #include<conio.h> void main() { int a[100],i,j,n,k,b,l; printf("enter the no of elements"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter the %d element",i+1); scanf("%d",&a[i]); } for(k=0;k>n;k++) { for(j=k+1;j<n;j++) { if(a[k]>a[j]) { b=a[k]; a[k]=a[j]; a[j]=b; } } } for(l=0;l<n;l++) printf("%d\n",a[l]); getch(); }

10th Feb 2020, 4:42 PM
Ragul P
Ragul P - avatar
7 ответов
+ 1
On quick glance: "k>n" is probably incorrect. Also, void main should really be int main even if some compilers accept the former.
10th Feb 2020, 4:46 PM
Dennis
Dennis - avatar
+ 1
Thanks Dennis sir...your guess is correct...I got the right answer
10th Feb 2020, 5:06 PM
Ragul P
Ragul P - avatar
0
...
10th Feb 2020, 4:43 PM
Ragul P
Ragul P - avatar
0
Logic is correct but if loop cannot work... The input value is equal to output...for eg if we give the input. 4,6,7,3,2 the output is also same..
10th Feb 2020, 4:49 PM
Ragul P
Ragul P - avatar
0
Yes, you're using the wrong compare operator, is what I was trying to hint at.
10th Feb 2020, 4:55 PM
Dennis
Dennis - avatar
0
What's the compare operate..pls explain clearly ...in which part I did a mistake
10th Feb 2020, 4:57 PM
Ragul P
Ragul P - avatar
0
"for(k=0;k>n;k++)" the '>' is incorrect. Currently you're only entering the loop if k is bigger than n which will ( almost ) never be true. You have to check if k is smaller than n. Surely you know which operator to use, you used it in the same program.
10th Feb 2020, 5:01 PM
Dennis
Dennis - avatar