-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.c
81 lines (54 loc) · 1.79 KB
/
array.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
int main() {
int first,second,third,fourth;
printf("Enter dimensions(first, second, third, fourth):\n");
scanf("%d %d %d %d", &first, &second, &third, &fourth);
printf("your dimenssion is arr[%d][%d][%d][%d]\n",first,second,third,fourth);
// allocate the 4D array
int ****arr = (int ****)malloc(first* sizeof(int ***));
for (int i = 0; i < first; i++) {
arr[i] = (int ***)malloc(second * sizeof(int **));
for (int j = 0; j < second; j++) {
arr[i][j] = (int **)malloc(third * sizeof(int *));
for (int k = 0; k < third; k++) {
arr[i][j][k] = (int *)malloc(fourth * sizeof(int));
}
}
}
// Accept elements from the user
printf("Enter the elements of the 4D array:\n");
for (int i = 0; i < first; i++) {
for (int j = 0; j < second; j++) {
for (int k = 0; k < third; k++) {
for (int l = 0; l < fourth; l++) {
scanf("%d", &arr[i][j][k][l]);
}
}
}
}
// Display the elements
printf("The elements of the 4D array are:\n");
for (int i = 0; i < first; i++) {
for (int j = 0; j < second; j++) {
for (int k = 0; k < third; k++) {
for (int l = 0; l < fourth; l++) {
printf("arr[%d][%d][%d][%d]=%d\n",i,j,k,l, arr[i][j][k][l]);
}
printf("\n");
}
}
}
// Free allocated memory
for (int i = 0; i < first; i++) {
for (int j = 0; j < second; j++) {
for (int k = 0; k < third; k++) {
free(arr[i][j][k]);
}
free(arr[i][j]);
}
free(arr[i]);
}
free(arr);
return 0;
}