-
Notifications
You must be signed in to change notification settings - Fork 0
/
1dArray.c
44 lines (34 loc) · 944 Bytes
/
1dArray.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
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define ARRAY_MAX 100
/*
input
6
16 13 7 2 1 12
output
51
*/
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
// initialize them, otherwise you gonna get garbage
int n = 0, a = 0, sum = 0;
scanf("%d",&n);
// dinamically alocate the array with the size of &n
int *arr = (int*)malloc(n * sizeof(int));
// while a(number inserted) is less than n(array size)
// read from STDIN and append a(number inserted) into array
// then gets its value and sums with the previous number
// and then increment the array size
while (a < n) {
scanf("%d", &arr[a]);
sum += arr[a];
a++;
}
// dealocate array memory
free(arr);
// prints the sum
printf("%d", sum);
return 0;
}