0% found this document useful (0 votes)
6 views5 pages

Array

The document provides an overview of C++ arrays, including how to declare, initialize, access, and modify array elements. It explains the use of loops, including traditional for loops and for-each loops, to iterate through array elements, and discusses best practices for declaring arrays and determining their size. Additionally, it covers how to use the sizeof() operator to find the size of an array and emphasizes the importance of writing sustainable loops that adapt to array size.

Uploaded by

Joshua Bachuba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Array

The document provides an overview of C++ arrays, including how to declare, initialize, access, and modify array elements. It explains the use of loops, including traditional for loops and for-each loops, to iterate through array elements, and discusses best practices for declaring arrays and determining their size. Additionally, it covers how to use the sizeof() operator to find the size of an array and emphasizes the importance of writing sustainable loops that adapt to array size.

Uploaded by

Joshua Bachuba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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:

string cars[4];

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:

string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};

To create an array of three integers, you could write:

int myNum[3] = {10, 20, 30};

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:

Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo
Note: 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:

cars[0] = "Opel";

Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo

C++ Exercises
Test Yourself With Exercises
Exercise:
Create an array of type string called cars.

C++ Arrays and Loops


You can loop through the array elements with the for loop.

The following example outputs all elements in the cars array:

Example
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << cars[i] << "\n";
}

This example outputs the index of each element together with its value:

Example
string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
for (int i = 0; i < 5; i++) {
cout << i << " = " << cars[i] << "\n";
}
And this example shows how to loop through an array of integers:

Example
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}

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:

Syntax
for (type variableName : arrayName) {
// code block to be executed
}

The following example outputs all elements in an array, using a "for-


each loop":

Example
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}

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:

string cars[] = {"Volvo", "BMW", "Ford"}; // Three array elements

The example above is equal to:

string cars[3] = {"Volvo", "BMW", "Ford"}; // Also three array elements


However, the last approach is considered as "good practice", because it will
reduce the chance of errors in your program.

Omit Elements on Declaration


It is also possible to declare an array without specifying the elements on
declaration, and add them later:

Example
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
...

Get the Size of an Array


To get the size of an array, you can use the sizeof() operator:

Example
int myNumbers[5] = {10, 20, 30, 40, 50};
cout << sizeof(myNumbers);

Result:

20

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.

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:

Example
int myNumbers[5] = {10, 20, 30, 40, 50};
int getArrayLength = sizeof(myNumbers) / sizeof(int);
cout << getArrayLength;

Result:

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:

int myNumbers[5] = {10, 20, 30, 40, 50};


for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}

It is better to write:

Example
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i = 0; i < sizeof(myNumbers) / sizeof(int); i++) {
cout << myNumbers[i] << "\n";
}

Note that, in C++ version 11 (2011), you can also use the "for-each" loop:

Example
int myNumbers[5] = {10, 20, 30, 40, 50};
for (int i : myNumbers) {
cout << i << "\n";
}

You might also like