/* Manipulations sur une matrice de double */ public class ManipulationsMatriceReel { /* Methode de creation d'une matrice de double */ /* initialise avec des nombres aleatoires */ /* compris dans l'intervalle [10.0,10.0+max] */ /* et un chiffre apres la virgule */ static double [][] initRandMatrice(int lignes, int colonnes, double max) { int i; int j; double [][] tab = new double[lignes][colonnes]; for ( i = 0 ; i < lignes ; i = i+1 ) { for ( j = 0 ; j < colonnes ; j = j+1 ) { tab[i][j] = ((int) (100.0+Math.random()*max*10.0))/10.0; } } return tab; } /* Methode d'affichage des valeurs contenues */ /* dans une matrice de double */ static void affichageMatrice(double [][] t) { int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { Ecran.afficher(t[i][j]," "); } Ecran.sautDeLigne(); } } ///////////////////////////////////////////////// /* Methode de copie d'une matrice de double */ /* dans une matrice de double supposee */ /* de taille identique */ static void copie(double [][] src,double [][] dst) { int i; int j; for ( i = 0 ; i < src.length ; i = i+1 ) { for ( j = 0 ; j < src[0].length ; j = j+1 ) { dst[i][j] = src[i][j]; } } } /* Methode de transposition d'une matrice */ /* de double dans une matrice de double */ /* supposee de taille compatible */ static void transposition(double [][] src,double [][] dst) { int i; int j; for ( i = 0 ; i < src.length ; i = i+1 ) { for ( j = 0 ; j < src[0].length ; j = j+1 ) { dst[j][i] = src[i][j]; } } } /* Methode de transposition d'une matrice */ /* carree de double */ static void transposition(double [][] m) { int i; int j; double aux; for ( i = 0 ; i < m.length-1 ; i = i+1 ) { for ( j = i+1 ; j < m.length ; j = j+1 ) { aux = m[i][j]; m[i][j] = m[j][i]; m[j][i] = aux; } } } ///////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { double [][] m = initRandMatrice(5,7,90.0); Ecran.afficherln("Matrice 5x7 initiale"); affichageMatrice(m); Ecran.sautDeLigne(); double [][] mc = new double[5][7]; copie(m,mc); Ecran.afficherln("Matrice copiee"); affichageMatrice(mc); Ecran.sautDeLigne(); double [][] mt = new double[7][5]; transposition(m,mt); Ecran.afficherln("Matrice 7x5 transposee"); affichageMatrice(mt); Ecran.sautDeLigne(); double [][] m2 = initRandMatrice(5,5,90.0); Ecran.afficherln("Matrice 5x5 initiale"); affichageMatrice(m2); Ecran.sautDeLigne(); transposition(m2); Ecran.afficherln("Matrice 5x5 transposee"); affichageMatrice(m2); } }