#include <stdio.h>
void reverse_array(int arr[], int n) {
// Your logic here
int start=0, end = n-1, temp;
while(start<=end){
temp = arr[start];
arr[start] = arr[end];
arr[end]= temp;
start++;
end--;
}
}
int main() {
int n;
scanf("%d", &n);
int arr[100];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
reverse_array(arr, n);
for (int i = 0; i < n; i++) {
printf("%d", arr[i]);
if(i < n-1){
printf(" ");
}
}
return 0;
}
Initialise variables, start and end indicators
int start=0, end = n-1, temp;
2. Set the Looping condition. For even number of elements in the array, (start<end) for odd (start=end). Now combine together, since we dont know if the input array will have even number of elements or odd.
while(start<=end)
3. Assign the first element to a temporary variable - temp, then the now first element value is replaced by last value.(arr[end]). Then assign the last element with temp.
while(start<=end){
temp = arr[start];
arr[start] = arr[end];
arr[end]= temp;
start++;
end--;
}
4. Finally Increment the start, and decrement the end flags.
Input
5 1 2 3 4 5
Expected Output
5 4 3 2 1