-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd.c
More file actions
70 lines (60 loc) · 1.58 KB
/
d.c
File metadata and controls
70 lines (60 loc) · 1.58 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
// void reverse_array(int array[], int size); // same as below
void reverse_array(int* arr, int size);
void display_array(int array[], int size);
int main(void){
// first array to reverse
int myarray[] = {1,2,3,4,5,6,7,8,9};
int size = sizeof(myarray) / sizeof(myarray[0]);
// int size = 9;
reverse_array(myarray, size);
display_array(myarray, size);
// second array to reverse
int myarray2[] = {10,9,8,7,6,5,4,3,2,1};
int size2 = sizeof(myarray2) / sizeof(myarray2[0]);
reverse_array(myarray2, size2);
display_array(myarray2, size2);
return 0;
}
/*
Reverse an array function
*/
// void reverse_array(int arr[], int size)
// {
// int temp = 0;
// printf("middle element index: %d\n", size / 2);
// for (int i = 0; i < size / 2; i++)
// {
// temp = arr[i];
// arr[i] = arr[size - i - 1]; // left hand side
// arr[size - i - 1] = temp; // right hand side
// }
// }
/*
Reverse an array function using pointers inside function
*/
void reverse_array(int* arr, int size)
{
int* start = arr;
int* end = arr + size - 1;
while (start < end) {
// swap elements pointed to by start and end pointers
int temp = *start;
*start = *end;
*end = temp;
// Move the pointers towards the center break loop when *start == *end
start++;
end--;
}
}
/*
Display an array function
*/
void display_array(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("array[%d] = %d\n", i, arr[i]);
}
printf("\n");
}