-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_array.c
More file actions
31 lines (28 loc) · 777 Bytes
/
Copy pathreverse_array.c
File metadata and controls
31 lines (28 loc) · 777 Bytes
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
#include <stdio.h>
void print_arr(int a[], int n);
void reverse_array(int a[], int n);
// Define Function; which reverses the array
void reverse_array(int a[], int n) {
int tem_1;
for(int i = 0; i < n/2; i++) {
tem_1 = a[i]; // Define the temporary variable
a[i] = a[n-i-1]; // Always used this formula " n-i-1 " for reversing array
a[n-i-1] = tem_1;
}
}
// Define Function for print of Array
void print_arr(int a[], int n) {
printf("[ ");
for(int i = 0; i < n; i++) {
printf("%d \t", a[i]);
}
printf("]");
}
int main() {
int arr[] = {1, 2, 3, 4, 5};
// call the reverse_array function
reverse_array(arr, 5);
// call the print_arr function
print_arr(arr, 5);
return 0;
}