Saturday, June 20, 2020

Matrix Addition

Given two matrices. Print the sum of two matrices.

Input Format:

Enter the row and column of a matrix Enter the matrix

Output Format:

Addition of a matrix

Sample Input:

2

2

1

2

3

4

1

2

3

4

Sample Output:

2 4

6 8

SOLUTION:

#include<iostream>

using namespace std;

int main(){

int r, c, mat1[100][100], mat2[100][100], sum[100][100], i, j;

cin >> r >> c;

//1st matrix

for(i=0; i<r; ++i){

for(j=0; j<c; ++j){

cin >> mat1[i][j];

}

}

//2nd matrix

for(i=0; i<r; ++i){

for(j=0; j<c; ++j){

cin >> mat2[i][j];

}

}

// Adding Two matrices

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

for(j=0;j<c;++j){

sum[i][j]=mat1[i][j]+mat2[i][j];

}

 // print the result

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

for(j=0;j<c;++j){

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

if(j==c-1){

cout << "\n";

}

}

return 0;

}


No comments:

Post a Comment

horizontal ads