Subtraction of matrices in C++
#include <iostream>
using namespace std;
void subtract(int[5][5], int[5][5], int, int);
int display(int[5][5], int, int);
int main()
{
int a[5][5], b[5][5], r1, c1, r2, c2;
cout<<"\n Enter rows for first matrix: ";
cin>>r1;
cout<<"\n Enter columns for second matrix: ";
cin>>c1;
cout<<"\n Enter rows for first matrix: ";
cin>>r2;
cout<<"\n Enter columns for second matrix: ";
cin>>c2;
// To check if columns of first matrix are equal to rows of second matrix
if (r1 != r2 || c1!=c2)
return 0;
// Storing elements of first matrix.
printf("\nEnter elements of first matrix \n");
for(int i=0; i<r1; i++)
{
for(int j=0; j<c1; j++)
cin>>a[i][j];
}
// Storing elements of second matrix.
cout<<"\nEnter elements of second matrix\n";
for(int i=0; i<r2; i++)
{
for(int j=0; j<c2; j++)
cin>>b[i][j];
}
cout<<"\n display a\n";
display(a, r1, c1);
cout<<"\n display a\n";
display(b, r2, c2);
//calling the function to subtract a and b. passing number of rows
//and columns in both of them
subtract(a, b, r1, c1);
return 0;
}
Output:
Enter rows for first matrix: 3
Enter columns for second matrix: 3
Enter rows for first matrix: 3
Enter columns for second matrix: 3
Enter elements of first matrix
76 35 24
24 56 24
86 25 75
Enter elements of second matrix
5 7 8
7 3 6
6 3 7
display a
76 35 24
24 56 24
86 25 75
display b
5 7 8
7 3 6
6 3 7
Matrix c after Subtraction is: 71 28 16 17 53 18 80 22 68
