-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
58 lines (46 loc) · 1.26 KB
/
main.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
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
// Struct
struct list {
int *data; // Points to the memory where the list items are stored
int size; // Indicates how many items fit in the allocated memory
// int numItems; // Indicates how many items are currently in the list
};
// Reverse Array Function Declaration
void reverseArray(struct list *nums, int start, int end);
// Print Array Function Declaration
void printArray(struct list *nums);
int main() {
struct list nums;
// User Input - Number of Numbers
scanf("%d", &nums.size);
// Dynamic Memory (HEAP) Allocation
nums.data = malloc(nums.size * sizeof(int));
// User Input - Numbers
for (int i = 0; i < nums.size; i++) {
scanf("%d", &nums.data[i]);
}
// Reverse Array
reverseArray(&nums, 0, nums.size - 1);
// Print Array
printArray(&nums);
// Return 0
return EXIT_SUCCESS;
}
// Reverse Array Function Definition
void reverseArray(struct list *nums, int start, int end) {
int tmp;
while (start < end) {
tmp = nums->data[start];
nums->data[start] = nums->data[end];
nums->data[end] = tmp;
start++;
end--;
}
}
// Print Array Function Definition
void printArray(struct list *nums) {
for (int i = 0; i < nums->size; i++) {
printf("%d ", nums->data[i]);
}
}