-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrays.hpp
353 lines (283 loc) · 9.55 KB
/
arrays.hpp
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
#ifndef __ARRAYS_H
#define __ARRAYS_H
#include <iostream>
#include <iomanip>
#include <ios>
#include <ctime>
#include <cstdlib>
#include <bitset>
using namespace std;
/*
- Arrays:
- Elements(Values of the array) should be the same data type as declaration.
- Index starts at: 0
- syntax: data_type var_name[array_size];
- array_size: is the array boundaries ->[start][elements][end]<-
- empty_values: are garbage data defined by the compiler.
- Define the array as Constant, *Can Not Be Modified Later*:
- ex:
const std::string grades [] { "VERY BAD", "BAD", "GOOD", "VERY GOOD", "EXCELLENT" };
- ex:
int collection[12]; // An Empty Array Values
double _3d[3] { 8.5, 4.6, 3.7 }; // An Array With 3 Elements As Double
char chs[5] = { 'A', 'H', 'M', 'E', 'D' }; // An Array With 5 Elements As Char
*/
void arrays()
{
// Decalaring an Array
int grades[8]; // An Array storing 8 integers
// Accessing the array elements
// Remeber grades[7] is equal to element 8 in the array, because the array starts at index 0
std::cout << "Grade 8 is: " << grades[7] << std::endl;
// Error: Out of boundary of the array: last element should be 7
// Maybe cause compile-time/run-time error, dependes on the compiler
std::cout << "Grade 9 is: " << grades[8] << std::endl; // throws a garbage value
// Writing/Modifying an array element
grades[3] = 4; // Writing manually
std::cout << std::endl;
// Writing/Modifying an array elements through for loop
for(auto index { 0 }, inc { 1 }; auto value : grades)
{
grades[index] = inc;
++inc;
++index;
}
std::cout << std::endl;
// Accessing the data through for loop
for(auto element : grades)
std::cout << "Current Element Value Is: " << element << std::endl;
std::cout << std::endl;
// An Initialize Array
// An Array With 5 Elements As Char
char chs[5] = { 'A', 'H', 'M', 'E', 'D' };
std::cout << "My Name Is: ";
for(auto element : chs)
std::cout << element;
std::cout << std::endl;
std::cout << std::endl;
// Define An Array As Constant
// Note: This Array Can Not Be Modified Later!!!!!!
const std::string str_grades []
{
"VERY BAD",
"BAD",
"GOOD",
"VERY GOOD",
"EXCELLENT"
};
for(auto element : str_grades)
std::cout << "Current Element Value Is: " << element << std::endl;
std::cout << std::endl;
std::cout << std::endl;
// Sum an array elements
int sum { 0 };
for(auto value : { 1, 2, 3, 4, 5 })
sum += value; // sum = 1 + 2 + 3 + 4 + 5
std::cout << "The Sum Of Values Is: " << sum << std::endl;
std::cout << std::endl;
std::cout << std::endl;
}
/*
- Arrays of Chars
- c-string: using '\0' null terminated
- string literal: using "" instead
*/
void arrays_of_chars()
{
// char array: valid C-string
// Should add '\0': Null Terminated at the end of the array
char fname [] { 'A', 'H', 'M', 'E', 'D', '\0' };
std::cout << "(Using c-string '\\0' Null Terminate) The First Name Is: " << fname << std::endl;
std::cout << "Size of The First Name Is: " << std::size(fname) << std::endl;
std::cout << std::endl;
std::cout << std::endl;
// char array: valid C-string
// Use "": to avoid '\0' Null Terminated
char lname [] { "SALAMA" };
std::cout << "(Using string literal \"\" Without Null Terminate) The Last Name Is: " << lname << std::endl;
std::cout << "Size of The Last Name Is: " << std::size(lname) << std::endl;
std::cout << std::endl;
std::cout << std::endl;
}
/*
- Random Numbers:
- std::rand(): generates an integer number between 0 and RAND_MAX.
- RAND_MAX: dependes on the compiler.
- Ranges: std::rand() % integer_num.
- integer_num: to define the upper bound for the range
- ex:
std:rand() % 11; // define a range [0-10]
- ((std::rand() % integer_num) + 1):
- If we want to start the range from 1 not 0 as [1-10]
- 1: is lower bound
- 10: is upper bound
- ex:
((std::rand() % 10) + 1); // Range [1-10]
- Notes:
- The Main Issue of std::rand:
- Each time the program runs, will give you the same sequance.
- std::time(0): Using UNIX time [01 January 1970][00:00:00 AM]
- Seeding: depends on the time
- std::srand(std::time(0));
*/
void arrays_rands()
{
std::cout << std::endl;
cout << "RAND_MAX is: " << RAND_MAX << endl;
std::cout << std::endl;
for(size_t i = 0; i < 10; ++i)
{
int rand_num = std::rand();
std::cout
<< "Random Number of("
<< i
<< ") is: "
<< rand_num
<< std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
}
// Without Seeding
// Using std::rand for range [0-10] starts from 0 *without seeding
void arrays_rands_ranges_range_0_to_10_starts_from_0_without_seeding()
{
// Define a range betwwen 0 to 10
int rand_num = std::rand() % 11;
for(size_t i = 0; i < 10; ++i)
{
rand_num = std::rand() % 11;
std::cout
<< "Random Number of("
<< i
<< ") is: "
<< rand_num
<< std::endl;
}
}
// Without Seeding
// Using std::rand for range [1-10] starts from 1 *without seeding
void arrays_rands_ranges_range_1_to_10_starts_from_1_without_seeding()
{
// Define a range betwwen 1 to 10
int rand_num = (std::rand() % 10) + 1;
for(size_t i = 0; i < 10; ++i)
{
rand_num = (std::rand() % 10) + 1;
std::cout
<< "Random Number of("
<< i
<< ") is: "
<< rand_num
<< std::endl;
}
}
// With Seeding
// Using std::rand for range [1-10] starts from 1 *with seeding using std::srand
void arrays_rands_ranges_range_1_to_10_starts_from_1_with_seeding()
{
// Seeding
std::srand(std::time(0)); // Using true randomizer by utilize the time
// Define a range betwwen 1 to 10
int rand_num = (std::rand() % 10) + 1;
for(size_t i = 0; i < 10; ++i)
{
rand_num = (std::rand() % 10) + 1;
std::cout
<< "Random Number of("
<< i
<< ") is: "
<< rand_num
<< std::endl;
}
}
/*
- Multi-Dimessional Array:
- syntax: type name [row_count][col_count] {};
- syntax: type name [][col_count] {}; // ommiting row_count
*/
// 2D Array
void multi_dim_2d_array()
{
// declaring and initializing 2D array
int data_packet [3] [4]
{
// col: 0 col: 1 col: 2 col: 3
{ 10, 20, 30, 40 }, // row: 0
{ 50, 60, 70, 80 }, // row: 1
{ 90, 10, 20, 30 }, // row: 2
};
// printing out the mult-dim array
for(size_t i {0}; i < std::size(data_packet); ++i)
{
std::cout << "Row: " << i << std::endl;
for(size_t j {0}; j < std::size(data_packet[i]); ++j)
{
std::cout
<< " ( Col: "
<< j
<< " val: "
<< data_packet[i][j]
<< " )\t";
}
std::cout << std::endl;
}
std::cout << std::endl << std::endl;
}
// 3D Array
void multi_dim_3d_array()
{
int cube_3d [8] /* points: */ [2] /* area: w, h */ [3] /* location-axes: x, y, z*/
{
{
{ 10, 11, 12 } /* area: 0 */, { 13, 14, 15 } /* area: 1 */
}, // point: 0
{
{ 16, 17, 18 } /* area: 0 */, { 19, 20, 21 } /* area: 1 */
}, // point: 1
{
{ 22, 23, 24 } /* area: 0 */, { 25, 26, 27 } /* area: 1 */
}, // point: 2
{
{ 22, 23, 24 } /* area: 0 */, { 25, 26, 27 } /* area: 1 */
}, // point: 3
{
{ 22, 23, 24 } /* area: 0 */, { 25, 26, 27 } /* area: 1 */
}, // point: 4
{
{ 22, 23, 24 } /* area: 0 */, { 25, 26, 27 } /* area: 1 */
}, // point: 5
{
{ 22, 23, 24 } /* area: 0 */, { 25, 26, 27 } /* area: 1 */
}, // point: 6
{
{ 22, 23, 24 } /* area: 0 */, { 25, 26, 27 } /* area: 1 */
}, // point: 7
};
std::cout << std::endl;
// printing out the mult-dim array
for(size_t j {0}; j < std::size(cube_3d); ++j) // j loop for points
{
for(size_t k {0}; k < std::size(cube_3d[j]); ++k) // k loop for area
{
for(size_t l {0}; l < std::size(cube_3d[j][k]); ++l) // l loop for axes
{
std::cout
<< " ( point: "
<< j
<< "\n\t area: "
<< k
<< "\n\t\t axes: "
<< l
<< "\n\t\t\t val: "
<< cube_3d[j][k][l]
<< " ) \n\n";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << std::endl;
}
#endif