#include <iostream>
const int MAX_SIZE = 100;
void multiplyMatrix(int firstMatrix[MAX_SIZE][MAX_SIZE], int secondMatrix[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowsFirst, int colsFirst, int rowsSecond, int colsSecond);
int main() {
int rowsFirst, colsFirst, rowsSecond, colsSecond;
// Input dimensions of the first matrix
std::cout << "Enter rows and columns for the first matrix: ";
std::cin >> rowsFirst >> colsFirst;
// Input elements of the first matrix
int firstMatrix[MAX_SIZE][MAX_SIZE];
std::cout << "Enter elements of matrix 1:" << std::endl;
for (int i = 0; i < rowsFirst; ++i)
for (int j = 0; j < colsFirst; ++j)
std::cin >> firstMatrix[i][j];
// Input dimensions of the second matrix
std::cout << "Enter rows and columns for the second matrix: ";
std::cin >> rowsSecond >> colsSecond;
// Input elements of the second matrix
int secondMatrix[MAX_SIZE][MAX_SIZE];
std::cout << "Enter elements of matrix 2:" << std::endl;
for (int i = 0; i < rowsSecond; ++i)
for (int j = 0; j < colsSecond; ++j)
std::cin >> secondMatrix[i][j];
// Check if matrices can be multiplied
if (colsFirst != rowsSecond) {
std::cout << "Matrices cannot be multiplied!";
return 0;
}
// Multiply matrices and store the result in 'result' matrix
int result[MAX_SIZE][MAX_SIZE];
multiplyMatrix(firstMatrix, secondMatrix, result, rowsFirst, colsFirst, rowsSecond, colsSecond);
// Display the result
std::cout << "Product of matrices:" << std::endl;
for (int i = 0; i < rowsFirst; ++i) {
for (int j = 0; j < colsSecond; ++j)
std::cout << result[i][j] << " ";
std::cout << std::endl;
}
return 0;
}
void multiplyMatrix(int firstMatrix[MAX_SIZE][MAX_SIZE], int secondMatrix[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rowsFirst, int colsFirst, int rowsSecond, int colsSecond) {
// Initializing elements of result matrix to 0
for (int i = 0; i < rowsFirst; ++i)
for (int j = 0; j < colsSecond; ++j)
result[i][j] = 0;
// Multiplying firstMatrix and secondMatrix and storing in result
for (int i = 0; i < rowsFirst; ++i)
for (int j = 0; j < colsSecond; ++j)
for (int k = 0; k < colsFirst; ++k)
result[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}