//Cristian Ronzoni 3Ain // ruota il bordo di una matrice #include #define RIGHE 4 #define COLONNE 4 using namespace std; // Funzione per stampare la matrice void stampaMatrice(int matrice[RIGHE][COLONNE]) { for (int i = 0; i < RIGHE; ++i) { for (int j = 0; j < COLONNE; ++j) { cout << matrice[i][j] << " "; } cout << endl; } cout << endl; } // Funzione per ruotare il bordo della matrice in-place void ruotaBordoInPlace(int matrice[RIGHE][COLONNE]) { // Verifica che la matrice sia abbastanza grande per avere un bordo da ruotare if (RIGHE < 2 || COLONNE < 2) return; // Salva temporaneamente il primo elemento int temp = matrice[0][0]; // Spostamento orario in senso del bordo // Prima riga for (int j = 0; j < COLONNE - 1; ++j) { matrice[0][j] = matrice[0][j + 1]; } // Ultima colonna for (int i = 0; i < RIGHE - 1; ++i) { matrice[i][COLONNE - 1] = matrice[i + 1][COLONNE - 1]; } // Ultima riga for (int j = COLONNE - 1; j > 0; --j) { matrice[RIGHE - 1][j] = matrice[RIGHE - 1][j - 1]; } // Prima colonna for (int i = RIGHE - 1; i > 0; --i) { matrice[i][0] = matrice[i - 1][0]; } // Ripristina il primo elemento salvato in fondo alla colonna matrice[1][0] = temp; } int main() { // Matrice predefinita int matrice[RIGHE][COLONNE] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16} }; cout << "Matrice originale:\n"; stampaMatrice(matrice); // Ruota il bordo in senso orario in-place ruotaBordoInPlace(matrice); cout << "Matrice dopo la rotazione del bordo (in-place):\n"; stampaMatrice(matrice); return 0; }