/* Nome: Mario Cognome: Montanari Somma 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 sumMatrices(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); sumMatrices(matrix1, matrix2, result, NROW, NCOL); cout << "Sum 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 sumMatrices(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] = matrix1[i][j] + matrix2[i][j]; } } }