0% found this document useful (0 votes)
18 views

Arrays

This document discusses C++ arrays including how to declare and initialize them, access elements, loop through arrays, and get the size of an array. Arrays allow storing multiple values in a single variable. Elements are accessed via indexes starting from 0. Common operations covered include outputting, changing, and looping through elements with for loops and the foreach loop.

Uploaded by

Aleeza gondal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Arrays

This document discusses C++ arrays including how to declare and initialize them, access elements, loop through arrays, and get the size of an array. Arrays allow storing multiple values in a single variable. Elements are accessed via indexes starting from 0. Common operations covered include outputting, changing, and looping through elements with for loops and the foreach loop.

Uploaded by

Aleeza gondal
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

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:
• 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:


• #include <iostream>
• #include <string>
• using namespace std;

• int main() {
• string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
• cout << cars[0];
• return 0;
• }
• Output:
• 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";
• #include <iostream>
• #include <string>
• using namespace std;

• int main() {
• string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
• cars[0] = "Opel";
• cout << cars[0];
• return 0;
• }
• Output: opel
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:


• #include <iostream>
• #include <string>
• using namespace std;

• int main() {
• string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
• for (int i = 0; i < 5; i++) {
• cout << cars[i] << "\n";
• }
• return 0;
• }
output
• Volvo
BMW
Ford
Mazda
Tesla
This example outputs the index of each
element together with its value:
• #include <iostream>
• #include <string>
• using namespace std;

• int main() {
• string cars[5] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
• for (int i = 0; i < 5; i++) {
• cout << i << " = " << cars[i] << "\n";
• }
• return 0;
• }
output
• 0 = Volvo
1 = BMW
2 = Ford
3 = Mazda
4 = Tesla
And this example shows 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;
• }
output
• 10
20
30
40
50
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":
• #include <iostream>
• using namespace std;

• int main() {
• int myNumbers[5] = {10, 20, 30, 40, 50};
• for (int i : myNumbers) {
• cout << i << "\n";
• }
• return 0;
• }
output
• 10
20
30
40
50
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:
• #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;
• }
output
• Volvo
BMW
Ford
Mazda
Tesla
Get the Size of an Array

• To get the size of an array, you can use the sizeof() operator:
• #include <iostream>
• using namespace std;

• int main() {
• int myNumbers[5] = {10, 20, 30, 40, 50};
• cout << sizeof(myNumbers);
• return 0;
• }
• Output: 20
To get array length
• #include <iostream>
• using namespace std;

• int main() {
• int myNumbers[5] = {10, 20, 30, 40, 50};
• int getArrayLength = sizeof(myNumbers) / sizeof(int);
• cout << getArrayLength;
• return 0;
• }
• Output 5
Loop Through an Array with sizeof()

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


for (int i = 0; i < 5; i++) {
cout << myNumbers[i] << "\n";
}
• It is better to write:
• #include <iostream>
• using namespace std;

• int main() {
• int myNumbers[5] = {10, 20, 30, 40, 50};
• for (int i = 0; i < sizeof(myNumbers) / sizeof(int); i++) {
• cout << myNumbers[i] << "\n";
• }
• return 0;
• }
output
• 10
20
30
40
50
"for-each" loop:
• #include <iostream>
• using namespace std;

• int main() {
• int myNumbers[5] = {10, 20, 30, 40, 50};
• for (int i : myNumbers) {
• cout << i << "\n";
• }
• return 0;
• }
• 10
20
30
40
50
• Next we will study multidimensional arrays

You might also like