-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynarr.c
More file actions
52 lines (37 loc) · 944 Bytes
/
dynarr.c
File metadata and controls
52 lines (37 loc) · 944 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
// Dynamically allocated array
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
double value;
int max;
double * ptd;
printf("Enter the maximum number of double entried:");
// Checks for non integer and 0 values.
while((scanf("%d", &max) != 1) || max == 0)
{
printf("The number was not entered correctly\n");
exit(EXIT_FAILURE);
}
ptd = (double *) malloc(max * sizeof(double));
if(ptd == NULL)
{
printf("Memory allocation failed!");
exit(EXIT_FAILURE);
}
// Dynamically store the values into an array from the user.
printf("Enter the values to be stored:\n");
int i = 0;
while((i < max) && scanf("%lf", &ptd[i]) == 1)
{
i++;
}
// Display the array stored
printf("Here are the values entered\n");
for(int j = 0; j < max; j++)
{
printf("%lf\n", ptd[j]);
}
free(ptd);
return 0;
}