-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdynamic_memory-array-1.c
55 lines (47 loc) · 1.74 KB
/
dynamic_memory-array-1.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
# include<stdio.h>
# include<stdlib.h>
void array_sum();
void main()
{
array_sum();
}
void max_num(int *p, int size)
{
int max = 0;
for (int i = 0; i < size; i++)
{
if (max < (*(p+i)))
{
max = *(p+i);
}
}
(void)free(p);
printf("> Maximum number in array: %d", max);
}
void array_sum()
{
int *ptr;
int size, num;
int sum = 0;
printf("> Enter array size: ");
scanf("%d", &size);
ptr = (int*) calloc(size, 4);
for (int i = 0; i < size; i++)
{
printf(" > Enter number: ");
scanf("%d", ptr+i);
sum = sum + *(ptr+i);
}
printf("> Sum of array: %d\n", sum);
max_num(ptr, size);
}
/* Output
> Enter array size: 5
> Enter number: 23
> Enter number: 54
> Enter number: 1
> Enter number: 23
> Enter number: 78
* Sum of array: 179
* Maximum number in array: 78
*/