-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
53 lines (40 loc) · 996 Bytes
/
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
#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
};
// Sum Function Declaration
int sumFunc(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]);
}
// Sum
int sum = sumFunc(&nums);
// Deallocation
free(nums.data);
nums.data = NULL;
// Print Sum
printf("%d\n", sum);
// Return 0
return EXIT_SUCCESS;
}
// Sum Function Definition
int sumFunc(struct list *nums) {
int sum = 0;
// Sum
for (int i = 0; i < nums->size; i++) {
sum = sum + nums->data[i];
}
// Return Sum
return sum;
}