/* Rotation horaire de 90° autour du centre */ /* des valeurs contenues dans une marice */ /* carree de int */ public class RotationMatrice { /* Fonction de creation d'une matrice de int */ /* initialise avec des nombres aleatoires */ /* compris dans l'intervalle [0,max] */ static int [][] initRandMatrice(int taille,int max) { int [][] tab = new int[taille][taille]; for ( int i = 0 ; i < taille ; i++ ) { for ( int j = 0 ; j < taille ; j++ ) { tab[i][j] =(int) (Math.random()*(max+1)); } } return tab; } /* Fonction d'affichage des valeurs contenues */ /* dans une matrice de int */ static void affichageMatrice(int [][] t) { int n = t.length; int m = t[0].length; for ( int i = 0 ; i < n ; i++ ) { for ( int j = 0 ; j < m ; j++ ) { Ecran.formater("%5d",t[i][j]); } Ecran.sautDeLigne(); } } ///////////////////////////////////////////////// /* Fonction de rotation de 90° en sens horaire */ /* autour du centre de 4 valeurs contenues */ /* dans une matrice carree de int */ static void rotation(int [][] m,int l,int c) { int n = m.length; int aux = m[n-1-c][l]; m[n-1-c][l] = m[n-1-l][n-1-c]; m[n-1-l][n-1-c] = m[c][n-1-l]; m[c][n-1-l] = m[l][c]; m[l][c] = aux; } /* Fonction de rotation de 90° en sens horaire */ /* autour du centre des valeurs contenues */ /* dans une matrice carree de int */ static void rotation(int [][] m) { int n = m.length; for ( int i = 0 ; i < n/2 ; i++ ) { for ( int j = 0 ; j < (n+1)/2 ; j++ ) { rotation(m,i,j); } } } ///////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { int taille; Ecran.afficher("Nombre de lignes et de colonnes ? "); taille = Clavier.saisirInt(); int [][] m = initRandMatrice(taille,999); Ecran.sautDeLigne(); affichageMatrice(m); rotation(m); Ecran.sautDeLigne(); Ecran.afficherln("Rotation"); affichageMatrice(m); } }