-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseArray.c
49 lines (38 loc) · 943 Bytes
/
ReverseArray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* QUESTION: Reverse an array.*/
/*CODE*/
#include <stdio.h>
// Function to reverse an array
void reverseArray(int arr[], int size) {
int start = 0;
int end = size - 1;
int temp;
while (start < end) {
// Swap the elements
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
// Move the pointers
start++;
end--;
}
}
int main() {
int n, i;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
// Input the elements of the array
printf("Enter the elements of the array:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Reverse the array
reverseArray(arr, n);
// Print the reversed array
printf("Reversed array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}