A **C++ matrix **is created **by using two-dimensional arrays**. In this article, you will get to know about different methods to create matrices with the knowledge of our coding experts.

We have surveyed different methods and techniques of **creating matrices in C++** to bring you the easiest ways to learn the concept of matrices. Keep reading to find them all out!

## What Is a Matrix in C++?

You have already studied the concept of a matrix in mathematics; but what is **the concept of a matrix in C++**? Well, as you know, C++ is a basic programming language with a vast amount of topics. An array of numbers that is organized rows and columns is called a matrix in C++. And this concept of rows and columns is known as a two-dimensional array.

## What Is a Two-Dimensional Array in C++?

In C++, two-dimensional arrays are the **type of arrays consisting of more than one row** and column. In two-dimensional arrays, there are two indexes for each element. The first index refers to the row of the matrix and the second index refers to the column of the matrix.

## How To Create a Matrix in C++

You now know more about what is a matrix and what is a two-dimensional array in the above sections. Now we will teach you about **how to make a matrix in C++**. So let us get started.

To start, we thought we could bring you the list below that includes the **steps of creating a matrix in C++**:

- Declaration of a matrix
- Initialization of a matrix in c++
- Printing output of a matric
- Taking input from the user in a matrix

### 1. C++ Matrix: Declaration of a Matrix

Declaration of two-dimensional arrays is just like a single-dimensional array except that **two-dimensional arrays have two indexes.** The first index refers to the row of the matrix and the second index refers to the column to the matrix that is A (row x column)or A (n x m). Just like in mathematics, the first element is indicated by a11 and so on.

Have a look at an example of a simple matrix:

[a11 a12 a21 a22 ]

Now to declare rows and columns we have to declare two indexes and an array.

#### – Declaration of a Matrix: Syntax

(data-type) array [number of rows] [number of columns];

#### – Declaration of a Matrix: Example

Int arr[2][2];

#### – Declaration of a Matrix: Sample Program

#include <iostream> using namespace std; int main(){ int n,m; int a[2][2]; } |

### 2. Initialization of a Matrix in C++

Like single-dimensional arrays, you can **initialize a matrix in a two-dimensional array** after declaration. For this purpose, you have to write the values in such an order that they will store in rows from left to right. Here you can understand this better with the help of an example.

#### – C++ Matrix Initialization: Syntax

int array[n][m] = {a11,a12,a13,a21,a22,a23}

#### – C++ Matrix Initialization: Example

int array[3][4] = {1,1,2,3,4,5,6,7,8,9,0,10}

the above matrix has **three rows and four columns**. The elements will be stored in the matrix in the same order such that the first four elements will be stored in a row from the left and the next four elements will be stored in the second row and so on. Here is a sample of how the matrix will look:

[ 1 1 2 3 4 5 6 7 8 9 0 10 ]

#### – C++ Matrix Initialization: Sample program

Let’s take a closer look at some sample programs:

#### – C++ Matrix Initialization: Code

#include <iostream> using namespace std; int main(){ int n,m; int a[2][2]= { 1,2,3,4}; cout<<”the array matrix is:”<<a[1][1]; return 0; } |

#### – C++ Matrix Initialization: Output

the array matrix is:4 |

#### – C++ Matrix: Alternate Method for Matrix Initialization

For your better understanding, we can use brackets **to differentiate different rows.** It is better to use this method instead of the first one because it is easy to understand that where the first row ends and the second row starts.

#### – Alternate Method for Matrix Initialization: Syntax

int array[n][m] = { {a11,a12,a13},{a21,a22,a23} }

#### – Alternate Method for Matrix Initialization: Example

int array[3][4] ={ {1,1,2,3},{4,5,6,7},{8,9,0,10} }

The matrix above has three rows and four columns. The elements will be stored in the matrix in the same order **such that the pair of the first four elements will be stored** in the first row from the left and the pair of the next four elements will be stored in the second row and so on. Here is a sample of how the matrix will look:

[ 1 1 2 3 4 5 6 7 8 9 0 10 ]

#### – Alternate Method for Matrix Initialization: Sample program

Let’s take a closer look at some sample programs:

#### – Alternate Method for Matrix Initialization: Code

#include <iostream> using namespace std; int main(){ int n,m; int a[2][2]= {{ 1,2},{3,4}}; cout<<”the array matrix is:”<<a[1][1]; return 0; } |

