School-Coding-Cpp/sfusi/scambia_la_prima_riga_con_l...

72 lines
1.7 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 13/12/2024
Genera una Matrice e poi scamba la prima riga della Matrice con l'ultima.
*/
#include <iostream>
#include <array>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#define RIGHE 4
#define COLONNE 4
#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 scambiaRiga(int matrice[RIGHE][COLONNE], int temp, int righe, int colonne);
int main(void) {
int matrice[RIGHE][COLONNE];
int righe = RIGHE;
int colonne = COLONNE;
int temp;
riempiMatrice(matrice, righe, colonne);
cout << "Matrice originale: " << endl,
stampaMatrice(matrice, righe, colonne);
scambiaRiga(matrice, temp, righe, colonne);
cout << "Matrice scambiata: " << endl;
stampaMatrice(matrice, 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 scambiaRiga(int matrice[RIGHE][COLONNE], int temp, int righe, int colonne) {
for (int c = 0; c < colonne; c++) {
temp = matrice[0][c];
matrice[0][c] = matrice[3][c];
matrice[3][c] = temp;
}
}