/* Nome: Mario Cognome: Montanari Moltiplicazione tra due matrici */ #include #include #include #include #include #define NROW 2 #define NCOL 2 #define VMIN -5 #define VMAX 5 using namespace std; void fillMatrix(int matrix[][NCOL], int nrow, int ncol); void printMatrix(int matrix[][NCOL], int nrow, int ncol); void multiplyMatrices(int matrix1[][NCOL], int matrix2[][NCOL], int result[][NCOL], int nrow, int ncol); int main(void) { int matrix1[NROW][NCOL]; int matrix2[NROW][NCOL]; int result[NROW][NCOL]; fillMatrix(matrix1, NROW, NCOL); fillMatrix(matrix2, NROW, NCOL); cout << "First Matrix: " << endl; printMatrix(matrix1, NROW, NCOL); cout << "Second Matrix: " << endl; printMatrix(matrix2, NROW, NCOL); multiplyMatrices(matrix1, matrix2, result, NROW, NCOL); cout << "Product of the Matrices: " << endl; printMatrix(result, NROW, NCOL); return 0; } void fillMatrix(int matrix[][NCOL], int nrow, int ncol) { srand(time(NULL)); for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN; } } } void printMatrix(int matrix[][NCOL], int nrow, int ncol) { for (int i = 0; i < nrow; i++) { cout << endl; for (int j = 0; j < ncol; j++) { cout << setw(5) << matrix[i][j]; } cout << endl; } cout << endl << endl << endl; } void multiplyMatrices(int matrix1[][NCOL], int matrix2[][NCOL], int result[][NCOL], int nrow, int ncol) { for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { result[i][j] = 0; } } for (int i = 0; i < nrow; i++) { for (int j = 0; j < ncol; j++) { for (int k = 0; k < ncol; k++) { result[i][j] = result[i][j] + matrix1[i][k] * matrix2[k][j]; } } } }