/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2005 */ import java.io.*; public class RechercheIndiceMinimum { static BufferedReader flux = new BufferedReader(new InputStreamReader(System.in)); /* Fonction d'affichage de tous les entiers contenus */ /* dans un tableau d'entiers */ public static void affichageTableau(int [] t) { int i; /* Pour toutes les valeurs d'indice 0 a t.length-1 inclus */ for ( i = 0 ; i < t.length ; i++ ) System.out.println(t[i]); } /* Fonction d'initialisation de tous les entiers */ /* contenus dans un tableau d'entiers */ /* avec une valeur tiree au sort entre 0 et max inclus */ public static void initialisationTableau(int [] t,int max) { int i; /* Pour toutes les valeurs d'indice 0 a t.length-1 inclus */ for ( i = 0 ; i < t.length ; i++ ) { /* Initialisation de t[i] avec un entier tire */ /* au hasard entre 0.0 (inclu) et max+1 (exclu) */ t[i] =(int) ((max+1)*Math.random()); } } /* Fonction de recherche de l'indice de la valeur */ /* minimum d'un tableau pour les seules valeurs */ /* allant de l'indice deb a l'indice fin inclus */ public static int indiceMinimum(int [] t,int deb,int fin) { /* Declaration et initialisation a t[deb] de la variable */ /* de stockage de l'indice recherche */ int imin = deb; int i; /* Pour toutes les valeurs d'indice 0 a t.length-1 inclus */ for ( i = deb+1 ; i <= fin ; i++ ) /* Si la valeur d'indice i est plus petite que la valeur */ /* d'indice imin */ if ( t[i] < t[imin] ) imin = i; return(imin); } /* Fonction principale */ public static void main(String [] args) throws IOException { /* Definition et allocation d'un tableau de 10 entiers */ /* tires au sort entre 0 et 5 */ int [] tab = new int[10]; int indice; int indiceDebut; int indiceFin; initialisationTableau(tab,100); System.out.println("Le tableau contient les valeurs suivantes:"); /* Appel a la fonction d'affichage */ affichageTableau(tab); System.out.println(); /* Lecture clavier de l'indice de debut de recherche */ System.out.println("Indice de debut de recherche:"); indiceDebut = Integer.valueOf(flux.readLine()).intValue(); /* Lecture clavier de l'indice de fin de recherche */ System.out.println("Indice de fin de recherche:"); indiceFin = Integer.valueOf(flux.readLine()).intValue(); /* Appel a la fonction de recherche */ indice = indiceMinimum(tab,indiceDebut,indiceFin); System.out.println("L'indice de la valeur minimale est "+indice); System.out.println("et correspond a la valeur "+tab[indice]); } }