Created
May 17, 2020 13:30
-
-
Save marco97pa/7096c2cce9c4ecffce130e1264ea829c to your computer and use it in GitHub Desktop.
Matrici quadrate: come dichiarare, caricare, stampare una matrice o solo la diagonale in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(){ | |
int N, i, j; | |
//Dichiarazione matrice | |
int m[10][10]; | |
//Scelta della dimensione | |
do{ | |
cout<<"Quante righe/colonne?"; | |
cin>>N; | |
//Controlla che la dimensione (N) non sia superiore a 10 o inferiore a 1 | |
}while (N < 1 || N > 10); | |
/*INPUT - Caricamento di una matrice*/ | |
cout<<endl<<"INSERISCI ELEMENTI MATRICE"<<endl; | |
for(i = 0; i < N; i++){ | |
for (j = 0; j < N; j++){ | |
cin>>m[i][j]; | |
} | |
} | |
/*OUTPUT - Stampa di una matrice*/ | |
cout<<endl<<"STAMPA ELEMENTI MATRICE"<<endl; | |
for(i = 0; i < N; i++){ | |
for (j = 0; j < N; j++){ | |
cout<<m[i][j]<<"\t"; | |
} | |
cout<<endl; | |
} | |
/*OUTPUT - Stampa la diagonale della matrice quadrata*/ | |
cout<<endl<<"STAMPA DIAGONALE MATRICE"<<endl; | |
for(i = 0; i < N; i++){ | |
cout<<m[i][i]<<"\t"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Risultato finale: