forked from sabir9136/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayelm.c
More file actions
64 lines (32 loc) · 989 Bytes
/
arrayelm.c
File metadata and controls
64 lines (32 loc) · 989 Bytes
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
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* ptr; //declaration of integer pointer
int limit; //to store array limit
int i; //loop counter
int sum; //to store sum of all elements
printf("Enter limit of the array: ");
scanf("%d", &limit);
//declare memory dynamically
ptr = (int*)malloc(limit * sizeof(int));
//read array elements
for (i = 0; i < limit; i++) {
printf("Enter element %02d: ", i + 1);
scanf("%d", (ptr + i));
}
//print array elements
printf("\nEntered array elements are:\n");
for (i = 0; i < limit; i++) {
printf("%d\n", *(ptr + i));
}
//calculate sum of all elements
sum = 0; //assign 0 to replace garbage value
for (i = 0; i < limit; i++) {
sum += *(ptr + i);
}
printf("Sum of array elements is: %d\n", sum);
//free memory
free(ptr); //hey, don't forget to free dynamically allocated memory.
return 0;
}