Structured Programming
Presented by:
Dr . Mona Hussein Alnaggar
2023-2024
1st term
Lecture 5
1
Agenda
2
C++ Arrays
Array Declaration and Initialization
// declare and initialize and array
int x[6] = {19, 10, 8, 17, 9, 15};
Array
C++ Arrays
C++ Arrays
Arrays are 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:
C++ Arrays
C++ Arrays
We have now 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:
To create an array of three integers, you could write:
C++ Arrays
Access the Elements of an Array
You access an array element by referring to the index number inside square brackets [].
This statement accesses the value of the first element in cars:
Note: Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
C++ Arrays
Change an Array Element
To change the value of a specific element, refer to the index number:
C++ Arrays and Loops
Loop Through an Array
You can loop through the array elements with the for loop.
The following example outputs all elements in the cars array:
C++ Arrays and Loops
Loop Through an Array
This example outputs the index of each element together with its value:
C++ Arrays and Loops
Loop Through an Array
And this example shows how to loop through an array of integers:
C++ Arrays and Loops
The foreach Loop
There is also a "for-each loop" (introduced in C++ version 11 (2011), which is used exclusively to loop
through elements in an array:
C++ Arrays and Loops
The foreach Loop
The following example outputs all elements in an array, using a "for-each loop":
C++ Omit Array Size
Omit Array Size
In C++, you don't have to specify the size of the array. The compiler is smart enough to determine the size
of the array based on the number of inserted values:
The example above is equal to:
C++ Arrays and Loops
Omit Elements on Declaration
It is also possible to declare an array without specifying the elements on declaration, and add them later:
Example:
Write a simple code to find the first letter from your friends names
Example:
Write a simple code to find the first letter from your friends names
Example 2
Write a simple code to print the full name of your friends which concatenate the first
name and last name
Example 2
Write a simple code to print the full name of your friends which concatenate the first
name and last name
C++ Array Size
Get the Size of an Array
To get the size of an array, you can use the sizeof() operator:
C++ Array Size
Get the Size of an Array
Why did the result show 20 instead of 5, when the array contains 5 elements?
• It is because the sizeof() operator returns the size of a type in bytes.
• You learned from the Data Types chapter that an int type is usually 4 bytes, so from the example
above, 4 x 5 (4 bytes x 5 elements) = 20 bytes.
C++ Array Size
Get the Size of an Array
To find out how many elements an array has, you have to divide the size of the array by the size of the
data type it contains:
C++ Array Size
Loop Through an Array with sizeof()
In the Arrays and Loops Chapter, we wrote the size of the array in the loop condition (i < 5). This is not
ideal, since it will only work for arrays of a specified size.
However, by using the sizeof() approach from the example above, we can now make loops that work for
arrays of any size, which is more sustainable.
Instead of writing:
C++ Array Size
It is better to write:
Note that, in C++ version 11 (2011), you can also use the "for-each" loop:
C++ Multi-Dimensional Arrays
Multi-Dimensional Arrays
A multi-dimensional array is an array of arrays.
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:
C++ Multi-Dimensional Arrays
Multi-Dimensional Arrays
As with ordinary arrays, you can insert values with an array literal - a comma-separated list inside curly
braces. In a multi-dimensional array, each element in an array literal is another array literal.
C++ Multi-Dimensional Arrays
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.
This statement accesses the value of the element in the first row (0) and third column (2) of the letters
array.
C++ Multi-Dimensional Arrays
Change Elements in a Multi-Dimensional Array
To change the value of an element, refer to the index number of the element in each of the dimensions:
C++ Multi-Dimensional Arrays
Loop Through a Multi-Dimensional Array
To loop through a multi-dimensional array, you need one loop for each of the array's dimensions.
The following example outputs all elements in the letters array:
C++ Multi-Dimensional Arrays
Why Multi-Dimensional Arrays?