Course2 Lecture2 M Array
Course2 Lecture2 M Array
Mohammed Yaseen
Programming In C++
Course 2: Lecture 2, Multiple Arrays
1
Programming Principles II A_lecturer. Mohammed Yaseen
Lecture 2
Arrays in C++
C++ Arrays
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value.
Declare an array
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.
Syntax
type arrayName [arraySize];
Example
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"};
string
cars "Volvo" "BMW", "Ford" "Mazda"
2
Programming Principles II A_lecturer. Mohammed Yaseen
Example
int myNum[3] = {10, 20, 30};
int
myNum 10 20 30
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cout << cars[0];
// Outputs Volvo
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
cout << cars[0];
// Now outputs Opel instead of Volvo
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << cars[i] << "\n";
}
The following example outputs the index of each element together with its
value:
3
Programming Principles II A_lecturer. Mohammed Yaseen
Example
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
for(int i = 0; i < 4; i++) {
cout << i << ": " << cars[i] << "\n";
}
This is completely fine. However, the problem arises if you want extra
space for future elements. Then you have to overwrite the existing values:
string cars[] = {"Volvo", "BMW", "Ford", "Mazda", "Tesla"};
If you specify the size, however, the array will reserve the extra space:
string cars[5] = {"Volvo", "BMW", "Ford"}; // size of array is 5, even
though it's only three elements inside it
Now you can add a fourth and fifth element without overwriting the others:
cars[3] = "Mazda";
cars[4] = "Tesla";
string cars[5];
cars[0] = "Volvo";
cars[1] = "BMW";
...
4
Programming Principles II A_lecturer. Mohammed Yaseen
Multidimensional arrays
In C++, we can create an array of an array, known as a multidimensional
array.
Syntax
type name[size1][size2]...[sizeN];
Two-Dimensional Arrays
The simplest form of the multidimensional array is the two-dimensional
array. A two-dimensional array is, in essence, a list of one-dimensional
arrays. To declare a two-dimensional integer array of size x,y, you would
write something as follows −
type arrayName [ x ][ y ];
Where type can be any valid C++ data type and arrayName will be a valid
C++ identifier.
Example
int test[2][3] = { {2, 4, 5}, {9, 0, 19}};
5
Programming Principles II A_lecturer. Mohammed Yaseen
An array is useful for storing and working with a set of data. Sometimes,
though, it’s necessary to work with multiple sets of data.
For example, in a grade-averaging program a teacher might record all of
one student’s test scores in an array of doubles. If the teacher has 30
students, that means she’ll need 30 arrays of doubles to record the
scores for the entire class. Instead of defining 30 individual arrays,
however, it would be better to define a two-dimensional array.
int main () {
// an array with 5 rows and 2 columns.
int a[3][4] = { {85, 90, 78, 92}, { 76, 88, 95, 89}, { 90, 92, 85, 87}};
return 0;
} cout << endl; 6
}
return 0;
}}
Programming Principles II A_lecturer. Mohammed Yaseen
7
Programming Principles II A_lecturer. Mohammed Yaseen
8
Programming Principles II A_lecturer. Mohammed Yaseen
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} }
};
3 4 2 3 13 4 56 3
0 -3 9 11 5 9 3 5
23 12 23 2 5 1 4 9
The first dimension has the value 2. So, the two elements comprising the
first dimension are:
The second dimension has the value 3. Notice that each of the elements
of the first dimension has three elements each:
{3, 4, 2, 3}, {0, -3, 9, 11} and {23, 12, 23, 2} for Element 1.
{13, 4, 56, 3}, {5, 9, 3, 5} and {5, 1, 4, 9} for Element 2.
Finally, there are four int numbers inside each of the elements of the
second dimension:
Example
#include <iostream>
using namespace std;
int main() {
// This array can store up to 12 elements (2x3x2)
int test[2][3][2] = {
{
{1, 2},
{3, 4},
{5, 6}
},
{
{7, 8},
{9, 10},
{11, 12}
}
};
9
Programming Principles II A_lecturer. Mohammed Yaseen
return 0;
}
The basic concept of printing elements of a 3d array is similar to that of a
2d array. However, since we are manipulating 3 dimensions, we use a
nested for loop with 3 total loops instead of just 2:
10
Programming Principles II A_lecturer. Mohammed Yaseen
Example
char greeting[6] = {'H', 'e', 'l', 'l', 'o'};
If you follow the rule of array initialization, then you can write the above
statement as follows −
The C++ compiler automatically places the '\0' at the end of the string
when it initializes the array.
String Concatenation
Example
string firstName = "John";
string lastName = "Doe";
string fullName = firstName + " " + lastName;
cout << fullName;
11