#include <stdio.h>
int main() {
int N = 10;
int A[10];
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
for (int i = 0; i < 5 / 2; i++) {
int temp = A[i];
A[i] = A[4 - i];
A[4 - i] = temp;
}
for (int i = 5; i < 5 + 5; i++) {
int temp = A[i];
A[i] = A[9 - (i - 5)];
A[9 - (i - 5)] = temp;
}
for (int i = 0; i < N; i++){
printf("%d ", A[i]);
if(i<9)printf(" ");
}
return 0;
}
This code reads 10 integers into an array, then attempts to reverse two separate halves of the array: the first 5 elements and the last 5 elements. It then prints the result.
- The first reversal loop uses `5 / 2` which equals 2, so it only swaps the first two elements of the first half (indices 0 and 4, then 1 and 3), leaving the middle element (index 2) untouched. Check if you intended to reverse all 5 elements in the first half.
- The second reversal loop runs from `i = 5` to `i < 10`, but the index calculation `9 - (i - 5)` is correct. However, the loop runs 5 times, which means it swaps each pair twice, undoing the reversal. Think about how many iterations are needed to reverse a segment of 5 elements.