In: Computer Science
Write Matrix Addition 2 D (dimensional) Array program in c++.
#include <iostream> using namespace std; void matrixAdd(int** A, int** B, int** R1, int r, int c){ int i,j; for(i = 0;i<r;i++){ for(j = 0;j<c;j++){ R1[i][j] = A[i][j] + B[i][j]; } } } int main(){ int rows, columns; cout<<"Enter number of rows: "; cin>>rows; cout<<"Enter number of columns: "; cin>>columns; int** arr1 = new int*[rows]; for(int i = 0; i < rows; ++i) arr1[i] = new int[columns]; int** arr2 = new int*[rows]; for(int i = 0; i < rows; ++i) arr2[i] = new int[columns]; int** res = new int*[rows]; for(int i = 0; i < rows; ++i) res[i] = new int[columns]; cout<<"Enter "<<rows<<" x "<<columns<<" matrix 1: "<<endl; for(int i = 0;i<rows;i++){ for(int j = 0;j<columns;j++){ cin>>arr1[i][j]; } } cout<<"\nEnter "<<rows<<" x "<<columns<<" matrix 2: "<<endl; for(int i = 0;i<rows;i++){ for(int j = 0;j<columns;j++){ cin>>arr2[i][j]; } } matrixAdd(arr1,arr2,res,rows,columns); cout<<"\nAddition matrix:\n"; for(int i = 0;i<rows;i++){ for(int j = 0;j<columns;j++){ cout<<res[i][j]<<" "; } cout<<endl; } return 0; }