#### – Alternate Method for Matrix Initialization: Output

the array matrix is:4 |

#### – Alternate Method for Matrix Initialization: Explanation

You might be wondering how the output of a[1][1] is four. Well, it is not that hard to understand.

Just keep in mind that, In arrays,** if the size of an array is n then its index will be from zero to (n-1).** Therefore, for the row index one, the row number is: (1+1=2). As the second row includes {3,4}, the second element of index one is four.

### 3. C++ Matrix: Printing Output of a Matrix

The last step **after declaration and storing input** is printing the output of the matrix. To print all the elements in the matrix, we use two loops. One loop will traverse rows and the other loop will traverse columns.

#### – Printing Output of a Matrix: Syntax

for (int i = 0; i < number of rows; i++) { for (int j = 0; j < number of columns; j++) { cout << “Element at array[” << i << “][” << j << “]: “; cout << array[i][j]<<endl; } } |

#### – Printing Output of a Matrix: Example

For example, let us suppose we have an array having **three rows and two columns** such that:

array[3][2]={{0,1},{2,3},{4,5}}.

Here is an example of how we will add loops to print the whole array.

for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << “Element at array[” << i << “][” << j << “]: “; cout << array[i][j]<<endl; } } |

#### – Printing Output of a Matrix: Sample Program

Here is a sample program to understand more clearly the **printing of two-dimensional arrays/matrices in C++.**

// C++ Program to print the elements of a // Two-Dimensional array #include<iostream> using namespace std; int main() { // an array with 3 rows and 2 columns. int array [3][2] = {{0,2}, {1,3}, {9,5}}; // output each array element’s value for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << “Element at array[” << i << “][” << j << “]: “; cout << array[i][j]<<endl; } } return 0; } |

#### – Printing Output of a Matrix: Output

Element at array[0][0]: 0 Element at array[0][1]: 2 Element at array[1][0]: 1 Element at array[1][1]: 3 Element at array[2][0]: 9 Element at array[2][1]: 5 |

### 4. C++ Matrix: Taking Input From the User in a Matrix

We have already learned** how to initialize the values of a 2D matrix in C++** with the declaration of arrays. We can also take input from users and arrange them in the form of a matrix. For this purpose, we use two loops. One loop indicates rows and the other loop indicates columns.

#### – Taking Input From the User in a Matrix: Syntax

Here is the syntax you will need to follow for taking input from users in a matrix:

int array[number of rows] [number of columns] for (int i = 0; i < number of rows; i++) { for (int j = 0; j < number of columns; j++) { cout<<“\ns[“<<i<<“][“<<j<<“]= “; cin>>s[i][j]; } } |

#### – Taking Input From the User in a Matrix: Example

Let us suppose we have an array having three rows and two columns such that:

array[3][2]={{0,1},{2,3},{4,5}}.

Here is an example of how we will add loops to print the whole array.

for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout<<“\ns[“<<i<<“][“<<j<<“]= “; cin>>s[i][j]; } } |

#### – Taking Input From the User in a Matrix: Sample Program

Here is a sample program to understand more clearly** the printing** of two-dimensional arrays/matrices in C++:

// C++ Program to print the elements of a // Two-Dimensional array #include<iostream> using namespace std; int main() { int array[3][2]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout<<“\ns[“<<i<<“][“<<j<<“]= “; cin>>array[i][j]; } } // output each array element’s value for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { cout << “Element at array[” << i << “][” << j << “]: “; cout <<array[i] [j]<<endl; } } return 0; } |

#### – Taking Input From the User in a Matrix: Output

s[0] [0]= 1 s[0] [1]= 2 s[1] [0]= 3 s[1] [1]= 4 s[2] [0]= 5 s[2] [1]= 6Element at array[0] [0]: 1 Element at array[0] [1]: 2 Element at array[1] [0]: 3 Element at array[1] [1]: 4 Element at array[2] [0]: 5 Element at array[2] [1]: 6 |

#### – Taking Input From the User in a Matrix: Explanation

In the above program, **we use loops for two different purposes**. The first pair of the for loop was created to store the values that the user will enter and the second pair of for loops was for printing the output user entered in the matrix.

## Customizing the Size of the Matrix

In C++, matrices are not restricted to only one size, **they can be of different sizes,** and you also have the option of choosing the size of your matrix. In this part, you will learn how you can change the size of the matrix of your own choice. Let’s get started!

