Question 02 of 02

Matrix Addition and Transpose using 2D Arrays

Write a Turbo C++ program to read two matrices of order R x C, compute their Matrix Addition (Sum Matrix), and find the Transpose of the first matrix.

C++
#include <iostream.h>
#include <conio.h>

void main() {
    int a[10][10], b[10][10], sum[10][10], trans[10][10];
    int r, c, i, j;

    clrscr();

    cout << "Enter number of rows (r): ";
    cin >> r;
    cout << "Enter number of columns (c): ";
    cin >> c;

    cout << "\nEnter elements of Matrix A (" << r << "x" << c << "):" << endl;
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            cin >> a[i][j];
        }
    }

    cout << "\nEnter elements of Matrix B (" << r << "x" << c << "):" << endl;
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            cin >> b[i][j];
        }
    }

    // Matrix Addition & Transpose calculation
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            sum[i][j] = a[i][j] + b[i][j];
            trans[j][i] = a[i][j];
        }
    }

    cout << "\n--- Sum Matrix (A + B) ---" << endl;
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++) {
            cout << sum[i][j] << "\t";
        }
        cout << endl;
    }

    cout << "\n--- Transpose of Matrix A (" << c << "x" << r << ") ---" << endl;
    for (i = 0; i < c; i++) {
        for (j = 0; j < r; j++) {
            cout << trans[i][j] << "\t";
        }
        cout << endl;
    }

    getch();
}
Question 2 of 2