// Fichier Demo3_bis.cpp : un peu plus sur fonction template #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 ; } template void determiner(T tab[], int nbElem, T & mini, T & 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 : */