Matrix in C++: Array of Arrays

In this tutorial from our C++ Course, we will learn the important concept of matrix in C++.

Array of Arrays - What is it? What is it for?

So far, in our arrays section, we have created arrays of integers, float, doubles, char etc.
That is, we create arrays that store numbers or characters.
However, it is also possible to store other things in arrays.

A curious thing to store in arrays are other arrays.

For example, imagine that you were hired by a school to do a C++ program that will, among other things, store students' grades. Each student has, for example, 5 different grades.

You can, first of all, reason like this: create an array to store students.
For example: students[10]

However, each student has 5 grades. So each student will have an array of grades:
notes[5]

You see: we have an array of students, each block represents a student. And within each block, that is, each student, there is a kind of internal array, with the grades of each student. Each student has his own array of grades, and each student is part of the student array.

Shall we formalize this?

How to declare an Array in C++

We call it a matrix, an array of arrays, or multidimensional arrays.
So far, we only work with arrays of one dimension, just a 'line' of blocks, which form an array.

To increase the number of dimensions of an array, just add pairs of square brackets, for example:

  • To declare an array of one dimension, we do: float grade[5];
  • To declare an array of two dimensions, we do: float students[10][5];


Come on. When we do 'float grade[5]' we mean: 5 float blocks.
When we do: 'float students[10][5]', we mean: 10 blocks, where each block has an array inside, of 5 floats.

We say that this is a 10x5 matrix (10 rows and 5 columns, where each row represents a student, and each column represents a different grade).

To make it easier, let's imagine a 3x3 matrix, we declare it like this:

  • int matrix[3][3];


The expression 'matrix[3]' means: an array of 3 elements.
What does each element contain? An integer? A float? A char? No, each element is another array, of size [3].

See the representation of this matrix:
Matrix in C++

The first element of our matrix is: matrix[0]
It is an array, of 3 elements, which are represented by:
First element: matrix[0][0]
Second element: matrix[0][1]
Third element: matrix[0][2]


The second element of our matrix is: matrix[1]
It is an array, of 3 elements, which are represented by:
First element: matrix[1][0]
Second element: matrix[1][1]
Third element: matrix[1][2]

The third element of our matrix is: matrix[2]
It is an array, of 3 elements, which are represented by:
First element: matrix[2][0]
Second element: matrix[2][1]
Third element: matrix[2][2]

That is, to declare an array of 'i' rows and 'j' columns, we do:

  • type matrix[i][j];


If you have studied matrix and determinants at school, you should remember how to use a matrix, how to flag each element, etc. The only difference here is that the count starts from index 0, instead of 1.

How to initialize a Matrix in C++

We will create a matrix to store the grades of 3 students, where each student has two grades.
That is, we will have a matrix of 3 rows (one for each student) and 2 columns (each column represents a grade). We declare this matrix, then:

  • grade float[3][2];


We will now initialize a 3x3 matrix, for example, with the notes:
float grade[3][3] = {{9.5, 9.1},
                                {8.8, 8.4},
                                {10.0, 10.0}};


The first student's grade array is: {9.5, 9.1}
The second student's grade array is: {8.8, 8.4}
The third student's grade array is: {10.0, 10.0}

For example, what is the second student's first grade? Second student is student 1, first grade is index 0, so that grade is stored in: grade[1][0] = 8.8
The second grade of the second student is: grade[1][1] = 8.4

Simple, isn't it?

No comments:

Post a Comment