Object Oriented Programming in C++
ARRAYS
Instructor: Laban Gasper
ARRAYS
In C++, an array is a variable that can store multiple values of the same type
They provide a way to organize and manage multiple values under a single variable name
Declaration of Arrays
To declare an array, specify the data type, the array name, and the size of the array in
square brackets
dataType arrayName[arraySize];
For example, to declare an integer array named numbers with a size of 5:
int numbers[5];
Example 02: strings
string cars[4];
ARRAYS
C++ Array Initialization
In C++, it’s possible to initialize an array during declaration
To insert values to it, we can use an array literal - place the values in a comma-separated
list, inside curly braces:
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
To create an array of three integers, you could write
int myNum[3] = {10, 20, 30};
If you initialize only a subset of elements, the remaining elements are initialized to 0 by
default:
int numbers[5] = {1, 2, 3}; // numbers will be {1, 2, 3, 0, 0}
ARRAYS
Access the Elements of an Array
In C++, each element in an array is associated with a number.
The number is known as an array index. We can access elements of an array by using
those indices
ARRAYS
Another method to initialize array during declaration:
//declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15}
Here, we have not mentioned the size of the array. In such cases, the compiler
automatically computes the size
ARRAYS
Change an Array Element
To change the value of a speci c element, refer to the index number
Example
string cars[4] = {"Volvo", "BMW", "Ford", “Mazda"};
cars[0] = “Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
What is the output of the following code?
string names[4] = {"Liam", "Jenny", "Angie", "Kasper"};
cout << names[2];
fi
ARRAYS
Multidimensional Arrays
In C++, we can create an array of an array, known as a multidimensional array. For
example: int x[3][4]
Here, x is a two-dimensional array. It can hold a maximum of 12 elements.
We can think of this array as a table with 3 rows and each row has 4 columns as shown
below.
ARRAYS
Multidimensional Arrays
In three-dimensional arrays also work in a similar way.
For example: oat x[2][4][3]
This array can hold a maximum of 24 elements.
x
We can nd out the total number of elements in the array simply by multiplying its
dimensions:
i. e 2 x 4 x 3 = 24
fl
fi
ARRAYS
Multidimensional Array Initialization
Like a normal array, we can initialize a multidimensional array in more than one way.
Initialization of two-dimensional array
int test[2][3] = {2, 4, 5, 9, 0, 19};
The above method is not preferred. A better way to initialize this array with the same
array elements is given below:
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
ARRAYS
Multidimensional Array Initialization
This array has 2 rows and 3 columns, which is why we have two rows of elements with 3
elements each
ARRAYS
Multidimensional Array Initialization
Initialization of three-dimensional array
int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23,2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};
This is not a good way of initializing a three-dimensional array. A better way to initialize
this array is:
int test[2][3][4] = {{ {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },{ {13, 4, 56, 3}, {5, 9, 3, 5}, {5, 1, 4, 9} }};
Notice the dimensions of this three-dimensional array
FOR LOOP
In C++, a for loop is used to repeat a block of code a speci c number of times. The
general syntax is
for (initialization; condition; update) {
// Code to execute in each iteration
}
Example
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
cout << "i = " << i << endl;
}
return 0;
}
fi
FOR LOOP
Explanation
Initialization: int i = 0 sets the starting point.
Condition: i < 5 checks if the loop should continue.
Update: i++ increases the value of after each iteration
i
Looping through an array
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << "Element " << i << ": " << numbers[i] << endl;
}
return 0;
}
FOR LOOP
Range-based for loop
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
for (int num : numbers) {
cout << num << endl;
}
return 0;
}