76 lines
1.7 KiB
C++
76 lines
1.7 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
|
|
Moltiplicazione tra due matrici
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <array>
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
|
|
#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 powerMatrix(int matrix[][NCOL], int result[][NCOL], int nrow, int ncol);
|
|
|
|
int main(void) {
|
|
int matrix[NROW][NCOL];
|
|
int result[NROW][NCOL];
|
|
|
|
fillMatrix(matrix, NROW, NCOL);
|
|
|
|
cout << "Matrix: " << endl;
|
|
printMatrix(matrix, NROW, NCOL);
|
|
|
|
powerMatrix(matrix, result, NROW, NCOL);
|
|
|
|
cout << "Power of the Matrix: " << 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 powerMatrix(int matrix[][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] + matrix[i][k] * matrix[k][j];
|
|
}
|
|
}
|
|
}
|
|
} |