-
Notifications
You must be signed in to change notification settings - Fork 15
/
ex.3.62.cpp
62 lines (47 loc) · 1.29 KB
/
ex.3.62.cpp
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
// ex.3.62
// en: Write a version of Program 3.16 that handles three-dimensional
// arrays.
// ru: Напишите версию программы 3.16, обрабатывающую трехмерные
// массивы.
#include <iostream>
int*** Malloc3D(int x, int y, int z) {
int*** a = new int**[x];
for (int i = 0; i < x; ++i) {
a[i] = new int*[y];
for (int j = 0; j < y; ++j) {
a[i][j] = new int[z];
}
}
return a;
}
void Free3D(int*** a, int x, int y) {
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
delete[] a[i][j];
}
delete[] a[i];
}
delete[] a;
}
int usage(const char* bin) {
std::cout << "Usage: " << bin
<< " <positive int X> <positive int Y> <positive int Z>\n";
return 1;
}
int main(int argc, char* argv[]) {
if (argc < 4) {
return usage(argv[0]);
}
const int X = std::atoi(argv[1]);
const int Y = std::atoi(argv[2]);
const int Z = std::atoi(argv[3]);
if (X <= 0 || Y <= 0 || Z <= 0) {
return usage(argv[0]);
}
auto p = Malloc3D(X, Y, Z);
for (int k = 0; k < Z; ++k)
for (int j = 0; j < Y; ++j)
for (int i = 0; i < X; ++i) p[i][j][k] = 1;
Free3D(p, X, Y);
return 0;
}