/* Calcola la trasposta di una matrice le cui dimensioni ed elementi sono inseriti dall'utente. */ #include #include // direttive: definisce un identificatore e una sequenza di caratteri // che sostituira' l'identificatore ogni volta che questo ricorre // nel file sorgente. Il compilatore sostituisce le parole // MAX_RIGHE e MAX_COLONNE con il valore 10 ogni volta che queste // compaiono nel file sorgente. #define MAX_RIGHE 10 #define MAX_COLONNE 10 int main() { // Richiesta delle dimensioni. int num_righe; do { cout << "Numero di righe (max 10): "; cin >> num_righe; } while (num_righe < 1 || num_righe > MAX_RIGHE); int num_colonne; do { cout << "Numero di colonne (max 10): "; cin >> num_colonne; } while (num_colonne < 1 || num_colonne > MAX_COLONNE); int mat[num_righe][num_colonne]; // Caricamento della matrice for (int i = 0; i < num_righe; i++) for (int j = 0; j < num_colonne; j++) { cout << "Inserisci l'elemento (" << i << ", " << j << "): "; cin >> mat[i][j]; } // Stampa la matrice. cout << endl << "\t *** MATRICE ***" << endl; for (int i = 0; i < num_righe; i++) { cout << endl; for (int j = 0; j < num_colonne; j++) cout << setw(6) << mat[i][j]; } cout << endl; int mat_trasposta[num_colonne][num_righe]; // Calcolo della trasposta. for (int i = 0; i < num_righe; i++) for (int j = 0; j < num_colonne; j++) mat_trasposta[j][i] = mat[i][j]; // Stampa la matrice trasposta. cout << endl << "\t *** MATRICE TRASPOSTA ***" << endl; for (int i = 0; i < num_colonne; i++) { cout << endl; for (int j = 0; j < num_righe; j++) cout << setw(6) << mat_trasposta[i][j]; } cout << endl << endl; return 0; }