-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1D-2D-3D-array
178 lines (141 loc) · 5.52 KB
/
1D-2D-3D-array
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
using namespace std;
int *Create1DArray(const int rows)//creating 1d array
// cout << " __Create1DArray start________________\n";
{
int *arr = new int [rows];
return arr;
}
// cout << " __Create1DArray end________________\n";
int ** Create2DArray ( const int rows, const int cols)//creating 2D array
// cout << " __Create2DArray start________________\n";
{ int **arr = new int *[rows];
for (int i = 0; i < rows; i++)
{
arr[i] = Create1DArray(cols);
}
return arr;
}
//cout << " __Create2DArray end________________\n";
int *** Create3DArray ( const int rows, const int cols, const int depth)//creating 3D array
// cout << " __Create3DArray start________________\n";
{ int ***arr = new int **[depth];
for (int i = 0; i < depth; i++)
{
arr[i] = Create2DArray(rows,cols);
}
return arr;
}
// cout << " __Create3DArray end________________\n";
void Fill1DArray (int *const arr,int const rows) // filling 1d array
// cout << " __Create3DArray start________________\n";
{
for (int i = 0; i < rows; i++)
{
arr[i] = i;
}
}
// cout << " __Create3DArray start________________\n";
void Fill2DArray(int** const arr,const int rows, const int cols) // filling 2d array
// cout << " __Fill2DArray start________________\n";
{
for (int i = 0; i < rows; i++)
{
Fill1DArray(arr[i],cols);
}
}
// cout << " ____Fill2DArray end________________\n";
void Fill3DArray(int*** const arr,const int rows, const int cols, const int depth) // filling 3d array
// cout << " __Fill3DArray start________________\n";
{
for (int i = 0; i < depth; i++)
{
Fill2DArray(arr[i],rows,cols);
}
}
// cout << " ____Fill3DArray end________________\n";
void Show1DArray (int* const arr, const int rows)// showing 1d array
// cout << " ____Show1DArray start________________\n";
{
for(int i = 0; i < rows; i++)
{
cout << arr[i]<<"\t";
}
cout<<endl;
}
// cout << " ____Show1DArray end________________\n";
void Show2DArray(int** const arr,const int rows, const int cols) // showing 2d array
// cout << " __Show2DArray start________________\n";
{
for (int i = 0; i < rows; i++)
{
Show1DArray(arr[i],cols);
}
cout<<endl;
}
// cout << " __Show2DArray end________________\n";
void Show3DArray(int*** const arr,const int rows, const int cols, const int depth) // showing 3d array
// cout << " __Show3DArray start________________\n";
{
for (int i = 0; i < depth; i++)
{
Show2DArray(arr[i],rows,cols);
}
cout<<endl;
}
// cout << " __Show2DArray end________________\n";
void Delete1DArray(int *arr)// delieting 1d array
// cout<<"___________delete1darray start________";
{
delete[] arr;
}
// cout<<"___________delete1darray end________";
void Delete2DArray(int **arr, const int rows)
// cout<<"___________delete2darray start______";
{
for (int i = 0; i <rows; i++)
{
Delete1DArray(arr[i]);
}
delete []arr;
}
// cout<<"___________delte2darray end_______";
void Delete3DArray(int ***arr, const int rows, const int cols)
// cout<<"___________delete2darray start______";
{
for (int i = 0; i <cols; i++)
{
Delete2DArray(arr[i],rows);
}
delete []arr;
}
// cout<<"___________delte2darray end_______";
int main()
{
int rows, cols,depth;
cout << "Please input rows:\t";
cin>>rows;
cout << "Please input cols:\t";
cin >> cols;
cout << "Please input depth:\t";
cin >> depth;
cout<<"*********************1d array*********************"<<endl;
int *arr1 = Create1DArray(rows);
Fill1DArray(arr1, rows);
Show1DArray(arr1,rows);
cout<<"*********************1d array*********************"<<endl;
cout<<"*********************2d array*********************"<<endl;
int **arr2 = Create2DArray(rows, cols);
Fill2DArray(arr2, rows, cols);
Show2DArray(arr2, rows, cols);
cout<<"*********************2d array*********************"<<endl;
cout<<"*********************3d array*********************"<<endl;
int ***arr3 = Create3DArray(rows, cols, depth);
Fill3DArray(arr3, rows, cols, depth);
Show3DArray(arr3, rows, cols, depth);
cout<<"*********************3d array*********************"<<endl;
Delete1DArray(arr1);
Delete2DArray(arr2,rows);
Delete3DArray(arr3, rows,cols);
return 0;
}