Answer to Question #180622 in C++ for awais

Question #180622

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.


1
Expert's answer
2021-04-13T17:59:15-0400
#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'));
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS