/* fichier Demo2.cpp Matière : - cin, cout - un peu de format d'affichage - tableaux à un indice - quelques statistiques vues dans IFT 1810 - quelques fonctions. - surcharge de fonctions (fonctions différences portant le même nom) - fonction "template" (patron de fonction, fonction modèle) - etc ... Voir Demo3_bis.cpp */ #include #include using namespace std; // pour consulter l'écran d'exécution void continuer() { char reponse ; cout << "\nAppuyez sur une lettre suivie de Entree " ; cin >> reponse ; } void afficher(int age[], int nbCafe[], float taille[], char sexe[], int nbPers) { cout << "Contenu de 4 tableaux :\n"; cout << " indice age cafe taille sexe\n"; cout.setf(ios::fixed); cout.setf(ios::showpoint); for(int i = 0; i < nbPers;i++) cout << setw(5) << i << ") " << setw(7) << age[i] << setw(4) << nbCafe[i] << setw(8) << setprecision(2) << taille[i] << setw(5) << sexe[i] << endl ; cout << endl << endl ; } int nombre(float tab[], int nbElem , float borne) { int n = 0; for(int i = 0; i < nbElem; i++) if (tab[i] > borne) n++; return n; } template float moyenne(T tab[], int nbElem) { float somme = 0.0 ; for(int i = 0; i < nbElem ; i++) somme += tab[i]; return somme / nbElem ; } void determiner(int tab[], int nbElem, int & mini, int & maxi) { mini = maxi = tab[0]; for(int i = 1; i < nbElem ; i++) { if (tab[i] > maxi) maxi = tab[i]; if (tab[i] < mini) mini = tab[i]; } } void determiner(float tab[], int nbElem, float & mini, float & maxi) { mini = maxi = tab[0]; for(int i = 1; i < nbElem ; i++) { if (tab[i] > maxi) maxi = tab[i]; if (tab[i] < mini) mini = tab[i]; } } int main() { int age[] = { 25, 19, 41, 37, 20, 37 }, nbCafe[] = { 3, 4, 0, 6, 3, 2 } ; char sexe[] = { 'F', 'M', 'F', 'F', 'M', 'F' }; float taille[] = { 1.72, 1.84, 1.65, 1.57, 1.93, 1.85 }; int nbPers = sizeof(age) / sizeof(int); cout << "On traite " << nbPers << " personne(s) " << endl; afficher(age, nbCafe, taille, sexe, nbPers); cout << "Le nombre de personne(s) dont taille > 1.80 m : " << nombre(taille, nbPers, 1.80) << endl; cout << "Le nombre de personne(s) dont taille > 1.70 m : " << nombre(taille, nbPers, 1.70) << endl; cout << "L'age moyen : " << setw(6) << setprecision(1) << moyenne(age, nbPers) << " an(s) " << endl ; cout << "La cons. moyenne de cafe : " << setw(6) << setprecision(1) << moyenne(nbCafe, nbPers) << " tasse(s) " << endl ; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout << "La taille moyenne : " << setw(6) << setprecision(2) << moyenne(taille, nbPers) << " metre " << endl ; int ageMin, ageMax, cafeMin, cafeMax ; determiner(age, nbPers, ageMin, ageMax); cout << "age mini : " << ageMin << " an(s)\n"; cout << "age maxi : " << ageMax << " an(s)\n"; determiner(nbCafe, nbPers, cafeMin, cafeMax); cout << "cons. mini : " << cafeMin << " tasse(s)\n"; cout << "cons. maxi : " << cafeMax << " tasse(s)\n"; float tailleMin, tailleMax; determiner(taille, nbPers, tailleMin, tailleMax); cout << "Taille mini. : " << tailleMin << " metre\n"; cout << "Taille maxi. : " << tailleMax << " metre\n"; continuer(); return 0; } /* Exécution : On traite 6 personne(s) Contenu de 4 tableaux : indice age cafe taille sexe 0) 25 3 1.72 F 1) 19 4 1.84 M 2) 41 0 1.65 F 3) 37 6 1.57 F 4) 20 3 1.93 M 5) 37 2 1.85 F Le nombre de personne(s) dont taille > 1.80 m : 3 Le nombre de personne(s) dont taille > 1.70 m : 4 L'age moyen : 29.8 an(s) La cons. moyenne de cafe : 3.0 tasse(s) La taille moyenne : 1.76 metre age mini : 19 an(s) age maxi : 41 an(s) cons. mini : 0 tasse(s) cons. maxi : 6 tasse(s) Taille mini. : 1.57 metre Taille maxi. : 1.93 metre Appuyez sur une lettre suivie de Entree g */