Revisio
n on
Array
Array
•Array can be used to store
multiple values in a single
variable, instead of declaring
separate variables for each
value.
•To declare an array, define the
variable type, specify the
name of the array followed by
square brackets and specify the
number of elements it should
store:
We have declared a variable that holds an array of four
strings.
To insert values to it, we can use an array literal - place
the values in a comma- separated list, inside curly
braces:
Example
string snames[4] = {"Abebe", "Lemma", "Tigist", "Hana"};
To create an array of three integers, we could write:
int myNum[3] = {10, 20, 30};
Access the Elements of an Array
• Youcan access an array
element by referring to
the index number inside
square brackets [].
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string snames[4] = {"Abebe", "Lemma", "Tigist",
"Hana"};
cout << snames[0];
Note:
return 0;
}
Array indexes start with 0: [0] is the first
element. [1] is the second element, etc.
Change an Array Element
To change the value of a specific element, refer to
the index number:
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string snames[4] = {"Abebe", "Lemma", "Tigist",
"Hana"};
snames[0] = “Kebede";
cout << snames[0];
return 0;
Arrays and loops
Loop Through an Array
• We can loop through the array elements with the for loop.
• The below example outputs all elements in the cars array:
#include <iostream> #include <iostream>
#include <string> #include <string>
using namespace std; using namespace std;
int main() {
int main() {
string cars[5] = {"Volvo", "BMW", "Ford",
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Mazda", "Tesla"};
"Tesla"}; for (int i = 0; i < 5; i++) {
for (int i = 0; i < 5; i++) { cout << i << " = " << cars[i] << "\n";
cout << cars[i] << "\ }
n"; return 0;
} }
return 0;
}
How to loop through an array of
integers?
#include<iostream>
using namespace std;
int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
return 0;
}
The for-each
•Loop
There is also a "for-each loop", which is used exclusively to loop
through elements in an array:
Syntax
for (type variableName :
arrayName) { #include <iostream>
// code block to be using namespace std;
executed
} int main() {
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}
return 0;
}
Omit Array Size
• In
C++, we don't have to specify the size of
the array.
• Thecompiler determine the size of the array
based on the number of inserted values:
string cars[] = {"Volvo", "BMW",
"Ford"}; // Three arrays
string cars[3] = {"Volvo", "BMW",
"Ford"}; // Also three arrays
Cont
• It is also possible to declare an array without specifying the elements
…. on
declaration, and add them later:
#include <iostream>
#include <string>
using namespace std;
int main() {
string cars[5]; cars[0] = "Volvo";
cars[1] = "BMW";
cars[2] = "Ford";
cars[3] = "Mazda";
cars[4] = "Tesla"; for(int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}
return 0;
}
Array
Size
Get the Size of an Array
• To get the size of an array, we can use the sizeof() operator:
• The sizeof() operator returns the size of a type in bytes.
Example
#include <iostream>
using namespace std;
int main() {
int myNumbers[5] = {10,
20, 30, 40, 50};
cout << sizeof(myNumbers);
return 0;
}
• To find out how many elements an array has, you have to Cont…
divide the size of the array by the size of the data type it
contains:
• Example
#include
<iostream>
using
namespace std;
int main() {
int
myNumbers[5
] = {10, 20, 30,
40, 50};
int getArrayLength =
sizeof(myNumbers) / sizeof(int); cout <<
Multi-
Dimensional
A multi-dimensional array is an array of arrays.
Arrays
Arrays can have any number of dimensions.
To declare a multi-dimensional array
• Define the variable type,
• Specify the name of the array followed by square
brackets which specify how many elements the main
array has,
• Followed by another set of square brackets which
indicates how many elements the sub-arrays have:
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
Access the Elements of a Multi-
Dimensional Array
• To access an element of a multi-dimensional array, specify an index
number in each of the array's dimensions.
• The statement below accesses the value of the element in the first
row (0) and third column (2) of the letters array.
#include<iostream>
using namespace std;
int main() {
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
Remember: Array indexes start with 0:
cout << letters[0][2]; [0] is the first element. [1] is the second
return 0; element, etc.
}
Cont
To change the value of an element, refer to the index
….
number of the element in each of the dimensions:
Example #include <iostream>
using namespace std;
int main() {
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
letters[0][0] = "Z";
cout << letters[0][0];
return 0;
}
Cont
Loop Through a Multi-Dimensional Array ….
• To loop through a multi-dimensional array, you need one loop
for each of the array's dimensions.
Example #include <iostream>
using namespace std;
int main() {
string letters[2][4] = {
{ "A", "B", "C", "D" },
{ "E", "F", "G", "H" }
};
for (int i = 0; i < 2; i++)
{ for (int j = 0; j < 4; j+
+) {
cout << letters[i][j] <<
"\n";
}
}