Advertisement
Guest User

Untitled

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