55 lines
1021 B
C++
55 lines
1021 B
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
|
|
Matrice Diagonale
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <array>
|
|
#include <ctime>
|
|
#include <iomanip>
|
|
|
|
#define NROW 4
|
|
#define NCOL 4
|
|
#define VMIN -50
|
|
#define VMAX 50
|
|
|
|
using namespace std;
|
|
|
|
void fillMatrix(int matrix[][NCOL], int nrow, int ncol);
|
|
void printMatrix(int matrix[][NCOL], int nrow, int ncol);
|
|
|
|
int main(void) {
|
|
int matrix[NROW][NCOL];
|
|
|
|
fillMatrix(matrix, NROW, NCOL);
|
|
printMatrix(matrix, 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++) {
|
|
if (i == j) {
|
|
matrix[i][j] = rand() % (VMAX - VMIN + 1) + VMIN;
|
|
} else {
|
|
matrix[i][j] = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
} |