### – Customizing the Size of the Matrix: Syntax

You can customize our matrix size by using this code:

cout <<“Enter Rows of Matrix\n”; cin >> rows; cout <<“Enter Columns of Matrix\n”; cin >> cols;cout <<“Enter Matrix of size “<<rows<<” X “<<cols; // Input first matrix*/ for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ cin >> array[i][j]; } |

### – Customizing the Size of the Matrix: Sample Program

To illustrate our point, we have brought you **a sample program**:

#include <iostream> using namespace std; int main(){ int rows, cols,array [50][50],i,j; cout <<“Enter Rows of Matrix\n”; cin >> rows; cout <<“Enter Columns of Matrix\n”; cin >> cols; cout <<“Enter Matrix of size “<<rows<<” X “<<cols; for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ cin >> array[i][j]; } } cout <<“The Matrix\n”<<endl; for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ cout << array[i][j] << ” “; } cout << “\n”<<endl; } return 0; } |

### – Customizing the Size of the Matrix: Output

Enter Rows of Matrix 2 Enter Columns of Matrix 2 Enter Matrix of size 2 X 2 1 2 3 4The Matrix 1 23 4 |

## Adding Two Matrices Using Two-Dimensional Arrays in C++

Like mathematics, **we can add two matrices in C++** using two-dimensional arrays.You first need to add two matrices and then store the result in another two-dimensional array. It will be like this visually:

[a11 a12 a21 a22 ] + [ b11 b12 b21 b22 ] = [ a11+b11 a12+b12 a21+b21 a22+b22 ]

### – Adding Two Matrices Using Two-Dimensional Arrays in C++: Syntax

Suppose we have two matrices which we named one and two. Here is **the code** **to add both matrices together**:

for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ sum[i][j] = one[i][j] + two[i][j]; } } |

### – Adding Two Matrices Using Two-Dimensional Arrays in C++: Sample Program

Here is a sample program to understand more clearly the addition of **two-dimensional arrays/matrices in C++.**

### – Adding Two Matrices Using Two-Dimensional Arrays in C++: Code

// C++ program to add two matrix #include <iostream> using namespace std; int main(){ int rows, cols, i, j; int one[50][50], two[50][50], sum[50][50]; cout <<“Enter Rows and Columns of Matrix\n”; cin >> rows >> cols; cout <<“Enter first Matrix of size “<<rows<<” X “<<cols; // Input first matrix*/ for(i = 0; i < rows; i++){ fo(j = 0; j < cols; j++){ cin >> one[i][j]; } } // Input second matrix cout <<“\nEnter second Matrix of size “<<rows<<” X “<<cols; for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ cin >> two[i][j]; } } /* adding the elements of both matrices sum[i][j] = one[i][j] + two[i][j] */ for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ sum[i][j] = one[i][j] + two[i][j]; } } cout <<“Sum of Matrix\n”; for(i = 0; i < rows; i++){ for(j = 0; j < cols; j++){ cout << sum[i][j] << ” “; } cout << “\n”; } return 0; } |

### – Adding Two Matrices Using Two-Dimensional Arrays in C++: Output

Enter Rows and Columns of Matrix 2 2 Enter the first Matrix of size 2 X 2 1 8 3 4 Enter the second Matrix of size 2 X 2 2 5 8 0 Sum Matrix 3 1 3 1 1 4 |

### – Adding Two Matrices Using Two-Dimensional Arrays in C++: Explanation

First of all, we have **declared the variables** and arrays of size 50. Now, we have taken the size of the matrices as the input from a user and then we took elements of both matrices from the user. Then we added both matrices and at last, printed the result.

## Conclusion:

The **concept of matrices in C++** is very important since it can really make a difference in your code and **the way the output is printed out**. Here is what we covered in this article with regards to this topic:

- A matrix can be created using the concept of two-dimensional arrays.
- You can initialize the matrix during the time of declaration.
- It is possible to customize the size of a matrix by your own choice.
- You can add two matrices using two-dimensional arrays in C++

If you create practice programs with **two-dimensional arrays** in them, you will be able to make different concepts regards matrices. Whenever you are confused about creating matrices in C++ come back here!

- PHP Class: A Comprehensive Glance Into the Exciting World of Classes - November 28, 2021
- PHP Integers: Working Efficiently With Integers and Converting to Them - November 28, 2021
- PHP Form: Perfect Ways To Optimize User Input From Forms - November 28, 2021