#include <stdio.h>
#include <windows.h>
template<typename TypeName> class Matrix{
public:
TypeName **matrix;
int _x, _y;
void Input(){
printf("Input matrix %dx%d:\n", _y, _x);
for(int y=0;y<_y;y++){
for(int x=0;x<_x;x++)
scanf("%d", &matrix[y][x]);
}
return;
}
void Print(){
for(int y=0;y<_y;y++){
for(int x=0;x<_x;x++)
printf("%d\t", matrix[y][x]);
printf("\n");
}
return;
}
Matrix(int y, int x){
_x=x;
_y=y;
matrix = new TypeName*[_y];
for(int i=0;i<_y;i++){
matrix[i]=new TypeName[_x];
}
return;
};
Matrix operator*(const Matrix &matr)const{
Matrix nmatrix(_y, matr._x);
for(int y=0;y<_y;y++){
for(int j=0;j<matr._x;j++){
nmatrix.matrix[y][j]=0;
for(int x=0;x<_x;x++){
nmatrix.matrix[y][j]+=matrix[y][x]*matr.matrix[x][j];
}
}
}
return nmatrix;
}
};
int main(void){
int y1, x1, y2, x2;//2 2 2 2 1 2 3 4 5 6 7 8
printf("Type size of first matrix:");
scanf("%d %d", &y1, &x1);
printf("Type size of second matrix:");
scanf("%d %d", &y2, &x2);
Matrix<int> mf(y1, x1), ms(y2, x2), mres(y1, x2);
mf.Input();
ms.Input();
if(mf._y==ms._x){
mres=mf*ms;
printf("Result is\n");
mres.Print();
}
else{
printf("Unable to multiply matrices.\n");
}
system("pause");
return 0;
}