School-Coding-Cpp/sfusi/matrice_trasposta.cpp

72 lines
1.7 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 13/12/2024
Genera una Matrice e poi ne stampa la Trasposta.
*/
#include <iostream>
#include <array>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#define RIGHE 3
#define COLONNE 3
#define MINIMO -9
#define MASSIMO 9
using namespace std;
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne);
void matriceTrasposta(int matrice[RIGHE][COLONNE], int temp[COLONNE][RIGHE], int righe, int colonne);
int main(void) {
int matrice[RIGHE][COLONNE];
int temp[COLONNE][RIGHE];
int righe = RIGHE;
int colonne = COLONNE;
riempiMatrice(matrice, righe, colonne);
cout << "Matrice: " << endl,
stampaMatrice(matrice, righe, colonne);
matriceTrasposta(matrice, temp, righe, colonne);
cout << "Matrice trasposta: " << endl;
stampaMatrice(temp, righe, colonne);
return 0;
}
void riempiMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
srand(time(NULL));
for (int r = 0; r < righe; r++) {
for (int c = 0; c < colonne; c++) {
matrice[r][c] = rand() % (MASSIMO - MINIMO + 1) + MINIMO;
}
}
}
void stampaMatrice(int matrice[RIGHE][COLONNE], int righe, int colonne) {
for (int r = 0; r < righe; r++) {
cout << endl;
for (int c = 0; c < colonne; c++) {
cout << setw(5) << matrice[r][c];
}
cout << endl;
}
cout << endl << endl << endl;
}
void matriceTrasposta(int matrice[RIGHE][COLONNE], int temp[COLONNE][RIGHE], int righe, int colonne) {
for (int r = 0; r < righe; r++) {
for (int c = 0; c < colonne; c++) {
temp[c][r] = matrice[r][c];
}
}
}