For example, simply in the process of executing a program, write multMatrix (mat1, mat2, mat3) and then matrices of other sizes, say, multMatrix (mat4, mat5, mat6), but it turned out somehow.
#include "stdafx.h" #include <iostream> #include <Windows.h> using namespace std; const int commonROWSandCOLUMNS = 3; const int numofROWS = 3; const int numofCOLUMNS = 3; void multMatrix(int matrix1[numofROWS][commonROWSandCOLUMNS], int matrix2[commonROWSandCOLUMNS][numofCOLUMNS], int matrix3[numofROWS] [numofCOLUMNS]) { for (int i = 0; i < numofROWS; i++) { for (int i2 = 0; i2 < numofCOLUMNS; i2++) { for (int i3 = 0; i3 < commonROWSandCOLUMNS; i3++) { matrix3[i][i2] += matrix1[i][i3] * matrix2[i3][i2]; } } } } int matrix1[3][3] = { {9,3,4}, {2,8,2}, {1,5,6} }; int matrix2[3][3] ={ { 9,0,0 }, { 1,0,0 }, { 8,0,0} }; int matrix3[3][3]; int main() { multMatrix(matrix1,matrix2,matrix3); for (int i = 0; i < 3; i++) { for (int i2 = 0; i2 < 3; i2++) { cout << matrix3[i][i2]<<" "; } cout << endl; } system("pause"); return 0; }
Or tell me some library. Or should we use dynamic arrays?