Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NumeroB {
- // Fonction tableaux avant tri
- static void afficher (char[] poste, int[] nbCafe, int[] age, int nbEmp) {
- System.out.printf("contenu des trois tableaux avant le tri :\n");
- System.out.printf("indice poste nbCafe age\n");
- for(int i = 0; i < nbEmp; i+=1)
- System.out.printf ("%3i %6c %5i %6i\n", i, poste[i], nbCafe[i], age[i]);
- System.out.printf("\n\n");
- }
- // Fonction compteur
- static int nombre(char posteVoulu, char[] poste, int nbEmp) {
- int n = 0;
- for(int i = 0; i < nbEmp; i+=1)
- if(poste[i] == posteVoulu)
- n++;
- return n;
- }
- // Fonction moyenne
- static double moyenne(int[] tableau, int nbEmp) {
- float somme = 0;
- for(int i = 0; i < nbEmp; i+=1)
- somme += tableau[i];
- return somme / nbEmp;
- }
- // Fonction tri des tableaux
- public static void trier(char[] poste, int[] nbCafe, int[] age, int nbEmp) {
- for (int i = 0; i < nbEmp-1; i++)
- {
- int indMin = i, j;
- for (j = i+1; j < nbEmp; j++)
- if (age[j] < age[indMin])
- indMin = j;
- if(indMin != i)
- {
- int tempo1 = age[i];
- age[i] = age[indMin];
- age[indMin] = tempo1;
- tempo1 = nbCafe[i];
- nbCafe[i] = nbCafe[indMin];
- nbCafe[indMin]= tempo1;
- char tempoCar = poste[i];
- poste[i] = poste[indMin];
- poste[indMin] = tempoCar;
- }
- }
- }
- // Fonction principale
- public static void main(String[] args) {
- char []poste = {'P', 'P', 'O', 'A', 'P', 'A', 'P', 'P'};
- int [] nbCafe = {3, 1, 6, 0, 5, 1, 0, 3},
- age = {25, 19, 27, 26, 49, 24, 56, 29};
- int nbEmp = age.length;
- // Tableux avant tri
- afficher (poste, nbCafe, age, nbEmp);
- // Afficher compteurs
- System.out.printf("Le nombre de programmeurs : %d\n", nombre('P', poste, nbEmp));
- System.out.printf("Le nombre de secretaires : %d\n", nombre('S', poste, nbEmp));
- System.out.printf("Le nombre d analystes : %d\n\n", nombre('A', poste, nbEmp));
- // Afficher moyennes
- System.out.printf("L age moyen : %.2f ans\n", moyenne (age, nbEmp));
- System.out.printf("La consommation moyenne de cafe : %.2f cafe(s)\n\n", moyenne (nbCafe, nbEmp));
- // Trier tableaux
- trier(poste, nbCafe, age, nbEmp);
- // Afficher tableaux apres tri
- System.out.printf("apres le tri selon les ages :\n");
- afficher(poste, nbCafe, age, nbEmp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement