-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task17.cpp
476 lines (406 loc) · 11.4 KB
/
Task17.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
In this assignment we are going to extend C++ by implementing a new way of creating arrays in C++.
This array like structure will be elastic in the sense that the programmer using it will be able to change
the size and dimensions of the array during program execution. Initially support will be provided for
up to 3 dimensions and hence programmer will be allowed to use an array either as a 1D, 2D or 3D
array as per need.
Programmer using these arrays will be allowed to use both non-negative and negative indices to access
the data stored in these arrays and the arrays will be safe in the sense that the programmer will be
unable to access an index that is not part of the array or in other words each integer will be mapped to
a valid index in the array.
To implement this notion we will need to keep a record of number of dimensions and size of each
dimension of the array along with the memory area reserved to store the array data. As discussed in
class, for this assignment we will keep this information in a structure/record defined using the struct
defining facility available in the language.
A proposed struct definition and some basic functions have been defined for your convenience and
your main responsibility is to complete this definition and provide definition of all necessary functions
needed to implement the notion of SAFE ELASTIC ARRAYS.
*/
#include <iostream>
using namespace std;
struct safeElasticArray
{
unsigned int arrayCapicity;
unsigned int arraySize;
unsigned int dimensions[3];
int* data;
void computeArraySize()
{
arraySize = dimensions[0] * dimensions[1] * dimensions[2];
}
bool allocateMemory()
{
data = new int[arrayCapicity];
for (unsigned int i = 0; i < arraySize; i++)
{
data[i] = 0;
}
return true;
}
void showArray(unsigned int D1 = 0, unsigned int D2 = 0, int D3 = 0)
{
int a = 0;
if (D1 != 0 && D2 == 0 && D3 == 0)
{
for (unsigned int i = 0; i < arraySize; i++)
cout << data[i] << " ";
}
if (D2 != 0 && D1 != 0 && D3 == 0)
{
for (unsigned int i = 0; i < D1; i++)
{
for (unsigned int j = 0; j < D2; j++)
{
cout << data[i * 2 + j] << " ";
}
cout << endl;
}
}
if (D2 != 0 && D1 != 0 && D3 != 0)
{
for (int i = 0; i < D3; i++)
{
for (int j = 0; j < D1; j++)
{
for (int k = 0; k < D2; k++)
{
cout << data[a] << " ";
a++;
}
cout << endl;
}
cout << endl;
}
}
}
void initArray(unsigned int D1 = 10, unsigned int D2 = 0, int D3 = 0)
{
if (D1 == 0)
D1 = 10;
dimensions[0] = D1;
if (D2 == 0) {
D2 = 1;
D3 = 1;
}
dimensions[1] = D2;
if (D3 == 0)
D3 = 1;
dimensions[2] = D3;
computeArraySize();
allocateMemory();
}
bool expandArray(unsigned int thisMuch, bool Erase = false)
{
if (thisMuch < 0)
{
return false;
}
else
{
int* exarr = nullptr;
exarr = new int[thisMuch];
for (int i = 0; i < arraySize; i++)
{
exarr[i] = data[i];
}
for (int i = 0; i < thisMuch - arraySize; i++)
{
exarr[arraySize + i] = 0;
}
delete[]data;
data = exarr;
}
return true;
}
bool shrinkArray(unsigned int thisMuch, bool Erase = false)
{
if (thisMuch < 0)
return false;
else
{
int* sharr = nullptr;
sharr = new int[thisMuch];
for (int i = 0; i < thisMuch; i++)
{
sharr[i] = data[i];
}
delete[]data;
data = sharr;
}
return true;
}
void fillArray()
{
for (unsigned int i = 0; i < arraySize; i++)
{
cout << "Data " << i << " : ";
cin >> data[i];
}
}
int safaeAccess(int index)
{
if (index >= arraySize)
return -1;
else
{
index %= arraySize;
return data[index];
}
}
int safaeAccess(int indexR, int indexC)
{
int a = 0, b = 0;
for (int i = 0; i <= indexR; i++)
{
for (int j = 0; j <= indexC; j++)
{
a = data[b];
b++;
}
}
return a;
}
int safaeAccess(int indexP, int indexR, int indexC)
{
int a = 0, b = 0;
for (int i = 0; i <= indexP; i++)
{
for (int j = 0; j <= indexR; j++)
{
for (int k = 0; k <= indexC; k++)
{
a = data[b];
b++;
}
}
}
return a;
}
bool resizeArray(double fraction)
{
if (arraySize * fraction <= arrayCapicity)
{
arraySize *= fraction;
return true;
}
else
return false;
}
void clearArray()
{
for (int i = 0; i < arraySize; i++)
{
data[i] = 0;
}
}
void deleteArray()
{
delete[]data;
}
int popBack()
{
int poarr;
poarr = data[arraySize - 1];
arraySize = arraySize - 1;
return poarr;
}
bool pushBack(int Value)
{
if ((arraySize + 1) <= arrayCapicity)
{
data[arraySize + 1] = Value;
return true;
}
else
return false;
}
bool reshape(int D1, int D2 = 0, int D3 = 0)
{
showArray(D1, D2, D3);
return true;
}
};
int main()
{
safeElasticArray Array;
cout << "Define the capacity of array : ";
cin >> Array.arrayCapicity;
Array.initArray();
Array.showArray();
cout << "Please press '1' for 1D array, '2' for 2D array, '3' for 3D array : ";
int d;
cin >> d;
if (d == 1)
{
cout << "Define the size of array: ";
int size;
cin >> size;
Array.initArray(size);
Array.fillArray();
Array.showArray(size);
}
if (d == 2)
{
int row, col;
cout << "Define the no. of rows : ";
cin >> row;
cout << "Define the no. of columns : ";
cin >> col;
Array.initArray(row, col);
Array.fillArray();
Array.showArray(row, col);
}
if (d == 3)
{
int row, col, page;
cout << "Define the no. of pages : ";
cin >> page;
cout << "Define the no. of rows : ";
cin >> row;
cout << "Define the no. of columns : ";
cin >> col;
Array.initArray(row, col, page);
Array.fillArray();
Array.showArray(row, col, page);
}
int ch;
cout << endl << "If you want to expand the array plz press 1 (0 for No) : ";
cin >> ch;
if (ch)
{
cout << "Enter the number by which you want to expand :";
int expand;
cin >> expand;
bool exp;
if (expand < 0)
exp = false;
else
exp = true;
if (!Array.expandArray(expand, exp))
cout << "Try Again" << endl;
else
cout << "Done" << endl;
}
cout << "If you want to shrink the array plz press 1 (0 for No) : ";
cin >> ch;
if (ch)
{
cout << "Enter the number by which you want to shrink :";
int shrink;
cin >> shrink;
bool shr;
if (shrink < 0)
shr = false;
else
shr = true;
if (!Array.shrinkArray(shrink, shr))
cout << "Try Again" << endl;
else
cout << "Done" << endl;
}
int check;
cout << "To access any element plz press 1 (0 for no ) : ";
cin >> check;
if (check)
{
if (d == 1)
{
cout << "Define the index : ";
int n;
cin >> n;
cout << Array.safaeAccess(n) << endl;
}
if (d == 2)
{
int row, col;
cout << "Define the no. of rows : ";
cin >> row;
cout << "Define the no. of columns : ";
cin >> col;
cout << Array.safaeAccess(row, col) << endl;
}
if (d == 3)
{
int row, col, page;
cout << "Define the no. of pages : ";
cin >> page;
cout << "Define the no. of rows : ";
cin >> row;
cout << "Define the no. of columns : ";
cin >> col;
cout << Array.safaeAccess(page, row, col) << endl;
}
}
cout << "To resize array plz press 1( 0 for no ) : ";
cin >> check;
if (check)
{
cout << "Enter the number : ";
double fraction;
cin >> fraction;
if (!Array.resizeArray(fraction))
{
cout << "Try Again " << endl;
}
}
cout << "To clear array plz press 1(0 for no ) : ";
cin >> check;
if (check)
Array.clearArray();
cout << "To delete array plz press 1( 0 for no ) : ";
cin >> check;
if (check)
Array.deleteArray();
cout << "To pushback array plz press 1( 0 for no ) : ";
cin >> check;
if (check)
{
cout << "enter number : ";
int value;
cin >> value;
if (!Array.pushBack(value))
cout << "Try Again ";
else
cout << "Successful" << endl;
}
cout << "To popback array plz press 1( 0 for no ) : ";
cin >> check;
if (check)
cout << Array.popBack() << endl;
cout << "To reshape array plz press 1(0 for no ) : ";
cin >> check;
if (check)
{
cout << "Please press '1' for 1D array, '2' for 2D array, '3' for 3D array : ";
cin >> d;
if (d == 1)
{
cout << "Define the size of array : ";
int size;
cin >> size;
Array.reshape(size);
}
if (d == 2)
{
int row, col;
cout << "Define the no. of rows: ";
cin >> row;
cout << "Define the no. of columns: ";
cin >> col;
Array.reshape(row, col);
}
if (d == 3)
{
int row, col, page;
cout << "Define the no. of pages: ";
cin >> page;
cout << "Define the no. of rows: ";
cin >> row;
cout << "Define the no. of columns: ";
cin >> col;
Array.reshape(row, col, page);
}
}
system("pause>nul");
return 0;
}