Help explain the output. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help explain the output.

#include<stdio.h> void main() { int i,n,a[100]; printf("-----------------------------------------\n"); printf("Input the number of elements to store in the array : "); scanf("%d", &n); printf("Input %d number of elements in the array :\n",n); for(i=0;i<n;i++) { printf("element - %d :", i); scanf("%d",&a[i]); } printf("\nThe values store into the araay are : \n"); for(i=0;i<n;i++) { printf("%5d",a[i]); } printf("\n\nThe values are : \n"); for(i=n-1;i>=0;i--) { printf("%5d",a[i]); } printf("\n\n"); }

2nd Dec 2022, 3:22 AM
clavel skie
clavel skie - avatar
3 Answers
+ 5
The last loop starts from index <n> - 1 for example, if <n> was 5, the loop begins from index 4 (5 - 1) The loop repeats while the loop counter <i> is greater than or equals to zero. So when <n> was 5, the loop will repeat in sequence 4, 3, 2, 1, 0 Basically the loop iterates the array in backwards and displays the value of element at index <i>
2nd Dec 2022, 4:18 AM
Ipang
+ 1
What exactly do you not understand?
2nd Dec 2022, 3:37 AM
Solo
Solo - avatar
0
Can you explain how the last for loop works?
2nd Dec 2022, 3:49 AM
clavel skie
clavel skie - avatar