Write a c++ program to subtract two Matrices
Answers
Answer:
#include <iostream>
using namespace std;
int main()
{
int sum = 0 , sum1 = 0 , row , collumn , row1 , collumn1;
cout<<"Enter the number of rows in the first matrix:";
cin >> row;
cout << "Enter the number of collumns in the first matrix:";
cin >> collumn;
int matrix[row][collumn];
cout << "Enter all the elements in the matrix:" << endl;
for(int i = 0; i < row; i++){
for(int j = 0; j < collumn; j++){
cin >> matrix[i][j];
sum = sum + matrix[i][j];
}
}
cout << "---THE FIRST MATRIX--- \n";
for(int i = 0; i < row; i++){
for(int j = 0; j < collumn; j++){
cout << matrix[i][j] << " ";
}
cout << "\n";
}
cout<<"Enter the number of rows in the second matrix:";
cin >> row1;
cout << "Enter the number of collumns in the second matrix:";
cin >> collumn1;
int matrix1[row1][collumn1];
cout << "Enter all the elements in the matrix:" << endl;
for(int i = 0; i < row1; i++){
for(int j = 0; j < collumn1; j++){
cin >> matrix1[i][j];
sum1 = sum1 + matrix1[i][j];
}
}
cout << "---THE SECOND MATRIX--- \n";
for(int i = 0; i < row1; i++){
for(int j = 0; j < collumn1; j++){
cout << matrix1[i][j] << " ";
}
cout << "\n";
}
cout << "The sum of all elements in the first matrix is " << sum << endl;
cout << "The sum of all elements in the second matrix is " << sum1 << endl;
cout << "The difference of the sum between the first and the second matrix " << (sum-sum1);
return 0;
}
Explanation: