Concetti Chiave
- Il programma include funzioni per dimensionare, caricare e stampare matrici, con dimensioni massime definite da una costante MAX.
- La funzione Dimensionamento richiede all'utente di inserire i valori delle dimensioni della matrice, garantendo che siano validi (tra 1 e 50).
- La funzione CaricaMatrice consente di popolare la matrice con valori forniti dall'utente, iterando attraverso le righe e le colonne.
- La funzione MatriceUnitaria verifica se la matrice è unitaria, controllando che gli elementi diagonali siano 1 e tutti gli altri siano 0.
- Se la matrice non è quadrata, il programma segnala che non è possibile verificarne la proprietà unitaria.
#include
#define MAX 50
using namespace std;
Funzioni di dimensionamento e caricamento
int Dimensionamento();
void CaricaMatrice(int M[MAX][MAX], int r, int c);
void MatriceUnitaria(int M[MAX][MAX], int r, int c);
void Stampa(int M[MAX][MAX], int r, int c);
int main()
{
int r, c, M[MAX][MAX];
cout
r=Dimensionamento();
cout
c=Dimensionamento();
CaricaMatrice(M, r, c);
if(r==c){
MatriceUnitaria(M, r, c);
}
else {
cout
}
Stampa(M, r, c);
}
int Dimensionamento()
{
int d;
do{
cin>>d;
} while ((dMAX));
return d;
}
void CaricaMatrice(int M[MAX][MAX], int r, int c)
{
for(int i=0; i
for(int q=0; q
cin>>M[q];
}
}
}
void MatriceUnitaria(int M[MAX][MAX], int r, int c)
{
int cont=0, cont0=0, tot;
for (int i=0; i
for(int j=0; j
if((i==j)&&(M[j]==1)){
cont++;
}
if(M[j]==0){
cont0++;
}
}
}
tot=r*c-r;
if((cont==r)&&(cont0==tot)){
cout
}
else{
cout
}
}
void Stampa(int M[MAX][MAX], int r, int c)
{
for (int i=0; i
for (int q=0; q
cout
cout[q];
} cout
}
}
Domande da interrogazione
- What is the purpose of the `Dimensionamento` function in the code?
- How does the program determine if a matrix is a unit matrix?
- What happens if the matrix is not square?
The `Dimensionamento` function is designed to prompt the user to input a dimension value for the matrix, ensuring it is within the valid range (greater than 0 and less than or equal to MAX, which is 50).
The program uses the `MatriceUnitaria` function to check if the matrix is a unit matrix by verifying that all diagonal elements are 1 and all off-diagonal elements are 0. It counts the number of 1s on the diagonal and 0s elsewhere, comparing these counts to the expected values for a unit matrix.
If the matrix is not square (i.e., the number of rows does not equal the number of columns), the program does not attempt to check for a unit matrix and instead outputs a message indicating that the matrix is not square.