2D Array.docx

3
2D Arrays: Q.Write a Program to add two matrices(2D).Inputs Order of Matrices(i.e Rows and Colums). The Matrices must be of Same size they can be ADD up.Then Inputs Elements of first and Second Matrix and Display them. After that Add them in another matrix and display the Sum. #include <iostream> using namespace std; int main() { int i,j,r1,r2,c1,c2,matrix1[5][5],matrix2[5][5],sum[5][5]; do{ cout<<"Enter the rows of first matrix:"; cin>>r1; cout<<"Enter the columns of first matrix:"; cin>>c1; cout<<"Enter the rows of second matrix:"; cin>>r2; cout<<"Enter the columns of second matrix:"; cin>>c2; if((r1!=r2)||(c1!=c2)) { cout<<"Addition not Possible, Enter Rows and Colums Again."<<endl; } }while((r1!=r2)||(c1!=c2)); //*******Entering of First Matrix**********// for(i=0;i<r1;i++) { for(j=0;j<c1;j++) { cout<<"Enter the element for the row "<<i+1<<" and colum "<<j+1; cin>>matrix1[i][j]; } } //*********Display of First Matrix**************// cout<<"****MATRIX 1****"<<endl; for(i=0;i<r1;i++) { for(j=0;j<c1;j++)

description

Program to add two matrices(2D).Inputs Order of Matrices(i.e Rows and Colums).The Matrices must be of Same size they can be ADD up.Then Inputs Elements of first and Second Matrix and Display them. After that Add them in another matrix and display the Sum.

Transcript of 2D Array.docx

Page 1: 2D Array.docx

2D Arrays:

Q.Write a Program to add two matrices(2D).Inputs Order of Matrices(i.e Rows and Colums).The Matrices must be of Same size they can be ADD up.Then Inputs Elements of first and Second Matrix and Display them. After that Add them in another matrix and display the Sum.

#include <iostream>using namespace std;int main(){int i,j,r1,r2,c1,c2,matrix1[5][5],matrix2[5][5],sum[5][5];do{cout<<"Enter the rows of first matrix:";cin>>r1;cout<<"Enter the columns of first matrix:";cin>>c1;cout<<"Enter the rows of second matrix:";cin>>r2;cout<<"Enter the columns of second matrix:";cin>>c2;if((r1!=r2)||(c1!=c2)){cout<<"Addition not Possible, Enter Rows and Colums Again."<<endl;}}while((r1!=r2)||(c1!=c2));//*******Entering of First Matrix**********//for(i=0;i<r1;i++){for(j=0;j<c1;j++){cout<<"Enter the element for the row "<<i+1<<" and colum "<<j+1;cin>>matrix1[i][j];}}//*********Display of First Matrix**************//cout<<"****MATRIX 1****"<<endl;for(i=0;i<r1;i++){for(j=0;j<c1;j++){cout<<matrix1[i][j]<<"\t";}cout<<endl;}

//*******Entering of Second Matrix**********//

Page 2: 2D Array.docx

for(i=0;i<r2;i++){for(j=0;j<c2;j++){cout<<"Enter the element for the row "<<i+1<<" and colum "<<j+1;cin>>matrix2[i][j];}}//*********Display of Second Matrix**************//cout<<"****MATRIX 2****"<<endl;for(i=0;i<r1;i++){for(j=0;j<c1;j++){cout<<matrix2[i][j]<<"\t";}cout<<endl;}

//*********Calculating Sum of Both Matrices*********//for(i=0;i<r1;i++){for(j=0;j<c1;j++){sum[i][j]=matrix1[i][j]+matrix2[i][j];}}//********Displaying Sum of Both Matrices*********//cout<<"****SUM****"<<endl;for(i=0;i<r1;i++){for(j=0;j<c1;j++){cout<<sum[i][j]<<"\t";}cout<<endl;} return 0;}