Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void matrix_multiply(int table1[][2], int rows1, int columns1, int table2[][2], int rows2, int columns2, int result[][2])
- {
- if (columns1 != rows2)
- {
- cout << "Matrix Multiplication not possible";
- }
- if (columns1 == rows2)
- {
- //If two matrices are being muiltiplied [m][n] and [n][p] then the resulting matrix is [m][p]
- //Start by initializing array at 0
- for (int m = 0; m < rows1; m++)
- {
- for (int p = 0; p < columns2; p++)
- {
- result[m][p] = 0;
- }
- }
- for (int i = 0; i < rows1; i++)
- {
- for (int j = 0; j < columns2; j++)
- {
- for (int k = 0; k < columns1; k++)
- result[i][j] += table1[i][k] * table2[k][j];
- }
- }
- }
- }
- void print_matrix(int matrix[][2], int rows, int columns)
- {
- for (int i = 0; i < rows; i++)
- {
- for (int j = 0; j < columns; j++)
- {
- cout << matrix[i][j] << " ";
- }
- }
- }
- int main()
- {
- int table1[2][2];
- table1[0][0] = 1;
- table1[0][1] = 2;
- table1[1][0] = 3;
- table1[1][1] = 4;
- print_matrix(table1, 2, 2);
- cout << endl;
- int table2[2][2];
- table2[0][0] = 1;
- table2[0][1] = 2;
- table2[1][0] = 2;
- table2[1][1] = 1;
- print_matrix(table2, 2, 2);
- cout << endl;
- int result[2][2]; // If two matrices are being multiplied [m][n] and [n][p]. [n] and [n] must be the same. The resulting Matrix is [m][p].
- matrix_multiply(table1, 2, 2, table2, 2, 2, result);
- print_matrix(result, 2, 2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement