Addition of two matrices in C++
Introduction:
This tutorial will demonstrate three methods for adding two matrices in C++:
- Making use of a basic C++ program without utilization of functions.
- Utilizing a function to add two matrices.
- Applying function and class.
A Basic C++ program to multiply two matrices:
In this case the user is asked to provide the required number of rows as well as columns within the matrices. Subsequently, we ask the programmer to provide the elements of both matrices. The input is then stored in a multiple-dimensional array for each matrix, along with the corresponding elements of the two matrices are added and displayed on the screen.
#include<iostream>
using namespace std;
int main() {
int row, column, matrix_1[15][15], matrix_2[15][15], sum_of_matrix[15][15];
cout<<" Enter the required no of rows(must be greater than 1 and less than 15) : ";
cin>>row;
cout<<" Enter the required no of columns(must be greater than 1 and less than 15) : ";
cin>>column;
cout << "Enter the elements within the 1st matrix: ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j < column; j++ ) {
cin>>matrix_1[i][j];
}
}
cout << " Enter the elements within the 2nd matrix: ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
cin>>matrix_2[i][j];
}
}
cout << " Addition of two matrices is : ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
sum_of_matrix[i][j] = matrix_1[i][j] + matrix_2[i][j];
cout << sum_of_matrix[i][j] << " ";
}
}
return 0;
}
Output:
![Addition of two matrices in C++](https://static.tutorialandexample.com/cpp/addition-of-two-matrices-in-cpp1.png)
Program using the function for adding two matrices:
This program uses a user-defined function called sum(int, int) to save user-entered values for the total number of columns as well as rows rather than storing everything in the main() method. Based on the provided rows and columns, this function develops two matrices, adds the respective elements of the matrices, and returns the final result.
#include<iostream>
using namespace std;
void sum(int, int);
int main() {
int rows, columns;
cout<<" Enter the required no of rows(must be greater than 1 and less than 15) : ";
cin>>rows;
cout<<" Enter the required no of columns(must be greater than 1 and less than 15) : ";
cin>>columns;
sum(rows, columns);
return 0;
}
void sum(int row, int column) {
int matrix_1[row][column], matrix_2[row][column], sum_matrix[row][column];
cout << " Enter the elements within the 1st matrix : ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j < column; j++ ) {
cin >> matrix_1[i][j];
}
}
cout << " Enter the elements within the 1st matrix : ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
cin >> matrix_2[i][j];
}
}
cout << " Addition of two matrices is : ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
sum_matrix[i][j] = matrix_1[i][j] + matrix_2[i][j];
cout << sum_matrix[i][j] << " ";
}
}
}
Output:
![Addition of two matrices in C++](https://static.tutorialandexample.com/cpp/addition-of-two-matrices-in-cpp2.png)
Using the function and the class:
When developing this program, the same logic which we have seen earlier can be used in conjunction with classes and functions.
#include<iostream>
using namespace std;
class Addition{
public:
void sum(int row, int column){
int matrix_1[row][column], matrix_2[row][column], s[row][column];
cout << " Enter the elements within the 1st matrix: ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j < column; j++ ) {
cin >> matrix_1[i][j];
}
}
cout << " Enter the elements within the 2nd matrix: ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
cin>>matrix_2[i][j];
}
}
cout << " Addition of two matrices : ";
for (int i = 0; i<row; i++ ) {
for (int j = 0; j<column; j++ ) {
s[i][j] = matrix_1[i][j] + matrix_2[i][j];
cout << s[i][j] << " ";
}
}
}
};
int main(){
int rows, columns;
cout << " Enter the required no of rows(must be greater than 1 and less than 15) : ";
cin >> rows;
cout << " Enter the required no of columns(must be greater than 1 and less than 15) : ";
cin >> columns;
Addition obj;
obj.sum(rows, columns);
return 0;
}
Output:
![Addition of two matrices in C++](https://static.tutorialandexample.com/cpp/addition-of-two-matrices-in-cpp3.png)
Conclusion:
In this way we can add the two matrices using C++ language..