This repository has been archived by the owner on Dec 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharrays.cpp
695 lines (525 loc) · 40.3 KB
/
arrays.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/**
* file: arrays.cpp
* type: C++ (source file)
* date: 05_JULY_2023
* author: karbytes
* license: PUBLIC_DOMAIN
*/
/* preprocessing directives */
#include <iostream> // standard input (std::cin), standard output (std::cout)
#include <fstream> // file input, file output
#include <stdio.h> // NULL macro
#include <stdlib.h> // srand(), rand()
#include <time.h> // time()
#define MAXIMUM_S 1000 // constant which represents maximum value for S
#define MAXIMUM_T 1000 // constant which represents maximum value for T
/* function prototype */
void bubble_sort(int * A, int S);
/**
* Use the Bubble Sort algorithm to arrange the elements of an int type array, A,
* in ascending order
* such that A[0] represents the smallest integer value in that array and
* such that A[S - 1] represents the largest integer value in that array.
*
* Assume that S is a natural number no larger than MAXIMUM_S.
*
* Assume that A is a pointer to an int type variable and that
* A stores the memory address of the first element, A[0],
* of an int type array comprised of exactly S elements.
* (In other words, assume that exactly S consecutive int-sized
* chunks of memory are allocated to the array represented by A).
*
* Although this function returns no value,
* the array which the pointer variable, A, points to is updated
* if the elements of that array are not already arranged in ascending order.
*/
void bubble_sort(int * A, int S)
{
int i = 0, placeholder = 0;
bool array_is_sorted = false, adjacent_elements_were_swapped = false;
while (!array_is_sorted)
{
adjacent_elements_were_swapped = false;
for (i = 1; i < S; i += 1)
{
if (A[i] < A[i - 1])
{
placeholder = A[i];
A[i] = A[i - 1];
A[i - 1] = placeholder;
adjacent_elements_were_swapped = true;
}
}
if (!adjacent_elements_were_swapped) array_is_sorted = true;
}
}
/* program entry point */
int main()
{
// Declare four int type variables and set each of their initial values to 0.
int S = 0, T = 0, i = 0, k = 0;
// Declare two pointer-to-int type variables.
int * A, * B;
// Declare one pointer-to-pointer-to-char type variable.
char ** C;
// Declare a file output stream object.
std::ofstream file;
/**
* If the file named arrays_output.txt does not already exist
* inside of the same file directory as the file named arrays.cpp,
* create a new file named arrays_output.txt in that directory.
*
* Open the plain-text file named arrays_output.txt
* and set that file to be overwritten with program data.
*/
file.open("arrays_output.txt");
// Print an opening message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nStart Of Program";
std::cout << "\n--------------------------------";
// Print an opening message to the file output stream.
file << "--------------------------------";
file << "\nStart Of Program";
file << "\n--------------------------------";
// Print "The following statements describe the data capacities of various primitive C++ data types:" to the command line terminal.
std::cout << "\n\nThe following statements describe the data capacities of various primitive C++ data types:";
// Print "The following statements describe the data capacities of various primitive C++ data types:" to the file output stream.
file << "\n\nThe following statements describe the data capacities of various primitive C++ data types:";
// Print the data size of a bool type variable to the command line terminal.
std::cout << "\n\nsizeof(bool) = " << sizeof(bool) << ". // number of bytes which a bool type variable occupies";
// Print the data size of a bool type variable to the file output stream.
file << "\n\nsizeof(bool) = " << sizeof(bool) << ". // number of bytes which a bool type variable occupies";
// Print the data size of a char type variable to the command line terminal.
std::cout << "\n\nsizeof(char) = " << sizeof(char) << ". // number of bytes which a char type variable occupies";
// Print the data size of a char type variable to the file output stream.
file << "\n\nsizeof(char) = " << sizeof(char) << ". // number of bytes which a char type variable occupies";
// Print the data size of an int type variable to the command line terminal.
std::cout << "\n\nsizeof(int) = " << sizeof(int) << ". // number of bytes which an int type variable occupies";
// Print the data size of an int type variable to the file output stream.
file << "\n\nsizeof(int) = " << sizeof(int) << ". // number of bytes which an int type variable occupies";
// Print the data size of a long type variable to the command line terminal.
std::cout << "\n\nsizeof(long) = " << sizeof(long) << ". // number of bytes which a long type variable occupies";
// Print the data size of a long type variable to the file output stream.
file << "\n\nsizeof(long) = " << sizeof(long) << ". // number of bytes which a long type variable occupies";
// Print the data size of a float type variable to the command line terminal.
std::cout << "\n\nsizeof(float) = " << sizeof(float) << ". // number of bytes which a float type variable occupies";
// Print the data size of a float type variable to the file output stream.
file << "\n\nsizeof(float) = " << sizeof(float) << ". // number of bytes which a float type variable occupies";
// Print the data size of a double type variable to the command line terminal.
std::cout << "\n\nsizeof(double) = " << sizeof(double) << ". // number of bytes which a double type variable occupies";
// Print the data size of a doudle type variable to the file output stream.
file << "\n\nsizeof(double) = " << sizeof(double) << ". // number of bytes which a double type variable occupies";
// Print the data size of a pointer-to-bool type variable to the command line terminal.
std::cout << "\n\nsizeof(bool *) = " << sizeof(bool *) << ". // number of bytes which a pointer-to-bool type variable occupies";
// Print the data size of a pointer-to-bool type variable to the file output stream.
file << "\n\nsizeof(bool *) = " << sizeof(bool *) << ". // number of bytes which a pointer-to-bool type variable occupies";
// Print the data size of a pointer-to-char type variable to the command line terminal.
std::cout << "\n\nsizeof(char *) = " << sizeof(char *) << ". // number of bytes which a pointer-to-char type variable occupies";
// Print the data size of a pointer-to-char type variable to the file output stream.
file << "\n\nsizeof(char *) = " << sizeof(char *) << ". // number of bytes which a pointer-to-char type variable occupies";
// Print the data size of a pointer-to-int type variable to the command line terminal.
std::cout << "\n\nsizeof(int *) = " << sizeof(int *) << ". // number of bytes which a pointer-to-int type variable occupies";
// Print the data size of a pointer-to-int type variable to the file output stream.
file << "\n\nsizeof(int *) = " << sizeof(int *) << ". // number of bytes which a pointer-to-int type variable occupies";
// Print the data size of a pointer-to-long type variable to the command line terminal.
std::cout << "\n\nsizeof(long *) = " << sizeof(long *) << ". // number of bytes which a pointer-to-long type variable occupies";
// Print the data size of a pointer-to-long type variable to the file output stream.
file << "\n\nsizeof(long *) = " << sizeof(long *) << ". // number of bytes which a pointer-to-long type variable occupies";
// Print the data size of a pointer-to-float type variable to the command line terminal.
std::cout << "\n\nsizeof(float *) = " << sizeof(float *) << ". // number of bytes which a pointer-to-float type variable occupies";
// Print the data size of a pointer-to-float type variable to the file output stream.
file << "\n\nsizeof(float *) = " << sizeof(float *) << ". // number of bytes which a pointer-to-float type variable occupies";
// Print the data size of a pointer-to-double type variable to the command line terminal.
std::cout << "\n\nsizeof(double *) = " << sizeof(double *) << ". // number of bytes which a pointer-to-double type variable occupies";
// Print the data size of a pointer-to-double type variable to the file output stream.
file << "\n\nsizeof(double *) = " << sizeof(double *) << ". // number of bytes which a pointer-to-double type variable occupies";
// Print the data size of a pointer-to-pointer-to-bool type variable to the command line terminal.
std::cout << "\n\nsizeof(bool **) = " << sizeof(bool **) << ". // number of bytes which a pointer-to-pointer-to-bool type variable occupies";
// Print the data size of a pointer-to-pointer-to-bool type variable to the file output stream.
file << "\n\nsizeof(bool **) = " << sizeof(bool **) << ". // number of bytes which a pointer-to-pointer-to-bool type variable occupies";
// Print the data size of a pointer-to-pointer-to-char type variable to the command line terminal.
std::cout << "\n\nsizeof(char **) = " << sizeof(char **) << ". // number of bytes which a pointer-to-pointer-to-char type variable occupies";
// Print the data size of a pointer-to-pointer-to-char type variable to the file output stream.
file << "\n\nsizeof(char **) = " << sizeof(char **) << ". // number of bytes which a pointer-to-pointer-to-char type variable occupies";
// Print the data size of a pointer-to-pointer-to-int type variable to the command line terminal.
std::cout << "\n\nsizeof(int **) = " << sizeof(int **) << ". // number of bytes which a pointer-to-pointer-to-int type variable occupies";
// Print the data size of a pointer-to-pointer-to-int type variable to the file output stream.
file << "\n\nsizeof(int **) = " << sizeof(int **) << ". // number of bytes which a pointer-to-pointer-to-int type variable occupies";
// Print the data size of a pointer-to-pointer-to-long type variable to the command line terminal.
std::cout << "\n\nsizeof(long **) = " << sizeof(long **) << ". // number of bytes which a pointer-to-pointer-to-long type variable occupies";
// Print the data size of a pointer-to-pointer-to-long type variable to the file output stream.
file << "\n\nsizeof(long **) = " << sizeof(long **) << ". // number of bytes which a pointer-to-pointer-to-long type variable occupies";
// Print the data size of a pointer-to-pointer-to-float type variable to the command line terminal.
std::cout << "\n\nsizeof(float **) = " << sizeof(float **) << ". // number of bytes which a pointer-to-pointer-to-float type variable occupies";
// Print the data size of a pointer-to-pointer-to-float type variable to the file output stream.
file << "\n\nsizeof(float **) = " << sizeof(float **) << ". // number of bytes which a pointer-to-pointer-to-float type variable occupies";
// Print the data size of a pointer-to-pointer-to-double type variable to the command line terminal.
std::cout << "\n\nsizeof(double **) = " << sizeof(double **) << ". // number of bytes which a pointer-to-pointer-to-double type variable occupies";
// Print the data size of a pointer-to-pointer-to-double type variable to the file output stream.
file << "\n\nsizeof(double **) = " << sizeof(double **) << ". // number of bytes which a pointer-to-pointer-to-double type variable occupies";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_0: CREATE A DYNAMIC ARRAY WHICH IS NAMED A AND WHICH IS COMPRISED OF S INT TYPE VALUES." to the command line terminal.
std::cout << "\n\nSTEP_0: CREATE A DYNAMIC ARRAY WHICH IS NAMED A AND WHICH IS COMPRISED OF S INT TYPE VALUES.";
// Print "STEP_0: CREATE A DYNAMIC ARRAY WHICH IS NAMED A AND WHICH IS COMRPISED OF S INT TYPE VALUES." to the file output stream.
file << "\n\nSTEP_0: CREATE A DYNAMIC ARRAY WHICH IS NAMED A AND WHICH IS COMPRISED OF S INT TYPE VALUES.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "Enter a natural number, S, for representing the number of elements to include in an array which is no larger than than {MAXIMUM_S}: " to the command line terminal.
std::cout << "\n\nEnter a natural number, S, for representing the number of elements to include in an array which is no larger than " << MAXIMUM_S << ": ";
// Scan the command line terminal for the most recent keyboard input value.
std::cin >> S;
// Print "The value which was entered for S is {S}." to the command line terminal.
std::cout << "\nThe value which was entered for S is " << S << ".";
// Print "The value which was entered for S is {S}." to the file output stream.
file << "\n\nThe value which was entered for S is " << S << ".";
// If S is smaller than 1 or if S is larger than MAXIMUM_S, set S to 10.
S = ((S < 1) || (S > MAXIMUM_S)) ? 10 : S;
// Print "S := {S}. // number of consecutive int-sized chunks of memory to allocate to an array such that the memory address of the first element of that array, A[0], is stored in a pointer-to-int type variable named A" to the command line terminal.
std::cout << "\n\nS := " << S << ". // number of consecutive int-sized chunks of memory to allocate to an array such that the memory address of the first element of that array, A[0], is stored in a pointer-to-int type variable named A";
// Print "S := {S}. // number of consecutive int-sized chunks of memory to allocate to an array such that the memory address of the first element of that array, A[0], is stored in a pointer-to-int type variable named A" to the file output stream.
file << "\n\nS := " << S << ". // number of consecutive int-sized chunks of memory to allocate to an array such that the memory address of the first element of that array, A[0], is stored in a pointer-to-int type variable named A";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Allocate S contiguous int-sized chunks of memory and store the memory address of the first int-sized chunk of memory, A[0]. inside the pointer-to-int type variable named A.
A = new int [S];
// Print the program instruction used to generate the dynamic array represented by A to the command line terminal.
std::cout << "\n\n// Declare a pointer-to-int type variable named A.";
std::cout << "\nint * A;";
std::cout << "\n\n// Allocate S contiguous int-sized chunks of memory and store the memory address of the first int-sized chunk of memory, A[0], inside the pointer-to-int type variable named A.";
std::cout << "\nA = new int [S];";
// Print the program instruction used to generate the dynamic array represented by A to the file output stream.
file << "\n\n// Declare a pointer-to-int type variable named A.";
file << "\nint * A;";
file << "\n\n// Allocate S contiguous int-sized chunks of memory and store the memory address of the first int-sized chunk of memory, A[0], inside the pointer-to-int type variable named A.";
file << "\nA = new int [S];";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print the contents of A to the command line terminal.
std::cout << "\n\nA = " << A << ". // memory address of A[0]\n";
// Print the contents of A to the file output stream.
file << "\n\nA = " << A << ". // memory address of A[0]\n";
/**
* For each element, i, of the array represented by A,
* print the contents of the ith element of the array, A[i],
* and the memory address of that array element
* to the command line terminal and to the file output stream.
*/
for (i = 0; i < S; i += 1)
{
std::cout << "\nA[" << i << "] = " << A[i] << ". \t\t// &A[" << i << "] = " << &A[i] << ". (memory address of the first byte-sized memory cell comprising the block of 4 contiguous byte-sized memory cells allocated to A[" << i << "]).";
file << "\nA[" << i << "] = " << A[i] << ". \t\t// &A[" << i << "] = " << &A[i] << ". (memory address of the first byte-sized memory cell comprising the block of 4 contiguous byte-sized memory cells allocated to A[" << i << "]).";
}
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_1: RANDOMLY ASSIGN ONE OF THE FIRST T RANDOM NONNEGATIVE INTEGERS TO EACH ELEMENT OF THE ARRAY NAMED A." to the command line terminal.
std::cout << "\n\nSTEP_1: RANDOMLY ASSIGN ONE OF THE FIRST T RANDOM NONNEGATIVE INTEGERS TO EACH ELEMENT OF THE ARRAY NAMED A.";
// Print "STEP_1: RANDOMLY ASSIGN ONE OF THE FIRST T RANDOM NONNEGATIVE INTEGERS TO EACH ELEMENT OF THE ARRAY NAMED A." to the file output stream.
file << "\n\nSTEP_1: RANDOMLY ASSIGN ONE OF THE FIRST T RANDOM NONNEGATIVE INTEGERS TO EACH ELEMENT OF THE ARRAY NAMED A.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "Enter a natural number, T, for representing the number of unique states which each element of the array can store exactly one of which is no larger than {MAXIMUM_T}: " to the command line terminal.
std::cout << "\n\nEnter a natural number, T, for representing the number of unique states which each element of the array can store exactly one of which is no larger than " << MAXIMUM_T << ": ";
// Scan the command line terminal for the most recent keyboard input value.
std::cin >> T;
// Print "The value which was entered for T is {T}." to the command line terminal.
std::cout << "\nThe value which was entered for T is " << T << ".";
// Print "The value which was entered for T is {T}." to the file output stream.
file << "\n\nThe value which was entered for T is " << T << ".";
// If T is smaller than 1 or if T is larger than MAXIMUM_T, set T to 100.
T = ((T < 1) || (T > MAXIMUM_T)) ? 100 : T;
// Print "T := {T}. // number of unique states which each element of array A can represent" to the command line terminal.
std::cout << "\n\nT := " << T << ". // number of unique states which each element of array A can represent";
// Print "T := {T}. // number of unique states which each element of array A can represent" to the file output stream.
file << "\n\nT := " << T << ". // number of unique states which each element of array A can represent";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Seed the pseudo random number generator with the integer number of seconds which have elapsed since the Unix Epoch (i.e. midnight of 01_JANUARY_1970).
srand(time(NULL));
// Print the command to seed the pseudo random number generator to the command line.
std::cout << "\n\n// Seed the pseudo random number generator with the integer number of seconds which have elapsed since the Unix Epoch (i.e. midnight of 01_JANUARY_1970).";
std::cout << "\nsrand(time(NULL));";
// Print the command to seed the pseudo random number generator to the file output stream.
file << "\n\n// Seed the pseudo random number generator with the integer number of seconds which have elapsed since the Unix Epoch (i.e. midnight of 01_JANUARY_1970).";
file << "\nsrand(time(NULL));";
// For each element, A[i], of the array named A, set A[i] to a randomly generated integer which is no smaller than 0 and no larger than (T - 1).
for (i = 0; i < S; i += 1) A[i] = rand() % T;
// Print the command to populate each element of the array named A with a randomly generated integer which is no smaller than 0 and no larger than (T - 1) to the command line terminal.
std::cout << "\n\n// For each element, A[i], of the array named A, set A[i] to a randomly generated integer which is no smaller than 0 and no larger than (T - 1).";
std::cout << "\nfor (i = 0; i < S; i += 1) A[i] = rand() % T;";
// Print the command to populate each element of the array named A with a randomly generated integer which is no smaller than 0 and no larger than (T - 1) to the file output stream.
file << "\n\n// For each element, A[i], of the array named A, set A[i] to a randomly generated integer which is no smaller than 0 and no larger than (T - 1).";
file << "\nfor (i = 0; i < S; i += 1) A[i] = rand() % T;";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print the contents of A to the command line terminal.
std::cout << "\n\nA = " << A << ". // memory address of A[0]\n";
// Print the contents of A to the file output stream.
file << "\n\nA = " << A << ". // memory address of A[0]\n";
/**
* For each element, i, of the array represented by A,
* print the contents of the ith element of the array, A[i],
* and the memory address of that array element
* to the command line terminal and to the file output stream.
*/
for (i = 0; i < S; i += 1)
{
std::cout << "\nA[" << i << "] = " << A[i] << ". \t\t// &A[" << i << "] = " << &A[i] << ". (memory address of the first memory cell comprising the block of 4 contiguous memory cells allocated to A[" << i << "]).";
file << "\nA[" << i << "] = " << A[i] << ". \t\t// &A[" << i << "] = " << &A[i] << ". (memory address of the first memory cell comprising the block of 4 contiguous memory cells allocated to A[" << i << "]).";
}
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_2: SORT THE ELEMENT VALUES OF THE ARRAY NAMED A TO BE IN ASCENDING ORDER." to the command line terminal.
std::cout << "\n\nSTEP_2: SORT THE ELEMENT VALUES OF THE ARRAY NAMED A TO BE IN ASCENDING ORDER.";
// Print "STEP_2: SORT THE ELEMENT VALUES OF THE ARRAY NAMED A TO BE IN ASCENDING ORDER." to the file output stream.
file << "\n\nSTEP_2: SORT THE ELEMENT VALUES OF THE ARRAY NAMED A TO BE IN ASCENDING ORDER.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Sort the integer values stored in array A to be in ascending order using the Bubble Sort algorithm.
bubble_sort(A, S);
// Print the command to sort the integer values stored in array A in ascending order to the command line.
std::cout << "\n\n// Sort the integer values stored in array A to be in ascending order using the Bubble Sort algorithm.";
std::cout << "\nbubble_sort(A, S);";
// Print the command to sort the integer values stored in array A in ascending order to the file output stream.
file << "\n\n// Sort the integer values stored in array A to be in ascending order using the Bubble Sort algorithm.";
file << "\nbubble_sort(A, S);";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print the contents of A to the command line terminal.
std::cout << "\n\nA = " << A << ". // memory address of A[0]\n";
// Print the contents of A to the file output stream.
file << "\n\nA = " << A << ". // memory address of A[0]\n";
/**
* For each element, i, of the array represented by A,
* print the contents of the ith element of the array, A[i],
* and the memory address of that array element
* to the command line terminal and to the file output stream.
*/
for (i = 0; i < S; i += 1)
{
std::cout << "\nA[" << i << "] = " << A[i] << ".\t\t// &A[" << i << "] = " << &A[i] << ". (memory address of the first memory cell comprising the block of 4 contiguous memory cells allocated to A[" << i << "]).";
file << "\nA[" << i << "] = " << A[i] << ".\t\t// &A[" << i << "] = " << &A[i] << ". (memory address of the first memory cell comprising the block of 4 contiguous memory cells allocated to A[" << i << "]).";
}
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_3: CREATE A DYNAMIC ARRAY WHICH IS NAMED B AND WHICH IS COMPRISED OF T INT TYPE VALUES." to the command line terminal.
std::cout << "\n\nSTEP_3: CREATE A DYNAMIC ARRAY WHICH IS NAMED B AND WHICH IS COMPRISED OF T INT TYPE VALUES.";
// Print "STEP_3: CREATE A DYNAMIC ARRAY WHICH IS NAMED B AND WHICH IS COMPRISED OF T INT TYPE VALUES." to the file output stream.
file << "\n\nSTEP_3: CREATE A DYNAMIC ARRAY WHICH IS NAMED B AND WHICH IS COMPRISED OF T INT TYPE VALUES.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Allocate T contiguous int-sized chunks of memory and store the memory address of the first int-sized chunk of memory, B[0]. inside the pointer-to-int type variable named B.
B = new int [T];
// Print the program instruction used to generate the dynamic array represented by B to the command line terminal.
std::cout << "\n\n// Declare a pointer-to-int type variable named B.";
std::cout << "\nint * B;";
std::cout << "\n\n// Allocate T contiguous int-sized chunks of memory and store the memory address of the first int-sized chunk of memory, B[0], inside the pointer-to-int type variable named B.";
std::cout << "\nB = new int [T];";
// Print the program instruction used to generate the dynamic array represented by B to the file output stream.
file << "\n\n// Declare a pointer-to-int type variable named B.";
file << "\nint * B;";
file << "\n\n// Allocate T contiguous int-sized chunks of memory and store the memory address of the first int-sized chunk of memory, B[0], inside the pointer-to-int type variable named B.";
file << "\nB = new int [T];";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print the contents of B to the command line terminal.
std::cout << "\n\nB = " << B << ". // memory address of B[0]\n";
// Print the contents of B to the file output stream.
file << "\n\nB = " << B << ". // memory address of B[0]\n";
/**
* For each element, i, of the array represented by B,
* print the contents of the ith element of the array, B[i],
* and the memory address of that array element
* to the command line terminal and to the file output stream.
*/
for (i = 0; i < T; i += 1)
{
std::cout << "\nB[" << i << "] = " << B[i] << ".\t\t// &B[" << i << "] = " << &B[i] << ". (memory address of the first byte-sized memory cell comprising the block of 4 contiguous byte-sized memory cells allocated to B[" << i << "]).";
file << "\nB[" << i << "] = " << B[i] << ".\t\t// &B[" << i << "] = " << &B[i] << ". (memory address of the first byte-sized memory cell comprising the block of 4 contiguous byte-sized memory cells allocated to B[" << i << "]).";
}
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_4: FOR EACH ELEMENT B[i] OF THE ARRAY NAMED B, STORE THE NUMBER OF TIMES i APPEARS AS AN ELEMENT VALUE IN THE ARRAY NAMED A." to the command line terminal.
std::cout << "\n\nSTEP_4: FOR EACH ELEMENT B[i] OF THE ARRAY NAMED B, STORE THE NUMBER OF TIMES i APPEARS AS AN ELEMENT VALUE IN THE ARRAY NAMED A.";
// Print "STEP_4: FOR EACH ELEMENT B[i] OF THE ARRAY NAMED B, STORE THE NUMBER OF TIMES i APPEARS AS AN ELEMENT VALUE IN THE ARRAY NAMED A." to the file output stream.
file << "\n\nSTEP_4: FOR EACH ELEMENT B[i] OF THE ARRAY NAMED B, STORE THE NUMBER OF TIMES i APPEARS AS AN ELEMENT VALUE IN THE ARRAY NAMED A.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
/**
* For each element, i, of the array represented by B,
* store the number of times i appears as an element value in the array represented by A
* in B[i].
*/
for (i = 0; i < T; i += 1)
{
for (k = 0; k < S; k += 1)
{
if (i == A[k]) B[i] += 1;
}
}
// Print the contents of B to the command line terminal.
std::cout << "\n\nB = " << B << ". // memory address of B[0]\n";
// Print the contents of B to the file output stream.
file << "\n\nB = " << B << ". // memory address of B[0]\n";
/**
* For each element, i, of the array represented by B,
* print the contents of the ith element of the array, B[i],
* and the memory address of that array element
* to the command line terminal and to the file output stream.
*/
for (i = 0; i < T; i += 1)
{
std::cout << "\nB[" << i << "] = " << B[i] << ".\t\t// &B[" << i << "] = " << &B[i] << ". (memory address of the first byte-sized memory cell comprising the block of 4 contiguous byte-sized memory cells allocated to B[" << i << "]).";
file << "\nB[" << i << "] = " << B[i] << ".\t\t// &B[" << i << "] = " << &B[i] << ". (memory address of the first byte-sized memory cell comprising the block of 4 contiguous byte-sized memory cells allocated to B[" << i << "]).";
}
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_5: CREATE A DYNAMIC ARRAY WHICH IS NAMED C AND WHICH IS COMPRISED OF T POINTER-TO-CHAR TYPE VALUES." to the command line terminal.
std::cout << "\n\nSTEP_5: CREATE A DYNAMIC ARRAY WHICH IS NAMED C AND WHICH IS COMPRISED OF T POINTER-TO-CHAR TYPE VALUES.";
// Print "STEP_5: CREATE A DYNAMIC ARRAY WHICH IS NAMED C AND WHICH IS COMPRISED OF T POINTER-TO-CHAR TYPE VALUES." to the file output stream.
file << "\n\nSTEP_5: CREATE A DYNAMIC ARRAY WHICH IS NAMED C AND WHICH IS COMPRISED OF T POINTER-TO-CHAR TYPE VALUES.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Allocate T contiguous pointer-to-char-sized chunks of memory and store the memory address of the first pointer-to-char-sized chunk of memory, C[0], inside the pointer-to-pointer-to-char type variable named C.
C = new char * [T];
// C is a two-dimensional array which depicts a histogram (i.e. bar graph) such the length of the ith row is identical to the value stored in B[i].
for (i = 0; i < T; i += 1)
{
C[i] = new char [B[i]];
for (k = 0; k < B[i]; k += 1) C[i][k] = 'X';
}
// Print the program instruction used to generate the dynamic array represented by C to the command line terminal.
std::cout << "\n\n// Declare one pointer-to-pointer-to-char type variable.";
std::cout << "\nchar ** C;";
std::cout << "\n\n// Allocate T contiguous pointer-to-char-sized chunks of memory and store the memory address of the first pointer-to-char-sized chunk of memory, C[0], inside the pointer-to-pointer-to-char type variable named C.";
std::cout << "\nC = new char * [T];";
std::cout << "\n\n// C is a two-dimensional array which depicts a histogram (i.e. bar graph) such the length of the ith row is identical to the value stored in B[i].";
std::cout << "\nfor (i = 0; i < T; i += 1)";
std::cout << "\n{";
std::cout << "\n C[i] = new char [B[i]];";
std::cout << "\n for (k = 0; k < B[i]; k += 1) C[i][k] = 'X';";
std::cout << "\n}";
// Print the program instruction used to generate the dynamic array represented by C to the file output stream.
file << "\n\n// Declare one pointer-to-pointer-to-char type variable.";
file << "\nchar ** C;";
file << "\n\n// Allocate T contiguous pointer-to-char-sized chunks of memory and store the memory address of the first pointer-to-char-sized chunk of memory, C[0], inside the pointer-to-pointer-to-char type variable named C.";
file << "\nC = new char * [T];";
file << "\n\n// C is a two-dimensional array which depicts a histogram (i.e. bar graph) such the length of the ith row is identical to the value stored in B[i].";
file << "\nfor (i = 0; i < T; i += 1)";
file << "\n{";
file << "\n C[i] = new char [B[i]];";
file << "\n for (k = 0; k < B[i]; k += 1) C[i][k] = 'X';";
file << "\n}";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print the contents of C to the command line terminal.
std::cout << "\n\nC = " << C << ". // memory address of C[0]\n";
// Print the contents of C to the file output stream.
file << "\n\nC = " << C << ". // memory address of C[0]\n";
/**
* For each element, i, of the array represented by C,
* print the contents of the ith element of the array, C[i],
* and the memory address of that array element
* to the command line terminal and to the file output stream.
*/
for (i = 0; i < T; i += 1)
{
std::cout << "\nC[" << i << "] = " << C[i] << ".\t\t// &C[" << i << "] = " << &C[i] << ". (memory address of the first byte-sized memory cell comprising the block of 8 contiguous byte-sized memory cells allocated to C[" << i << "]).";
file << "\nC[" << i << "] = " << C[i] << ".\t\t// &C[" << i << "] = " << &C[i] << ". (memory address of the first byte-sized memory cell comprising the block of 8 contiguous byte-sized memory cells allocated to C[" << i << "]).";
}
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
// Print "STEP_6: RELEASE MEMORY WHICH WAS ALLOCATED TO THE DYNAMIC ARRAYS NAMED A, B, AND C." to the command line terminal.
std::cout << "\n\nSTEP_6: RELEASE MEMORY WHICH WAS ALLOCATED TO THE DYNAMIC ARRAYS NAMED A, B, AND C.";
// Print "STEP_6: RELEASE MEMORY WHICH WAS ALLOCATED TO THE DYNAMIC ARRAYS NAMED A, B, AND C." to the file output stream.
file << "\n\nSTEP_6: RELEASE MEMORY WHICH WAS ALLOCATED TO THE DYNAMIC ARRAYS NAMED A, B, AND C.";
// Print a horizontal line to the command line terminal.
std::cout << "\n\n--------------------------------";
// Print a horizontal line to the command line terminal.
file << "\n\n--------------------------------";
/**
* Note that, unlike a static array, a dynamic array is instantiated during program runtime instead of during program compile time.
* (A static array is assigned memory during program compilation while a dynamic array is assigned memory during program runtime).
* At compile time, the computer does not know how much memory space to allocate to a dynamic array because the number of elements
* in that array may vary and is not specified in the program source code.
*/
// De-allocate memory which was assigned to the dynamically-allocated array of S int type values
delete [] A;
// Print the command to de-allocate memory which was assigned to the dynamically-allocated array of S int type values to the command line terminal.
std::cout << "\n\n// De-allocate memory which was assigned to the dynamically-allocated array of S int type values.";
std::cout << "\ndelete [] A; // Free up S contiguous int-sized chunks of memory which were assigned to the dynamic array named A.";
// Print the command to de-allocate memory which was assigned to the dynamically-allocated array of S int type values to the file output stream.
file << "\n\n// De-allocate memory which was assigned to the dynamically-allocated array of S int type values.";
file << "\ndelete [] A; // Free up S contiguous int-sized chunks of memory which were assigned to the dynamic array named A.";
// De-allocate memory which was assigned to the dynamically-allocated array of T int type values.
delete [] B;
// Print the command to de-allocate memory which was assigned to the dynamically-allocated array of T int type values to the command line terminal.
std::cout << "\n\nDe-allocate memory which was assigned to the dynamically-allocated array of T int type values.";
std::cout << "\ndelete [] B; // Free up T contiguous int-sized chunks of memory which were assigned to the dynamic array named B.";
// Print the command to de-allocate memory which was assigned to the dynamically-allocated array of T int type values to the file output stream.
file << "\n\nDe-allocate memory which was assigned to the dynamically-allocated array of T int type values.";
file << "\ndelete [] B; // Free up T contiguous int-sized chunks of memory which were assigned to the dynamic array named B.";
// De-allocate memory which was assigned to the dynamically-allocated array of T pointer-to-char type values.
for (i = 0; i < T; i += 1) delete [] C[i];
delete [] C;
// Print the command to de-allocate memory which was assigned to the dynamically-allocated array of T pointer-to-char type values to the command line terminal.
std::cout << "\n\n// De-allocate memory which was assigned to the dynamically-allocated array of T pointer-to-char type values.";
std::cout << "\nfor (i = 0; i < T; i += 1) delete [] C[i]; // Free up B[i] char-sized chunks of memory which were assigned to the dynamic array named C[i].";
std::cout << "\ndelete [] C; // Free up T contiguous pointer-to-char-sized chunks of memory which were assigned to the dynamic array named C.";
// Print the command to de-allocate memory which was assigned to the dynamically-allocated array of T pointer-to-char type values to the file output stream.
file << "\n\n// De-allocate memory which was assigned to the dynamically-allocated array of T pointer-to-char type values.";
file << "\nfor (i = 0; i < T; i += 1) delete [] C[i]; // Free up B[i] char-sized chunks of memory which were assigned to the dynamic array named C[i].";
file << "\ndelete [] C; // Free up T contiguous pointer-to-char-sized chunks of memory which were assigned to the dynamic array named C.";
// Print a closing message to the command line terminal.
std::cout << "\n\n--------------------------------";
std::cout << "\nEnd Of Program";
std::cout << "\n--------------------------------\n\n";
// Print a closing message to the file output stream.
file << "\n\n--------------------------------";
file << "\nEnd Of Program";
file << "\n--------------------------------";
// Close the file output stream.
file.close();
// Exit the program.
return 0;
}