/*
Program for Matrix Multiplication (Max order of matrix is 10x10).
*/
#include<iostream>
#include<iomanip>
using namespace std;
void inputMatrix(int[][10], int, int); //For inputting matrix
void displayMatrix(int[][10], int, int); //For displaying matrix
void size(int &, int &); //Gets matrix size
void line(); //For displaying Horizontal line
int main()
{
int row1, row2, column1, column2, sum = 0;
int matrix1[10][10], matrix2[10][10], result[10][10];
//For size of Matrix-1
cout << "Enter number of rows and columns in Matrix-1:\n";
size(row1, column1);
do
{
//For size of Matrix-2
cout << "\n\nEnter number of rows and columns in Matrix-2:\n";
size(row2, column2);
if (column1 != row2)
cout << "Invalid Input........\nNumber of columns of Matrix-1 is not equal to number of rows of Matrix-2\n";
} while (column1 != row2);
line();
//For Input of Matrix-1
cout << "Enter elements of Matrix-1:\n\n";
inputMatrix(matrix1,row1,column1);
line();
//For Input of Matrix-2
cout << "Enter elements of Matrix-2:\n\n";
inputMatrix(matrix2, row2, column2);
//For Matrix Multiplication
for (int r = 0; r < row1; r++)
{
for (int c = 0; c < column2; c++)
{
sum = 0;
for (int i = 0; i < column1; i++)
sum += (matrix1[r][i] * matrix2[i][c]);
result[r][c] = sum;
}
}
system("cls"); //Clears Console
//displays matrix-1
cout << "Matrix - 1:\n";
displayMatrix(matrix1, row1, column1);
line();
//displays matrix-2
cout << "Matrix - 2:\n";
displayMatrix(matrix2, row2, column2);
//for formatting output
line();
for (int i = 0; i < 40; i++)
cout << "**";
//Displays Multiplication
cout << "MULTIPLICATION of Matrix - 1 and Matrix - 2 is:\n";
displayMatrix(result, row1, column2);
cout << endl;
return 0;
}
void size(int &row,int &column)
{
do
{
cout << "Rows (Max 10):\t\t";
cin >> row;
if (row > 10 || row < 0)
cout << "Invalid input.......\n";
} while (row > 10 || row < 0);
do
{
cout << "Columns (Max 10):\t";
cin >> column;
if (column > 10 || column < 0)
cout << "Invalid input.......\n";
} while (column > 10 || column < 0);
}
//For inputting matrix
void inputMatrix(int matrix[][10], int row, int column)
{
for (int r = 0; r < row; r++)
{
for (int c = 0; c < column; c++)
{
cout << "Enter element at index [" << r << "][" << c << "]?\t";
cin >> matrix[r][c];
}
}
}
void line() //Displays horizontal Line
{
cout << endl;
//for formatting output
for (int i = 0; i < 80; i++)
cout << "_";
}
void displayMatrix(int matrix[][10], int row, int column)
{
cout << endl;
for (int r = 0; r < row; r++)
{
cout << "\t";
for (int c = 0; c < column; c++)
cout << matrix[r][c] << "\t";
cout << endl<<endl;
}
}