-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion16.c
51 lines (35 loc) · 845 Bytes
/
question16.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
/*
2. Given an array of integers, reverse the original array using a function "void reverse(int arr[])".
For swapping two elements use another function "void swap (int *aptr, int *bptr)". Finally,
print the reverse of the array from the "main()" function.
The array might be hard-coded in your "main()" function.
input: none
output: the reversed array.
*/
#include <stdio.h>
void swap(int *aptr, int *bptr){
*aptr = *aptr + *bptr;
*bptr = *aptr - *bptr;
*aptr = *aptr - *bptr;
}
void reverse(int arr[], int b){
int i,j,size = b;
j = size - 1;
i = 0;
while(1){
swap(&arr[i], &arr[j]);
i++;
j--;
if(j==i || j<i){
break;
}
}
}
int main(){
int a[] = {1,2,3,4,5,6,7,8};
int size = sizeof(a)/sizeof(a[0]);
reverse(a, size);
for(int i = 0; i < size; i++){
printf("%d\n", a[i]);
}
}