-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.cpp
More file actions
146 lines (114 loc) · 4.27 KB
/
Copy patharray.cpp
File metadata and controls
146 lines (114 loc) · 4.27 KB
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
// ===================================================
// 1. DECLARATION & ACCESSING ARRAYS (1D)
// ===================================================
// Fixed size container; index starts from 0
cout << "=== 1. DECLARATION & ACCESSING ===" << endl;
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
int myNum[3] = {10, 20, 30};
// Access element using index [0]
cout << "First car: " << cars[0] << endl; // Outputs Volvo
cout << "First num: " << myNum[0] << endl;
// ===================================================
// 2. LOOPING THROUGH ARRAYS
// ===================================================
cout << "\n=== 2. LOOPING THROUGH ARRAYS ===" << endl;
string vehicles[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
// Standard For Loop
for (int i = 0; i < 5; i++) {
cout << i << " = " << vehicles[i] << "\n";
}
// For-Each Loop (C++11 Range-based)
cout << "\nFor-Each Loop:" << endl;
for (string car : vehicles) {
cout << car << "\n";
}
// ===================================================
// 3. OMIT SIZE / ELEMENTS & ARRAY SIZE (sizeof)
// ===================================================
cout << "\n=== 3. ARRAY SIZE & sizeof ===" << endl;
// Omitting array size (compiler infers size)
string autoCars[] = {"Volvo", "BMW", "Ford"};
// Declare size first, assign values later
string dynamicAssign[5];
dynamicAssign[0] = "Volvo";
dynamicAssign[1] = "BMW";
// Get number of elements using sizeof()
int numbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(numbers) / sizeof(numbers[0]); // 20 bytes / 4 bytes = 5 elements
cout << "Array Length: " << getArrayLength << endl;
// Loop dynamically using calculated length
for (int i = 0; i < getArrayLength; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// ===================================================
// 4. REAL-LIFE EXAMPLES (AVERAGE & LOWEST VALUE)
// ===================================================
cout << "\n=== 4. REAL-LIFE PRACTICES ===" << endl;
int ages[8] = {20, 22, 18, 35, 48, 26, 87, 70};
float sum = 0;
int length = sizeof(ages) / sizeof(ages[0]);
// Average Calculation
for (int age : ages) {
sum += age;
}
float avg = sum / length;
cout << "Average age is: " << avg << endl;
// Find Lowest Value
int lowestAge = ages[0];
for (int age : ages) {
if (lowestAge > age) {
lowestAge = age;
}
}
cout << "Lowest age is: " << lowestAge << endl;
// ===================================================
// 5. MULTI-DIMENSIONAL ARRAYS (2D & 3D)
// ===================================================
cout << "\n=== 5. MULTI-DIMENSIONAL ARRAYS ===" << endl;
// 2D Array [rows][columns]
string letters[2][4] = {
{"A", "B", "C", "D"},
{"E", "F", "G", "H"}
};
cout << "Element at [0][2]: " << letters[0][2] << endl; // Outputs "C"
// Nested loop for 2D Array
cout << "\nPrinting 2D Array Grid:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 4; j++) {
cout << letters[i][j] << " ";
}
cout << endl;
}
// 3D Array [blocks][rows][columns]
string letters3D[2][2][2] = {
{ {"A", "B"}, {"C", "D"} },
{ {"E", "F"}, {"G", "H"} }
};
cout << "\nLooping 3D Array:" << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
cout << letters3D[i][j][k] << " ";
}
}
}
cout << endl;
// ===================================================
// 6. FIXED SIZE (ARRAY) VS DYNAMIC SIZE (VECTOR)
// ===================================================
cout << "\n=== 6. VECTOR (DYNAMIC ARRAY) ===" << endl;
// Array biasa ukurannya kaku (Fixed Size). Vector ukurannya fleksibel (Dynamic).
vector<string> vectorCars = {"Volvo", "BMW", "Ford"};
vectorCars.push_back("Tesla"); // Adds "Tesla" at the end dynamically
for (string vCar : vectorCars) {
cout << vCar << " ";
}
cout << endl;
return 0;
}