Write a menu driven program to perform following: a) Input a matrix b) Display matrix c) Add two matrix d) Multiply two matrixes e) Transpose a matrix
#include <iostream>
using namespace std;
class menu
{
private:
int a[10][10];
int b[10][10],sum[10][10];
int i, j, n;
public:
void inputmatrix();
void displaymatrix();
void addmatrix();
void mulmatrix();
void transposematrix();
};
void menu ::inputmatrix()
{
cout << "enter the size of matrix : " << endl;
cin >> n;
cout << "enter the elements of the first matrix : " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
cout << "enter the elements of the second matrix : " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cin >> b[i][j];
}
}
}
void menu ::displaymatrix()
{
inputmatrix();
cout << "yr first matrix is : " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " << a[i][j];
}
cout << " " << endl;
}
cout << "yr second matrix is : " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " << b[i][j];
}
cout << " " << endl;
}
}
void menu ::addmatrix()
{
inputmatrix();
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = a[i][j] + b[i][j];
}
}
cout << " the sum of two matrix is :" << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " << sum[i][j];
}
cout << " " << endl;
}
}
void menu ::mulmatrix()
{
inputmatrix();
int mul[n][n], k, c[i][j];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
sum[i][j] = 0;
for (k = 0; k < n; k++)
{
sum[i][j] = sum[i][j] + a[i][k] * b[k][j];
c[i][j] = sum[i][j];
}
}
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " << c[i][j];
}
cout << " " << endl;
}
}
void menu ::transposematrix()
{
cout << " enter the size of a matrix :" << endl;
cin >> n;
cout << "enter the elements of matrix :" << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cin >> a[i][j];
}
}
cout << "yr matrix is : " << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " << a[i][j];
}
cout << " " << endl;
}
cout<<"the transpose of your matrix is :"<<endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
cout << " " <<a[j][i];
}
cout << " " << endl;
}
}
int main()
{
menu d;
int number;
cout << "choose a option :" << endl;
cout << "1. input a matrix \n2. display two matrix \n3. add two matrix \n4. multiply two matrix \n5. transpose a matrix " << endl;
cin >> number;
switch (number)
{
case 1:
d.inputmatrix();
break;
case 2:
d.displaymatrix();
break;
case 3:
d.addmatrix();
break;
case 4:
d.mulmatrix();
break;
case 5:
d.transposematrix();
break;
default:
break;
}
}
output :
choose a option :
1. input a matrix
2. display two matrix
3. add two matrix
4. multiply two matrix
5. transpose a matrix
4
enter the size of matrix :
2
enter the elements of the first matrix :
1
2
1
2
enter the elements of the second matrix :
2
1
2
1
6 3
6 3
Comments
Post a Comment