MULTIPLYING TWO MATRICES


#include "stdafx.h"
#include "iostream"
#define M 3
#define P 4
#define N 2
using namespace std;

void main(){
       int i, j, k;
       int **a, **b, **c;
       a = new int *[M + 1];
       b = new int *[P + 1];
       c = new int *[M + 1];
       for (i = 1; i <= M; i++)
             a[i] = new int[P + 1];
       for (j = 1; j <= P; j++)
             b[j] = new int[N + 1];
       for (k = 1; k <= M; k++)
             c[k] = new int[N + 1];
       cout << "Enter values of the matrix A[3x3]" << "\n";
       cin >> a[1][1]; cin >> a[1][2]; cin >> a[1][3];
       cin >> a[2][1]; cin >> a[2][2]; cin >> a[2][3];
       cin >> a[3][1]; cin >> a[3][2]; cin >> a[3][3];
       cout << "Matrix A: " << endl;
       for (i = 1; i <= M; i++){
             for (j = 1; j <= P; j++)
                    cout << a[i][j] << " ";
             cout << endl;
       }
       cout << "Enter values of the matrix B[2X3]" << "\n";
       cin >> b[1][1]; cin >> b[1][2];
       cin >> b[2][1]; cin >> b[2][2];
       cin >> b[3][1]; cin >> b[3][2];
       cin >> b[4][1]; cin >> b[4][2];
       cout << endl << "Matrix B: " << endl;
       for (i = 1; i <= P; i++){
             for (j = 1; j <= N; j++)
                    cout << b[i][j] << " ";
             cout << endl;
       }
       cout << endl << "Matrix C (AXB):" << endl;
       for (i = 1; i <= M; i++){
             for (j = 1; j <= N; j++){
                    c[i][j] = 0;
                    for (k = 1; k <= P; k++)
                           c[i][j] += a[i][k] * b[k][j];
                    cout << c[i][j] << " ";
             }
             cout << endl;
       }
       for (i = 1; i <= M; i++)
             delete a[i];
       for (j = 1; j <= P; j++)
             delete b[j];
       for (k = 1; k <= M; k++)
             delete c[k];
       delete a, b, c;
       system("pause");
}



Share on Google Plus