Somebody please help. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Somebody please help.

//c program to accept integer value and display the largest number using array.But it is not working properly.Sometimes infinite loop happens to scanf .if infinite loop does 't happen it can t display the correct largest interger.Help! #include<stdio.h> int main() { int a[10],i,la; for(i=0;i<=9 ;i++) { printf("Enter number %d\n",i); scanf("%d",&a[i]); } la=a[0]; for(i=0;i<=9;i++) { if(a[0]<a[i]) { la=a[i]; } } printf("The largest number is %d\n",la); return 0; }

29th May 2020, 8:12 AM
Coding San
Coding San - avatar
5 Answers
+ 3
I see no cause for an infinite loop to happen. It behaved okay when I tried the code. There is a logic error in the line that compares array values to determine which value is the largest. Since la is the variable where the largest value is held, the comparison should be with la. Change if(a[0]<a[i]) to if(la<a[i]) and that will fix the outcome.
29th May 2020, 9:30 AM
Brian
Brian - avatar
+ 2
Ok i am goona try this
29th May 2020, 9:53 AM
Coding San
Coding San - avatar
+ 2
Many thanks Brian,it works
29th May 2020, 10:07 AM
Coding San
Coding San - avatar
+ 1
Coding Gl, you are welcome!
29th May 2020, 10:19 AM
Brian
Brian - avatar
+ 1
๐Ÿ™‚๐Ÿ™‚๐Ÿ™‚โ™ฅ๏ธ
29th May 2020, 10:21 AM
Coding San
Coding San - avatar