#include<iostream>
using namespace std;
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
cout << "Enter the number of rows and columns of matrix : ";
cin >> m >> n;
cout << "Enter the elements of first matrix : \n";
cin >> m >> n;
cout << "Enter the elements of first matrix : \n";
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
cin >> first[c][d];
for (d = 0; d < n; d++)
cin >> first[c][d];
cout << "Enter the elements of second matrix : \n";
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
cin >> second[c][d];
for (d = 0; d < n; d++)
cin >> second[c][d];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
for (d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
cout << "Sum of entered matrices:-\n";
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
cout << sum[c][d] << "\t";
{
for (d = 0; d < n; d++)
cout << sum[c][d] << "\t";
cout << endl;
}
}
return 0;
}
}
Output :
Enter the number of rows and columns of matrix : 2 2 Enter the elements of first matrix : 8 1 2 3 Enter the elements of second matrix : 4 9 5 2 Sum of entered matrices:- 12 10 7 5
Matrix subtraction program
#include<iostream>
using namespace std;
int main()
{
int m, n, c, d, first[10][10], second[10][10], sub[10][10];
{
int m, n, c, d, first[10][10], second[10][10], sub[10][10];
cout << "Enter the number of rows and columns of matrix : ";
cin >> m >> n;
cout << "Enter the elements of first matrix : \n";
cin >> m >> n;
cout << "Enter the elements of first matrix : \n";
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
cin >> first[c][d];
for (d = 0; d < n; d++)
cin >> first[c][d];
cout << "Enter the elements of second matrix : \n";
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
cin >> second[c][d];
for (d = 0; d < n; d++)
cin >> second[c][d];
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
sub[c][d] = first[c][d] - second[c][d];
for (d = 0; d < n; d++)
sub[c][d] = first[c][d] - second[c][d];
cout << "Subtraction of entered matrices:-\n";
for (c = 0; c < m; c++)
{
for (d = 0; d < n; d++)
cout << sub[c][d] << "\t";
{
for (d = 0; d < n; d++)
cout << sub[c][d] << "\t";
cout << endl;
}
}
return 0;
}
}
Output :
Enter the number of rows and columns of matrix : 3 3 Enter the elements of first matrix : 7 6 4 1 9 8 2 3 0 Enter the elements of second matrix : 2 6 4 5 6 1 2 0 9 Subtraction of entered matrices:- 5 0 0 -4 3 7 0 3 -9
Matrix multiplication program
#include<iostream>
using namespace std;
int main()
{
{
int a[5][5], b[5][5], c[5][5], m, n, p, q, i, j, k;
cout << "Enter rows and columns of first matrix: ";
cin >> m >> n;
cin >> m >> n;
cout << "Enter rows and columns of second matrix: ";
cin >> p >> q;
cin >> p >> q;
if (n == p)
{
cout << "\nEnter first matrix:\n";
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
cin >> a[i][j];
{
cout << "\nEnter first matrix:\n";
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
cin >> a[i][j];
cout << "\nEnter second matrix:\n";
for (i = 0; i < p; ++i)
for (j = 0; j < q; ++j)
cin >> b[i][j];
for (i = 0; i < p; ++i)
for (j = 0; j < q; ++j)
cin >> b[i][j];
cout << "\nThe new matrix is:\n";
for (i = 0; i < m; ++i)
{
for (j = 0; j < q; ++j)
{
c[i][j] = 0;
for (k = 0; k < n; ++k)
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
cout << c[i][j] << "\t";
}
cout << "\n";
}
}
else
cout << "\nSorry!!!! Matrix multiplication can't be done";
{
for (j = 0; j < q; ++j)
{
c[i][j] = 0;
for (k = 0; k < n; ++k)
c[i][j] = c[i][j] + (a[i][k] * b[k][j]);
cout << c[i][j] << "\t";
}
cout << "\n";
}
}
else
cout << "\nSorry!!!! Matrix multiplication can't be done";
return 0;
}
}
Output :
Enter rows and columns of first matrix: 2 3 Enter rows and columns of second matrix: 3 2 Enter first matrix: 1 2 3 4 5 6 Enter second matrix: 1 2 3 4 5 6 The new matrix is: 22 28 49 64
0 Comments