1.Reverse an array and find its maximum
BeginnerRead 5 integers into an array, print the largest one and its position (index), then print the array in reverse order.
Show hint
Track the index of the largest element, not just its value. Print in reverse with for (int i = 4; i >= 0; i--).
Show solution
#include <stdio.h>
int main(void) {
int a[5];
for (int i = 0; i < 5; i++) {
if (scanf("%d", &a[i]) != 1) return 1;
}
int best = 0;
for (int i = 1; i < 5; i++) {
if (a[i] > a[best]) best = i;
}
printf("max %d at index %d\n", a[best], best);
for (int i = 4; i >= 0; i--) printf("%d ", a[i]);
printf("\n");
return 0;
}12
45
7
45
30
max 45 at index 1
30 45 7 45 12