Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. public class NumeroB {
  2.  
  3.  
  4. // Fonction tableaux avant tri
  5.  
  6.   static void afficher (char[] poste, int[] nbCafe, int[] age, int nbEmp) {
  7.  
  8.   System.out.printf("contenu des trois tableaux avant le tri :\n");
  9.   System.out.printf("indice poste nbCafe age\n");
  10.  
  11.       for(int i = 0; i < nbEmp; i+=1)
  12.     System.out.printf ("%3i %6c %5i %6i\n", i, poste[i], nbCafe[i], age[i]);
  13.     System.out.printf("\n\n");
  14.  
  15.     }
  16.  
  17. // Fonction compteur
  18.  
  19.     static int nombre(char posteVoulu, char[] poste, int nbEmp) {
  20.  
  21.       int n = 0;
  22.  
  23.       for(int i = 0; i < nbEmp; i+=1)
  24.         if(poste[i] == posteVoulu)
  25.           n++;
  26.  
  27.       return n;
  28.     }
  29.  
  30. // Fonction moyenne
  31.  
  32.     static double moyenne(int[] tableau, int nbEmp) {
  33.  
  34.     float somme = 0;
  35.  
  36.     for(int i = 0; i < nbEmp; i+=1)
  37.         somme += tableau[i];
  38.  
  39.     return somme / nbEmp;
  40.     }
  41.  
  42. // Fonction tri des tableaux
  43.  
  44.     public static void trier(char[] poste, int[] nbCafe, int[] age, int nbEmp) {
  45.  
  46.       for (int i = 0; i < nbEmp-1; i++)
  47.         {
  48.           int indMin = i, j;
  49.  
  50.           for (j = i+1; j < nbEmp; j++)
  51.             if (age[j] < age[indMin])
  52.               indMin = j;
  53.           if(indMin != i)
  54.             {
  55.               int tempo1 = age[i];
  56.               age[i] = age[indMin];
  57.               age[indMin] = tempo1;
  58.  
  59.               tempo1 = nbCafe[i];
  60.               nbCafe[i] = nbCafe[indMin];
  61.               nbCafe[indMin]= tempo1;
  62.  
  63.               char tempoCar = poste[i];
  64.               poste[i] = poste[indMin];
  65.               poste[indMin] = tempoCar;
  66.             }
  67.         }
  68.  
  69. }
  70.  
  71. // Fonction principale
  72.  
  73.     public static void main(String[] args) {
  74.  
  75.   char []poste = {'P', 'P', 'O', 'A', 'P', 'A', 'P', 'P'};
  76.   int  [] nbCafe = {3, 1, 6, 0, 5, 1, 0, 3},
  77.           age = {25, 19, 27, 26, 49, 24, 56, 29};
  78.   int  nbEmp = age.length;
  79.  
  80.   // Tableux avant tri
  81.  
  82.     afficher (poste, nbCafe, age, nbEmp);
  83.  
  84.   // Afficher compteurs
  85.  
  86.     System.out.printf("Le nombre de programmeurs : %d\n", nombre('P', poste, nbEmp));
  87.     System.out.printf("Le nombre de secretaires  : %d\n", nombre('S', poste, nbEmp));
  88.     System.out.printf("Le nombre d analystes     : %d\n\n", nombre('A', poste, nbEmp));
  89.  
  90.     // Afficher moyennes
  91.  
  92.     System.out.printf("L age moyen                     : %.2f ans\n", moyenne (age, nbEmp));
  93.     System.out.printf("La consommation moyenne de cafe : %.2f cafe(s)\n\n", moyenne (nbCafe, nbEmp));
  94.  
  95.     // Trier tableaux
  96.  
  97.     trier(poste, nbCafe, age, nbEmp);
  98.  
  99.     // Afficher tableaux apres tri
  100.  
  101.     System.out.printf("apres le tri selon les ages :\n");
  102.     afficher(poste, nbCafe, age, nbEmp);
  103.  
  104.  
  105.   }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement