150k views
4 votes
6. Write a program to compute the sum and difference of two matrices which have the same order. Have the user enter the order of the matrices and then have the user enter the elements in the two matrices, and then perform addition and subtraction of respective elements. You may have to use for loops to obtain the entries of the two matrices and also may have to use for loops to add/subtract the entries.

User Zae
by
6.4k points

1 Answer

5 votes

Answer:

see explaination

Step-by-step explanation:

#include<iostream>

using namespace std;

main()

{

int rows,columns,first[10][10], second[10][10], sum[10][10],difference[10][10],i,j;

cout << "Enter number of rows:";

cin >> rows;

cout << "Enter number of columns:";

cin>> columns;

cout << "Enter the elements of first matrix\\";

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

cin >> first[i][j];

}

cout << "Enter the elements of second matrix\\";

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

cin >> second[i][j];

}

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

sum[i][j] = first[i][j] + second[i][j];

}

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

sum[i][j] = first[i][j] + second[i][j];

}

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

difference[i][j] = first[i][j] - second[i][j];

}

cout << "Sum of entered matrices:-\\";

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

cout << sum[i][j] << "\t";

cout << endl;

}

cout << "Difference of entered matrices:-\\";

for ( i = 0 ; i < rows ;i++ )

{

for ( j = 0 ; j < columns ; j++ )

cout << difference[i][j] << "\t";

cout << endl;

}

return 0;

}

see attachment for output

User Jarz
by
5.5k points