-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicmemory.c
More file actions
41 lines (26 loc) · 802 Bytes
/
Dynamicmemory.c
File metadata and controls
41 lines (26 loc) · 802 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
/*Problem 1:
Write a function that allocates memory for an array of integers of the specified size.
The function should return a pointer to the allocated memory.*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int size;
printf("Enter teh size of the array : \n");
scanf("%d",&size);
int *arrayptr = (int*)malloc(size*sizeof(int));
printf("\nEnter %d elements:", size);
for (int i = 0; i < size; i++){
printf("\nEnetr the %d index valaue : ",i);
scanf("%d", &arrayptr[i]);
}
for (int i = 0; i < size ; i++){
printf("\nArray element of array[%d] is : %d ", i , arrayptr[i]);
}
int sum = 0;
for (int i = 0; i < size ; i++){
sum += arrayptr[i];
}
printf("\nSum of elements: %d\n", sum);
free(arrayptr);
return 0;
}