Need a project done?

C++ Programming Developer

Search This Blog

Program for the addition of 2 matrices of any Order in C++

//Program for the addition of 2 matrices of any Order in C++.


#include <iostream>
using namespace std;

int main()
{
cout << "Size of Matrix A:";
int rows1, columns1;
cin>>rows1>>columns1;
//Initializing a 2-d dynamic array.
int ** m1 = new int *[rows1];

for (int i = 0; i<rows1; i++)
m1[i] = new int [columns1];

//Inputing Matrix.
cout << "Enter Matrix A\n";
for (int r = 0; r<rows1; r++)
for (int c = 0; c<columns1; c++)
cin>>m1[r][c];
//--------------------------------------
cout << "Size of Matrix B:";
int rows2, columns2;
cin>>rows2>>columns2;

int ** m2 = new int *[rows2];

for (i = 0; i<rows2; i++)
m2[i] = new int [columns2];

//Inputing Matrix.
cout << "Enter Matrix B\n";
for ( r = 0; r<rows2; r++)
for (int c = 0; c<columns2; c++)
cin>>m2[r][c];
//--------------------------------------

//Printing Matrix A.
cout << "\nA:\n";
for ( r = 0; r<rows1; r++)
{
for (int c = 0; c<columns1; c++)
cout << m1[r][c] << ' ';

cout << endl;
}

//Printing Matrix B.
cout << "\nB:\n";
for ( r = 0; r<rows2; r++)
{
for (int c = 0; c<columns2; c++)
cout << m2[r][c] << ' ';

cout << endl;
}


//A+B
cout << endl <<endl << "A+B =\n";
for (r = 0; r<rows1; r++)
{
for (int c = 0; c<columns1; c++)
cout << m1[r][c] + m2[r][c] << ' ';

cout << endl;
}


return 0;
}

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."