/* Manipulations sur une matrice de int */ public class ManipulationsMatriceEntier { /* Methode de creation d'une matrice de int */ /* initialise avec des nombres aleatoires */ /* compris dans l'intervalle [0,max] */ static int [][] initRandMatrice(int lignes,int colonnes,int max) { int i; int j; int [][] tab = new int[lignes][colonnes]; for ( i = 0 ; i < lignes ; i = i+1 ) { for ( j = 0 ; j < colonnes ; j = j+1 ) { tab[i][j] =(int) (Math.random()*(max+1)); } } return tab; } /* Methode d'affichage d'un entier */ /* sur l caracteres de large */ static void afficher(int v,int l) { int i; int val; val = v; int nbc = 0; if ( v != 0 ) { while ( val != 0 ) { nbc = nbc+1; val = val/10; } } else { nbc = 1; } for ( i = 0 ; i < l-nbc ; i = i+1 ) { Ecran.afficher(" "); } Ecran.afficher(v); } /* Methode d'affichage des valeurs contenues */ /* dans une matrice de int */ static void affichageMatrice(int [][] t) { int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { afficher(t[i][j],5); } Ecran.sautDeLigne(); } } ///////////////////////////////////////////////// /* Methode de recherche la plus petite valeur */ /* contenue dans une matrice de int */ static int min(int [][] t) { int min = t[0][0]; int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { if ( t[i][j] < min ) { min = t[i][j]; } } } return min; } /* Type agrege de stockage d'une position */ static class Position { int l = 0; int c = 0; }; /* Methode de recherche de la position */ /* de la plus petite valeur */ /* contenue dans une matrice de int */ static Position positionMin(int [][] t) { Position pMin = new Position(); int min = t[0][0]; int i; int j; for ( i = 0 ; i < t.length ; i = i+1 ) { for ( j = 0 ; j < t[0].length ; j = j+1 ) { if ( t[i][j] < min ) { pMin.l = i; pMin.c = j; min = t[i][j]; } } } return pMin; } /* Methode de calcul de la somme des valeurs */ /* contenues dans un tableau de int */ static int sommeLigne(int [] ligne) { int somme; int i; somme = 0; for ( i = 0 ; i < ligne.length ; i = i+1 ) { somme = somme+ligne[i]; } return somme; } /* Methode de calcul de l'indice de la ligne */ /* de somme maximum des lignes */ /* contenues dans un tableau de int */ static int ligneMax(int [][] t) { int max; int iMax; int i; int somme; max = sommeLigne(t[0]); iMax = 0; for ( i = 1 ; i < t.length ; i = i+1 ) { somme = sommeLigne(t[i]); if ( somme > max ) { iMax = i; max = somme; } } return iMax; } ///////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { int iMax; int [][] t = initRandMatrice(20,8,999); affichageMatrice(t); Ecran.sautDeLigne(); Ecran.afficherln("Plus petite valeur : ",min(t)); Position p = positionMin(t); Ecran.afficherln("En position : ",p.l," ",p.c); iMax = ligneMax(t); Ecran.afficherln("La ligne max est : ",iMax); } }