/* Auteur: Nicolas JANEY */ /* nicolas.janey@univ-fcomte.fr */ /* Fevrier 2005 */ import java.io.*; public class NombreOccurrences { 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 calcul du nombre d'apparitions */ /* d'un entier dans un tableau d'entiers */ public static int nombreOccurrences(int n,int [] t) { /* Definition et initialisation a 0 d'une variable compteur */ int compteur = 0; int i; /* Pour toutes les valeurs d'indice 0 a t.length-1 inclus */ for ( i = 0 ; i < t.length ; i++ ) /* si t[i] egal n alors incrementation de 1 du compteur */ if ( t[i] == n ) compteur++; return(compteur); } /* Fonction principale */ public static void main(String [] args) throws IOException { /* Definition et allocation d'un tableau de 10 entiers */ int [] tab = new int[10]; int n; /* Appel a la fonction d'initialisation */ initialisationTableau(tab,3); System.out.println("Le tableau contient les valeurs suivantes:"); /* Appel a la fonction d'affichage */ affichageTableau(tab); System.out.println(); /* Lecture clavier du nombre a rechercher */ System.out.println("Tapez la valeur recherchee"); n = Integer.valueOf(flux.readLine()).intValue(); /* Appel a la fonction de calcul du nombre d'apparition */ System.out.println("Le nombre de "+n+" est: "+nombreOccurrences(n,tab)); } }