Write an efficient C++ program to make a matrices calculator. Your program must be having the
following options.
1) Addition of two matrices
2) Subtraction of two matrices
3) Finding the transpose of a matrix
Elements and size of the matrix will be inputted by the user and there is no limit on the size of matrix.
Use the characters A, S and T for addition, subtraction and transpose respectively. Try to complete
your program in minimum possible lines.
Hints
• Use dynamic memory allocation and passing arrays as argument to functions will reduce the
lines of code significantly.
• Write a function for taking the input in a matrix.
• Write another function for displaying the elements of a matrix.
• If user selects addition or subtraction then your code must ask the user to enter the elements
for 2 matrices, otherwise ask the user to enter the elements for only one matrix.
• If user presses a wrong character, ask him/her to input again.
#include <iostream>
using namespace std;
int **input(int m, int n)
{
//allocating memory for 2d array
int **M = new int *[m];
for (int i = 0; i < m; i++)
M[i] = new int[n];
//reading elements
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> M[i][j];
return M;
}
//to print matrix
void output(int **M, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << M[i][j] << "\t";
}
cout << endl;
}
}
// to add matices
int **Adition(int **M, int **N, int m, int n)
{
//allocating memory for 2d array
int **O = new int *[m];
for (int i = 0; i < m; i++)
O[i] = new int[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
O[i][j] = M[i][j] + N[i][j];
return O;
}
int **Substraction(int **M, int **N, int m, int n)
{
//allocating memory for 2d array
int **O = new int *[m];
for (int i = 0; i < m; i++)
O[i] = new int[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
O[i][j] = M[i][j] - N[i][j];
return O;
}
int **Transpose(int **M, int m, int n)
{
//allocating memory for 2d array
int **T = new int *[m];
for (int i = 0; i < m; i++)
T[i] = new int[n];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
T[i][j] = M[j][i];
return T;
}
int main()
{
int **M, **N, **R, n, m;
char ch;
cout << "Choose One (A , S , T) " << endl;
cout << "1) Addition of two matrice" << endl;
cout << "2) Subtraction of two matrices" << endl;
cout << "3) Finding the transpose of a matrix" << endl;
do{
cin >> ch;
switch (ch)
{
case 'A':
case 'a':
cout << "Enter number of rows and columns :";
cin >> m >> n;
cout << "Enter elements for Matrix 1: " << endl;
M = input(m, n);
cout << "Enter elements for Matric 2: " << endl;
N = input(m, n);
R = Adition(M, N, m, n);
cout << "Addition of Matrices :" << endl;
output(R, m, n);
break;
case 'S':
case 's':
cout << "Enter number of rows and columns :";
cin >> m >> n;
cout << "Enter elements for Matrix 1: " << endl;
M = input(m, n);
cout << "Enter elements for Matric 2: " << endl;
N = input(m, n);
R = Substraction(M, N, m, n);
cout << "Substraction of Matrices :" << endl;
output(R, m, n);
break;
case 'T':
case 't':
cout << "Enter number of rows and columns :";
cin >> m >> n;
cout << "Enter elements for Matrix : " << endl;
M = input(m, n);
R = Transpose(M, m, n);
cout << "Transppose of Matrix :" << endl;
output(R, m, n);
break;
default:
cout << "Wrong choise !! enter again" << endl;
break;
}
} while (!(ch == 'A' || ch == 'S' || ch == 'T' || ch == 'a' || ch == 's' || ch == 't'));
}
Comments
Leave a comment