/* Fichier Tableaux.cpp Cas de plusieurs tableaux Dans cet exemple : - 3 tableaux a traiter - 2 fonctions permuter avec parametres transmis par reference - parametre par defaut (dans afficher) */ #include #include // pour setw et setprecision using namespace std; void continuer() { cout << endl << endl << "Appuyez sur une lettre suivie de Entree " ; char lettre; cin >> lettre ; cout << endl << endl; } void afficher(float taille[], float poids[], int nbCafe[], int nbPers, char message[] = "au debut") { cout.setf(ios::floatfield, ios::fixed); cout.setf(ios::showpoint); cout << "\nContenu de trois tableau " << message << ": " << endl; cout << "indice taille[i] poids[i] nbCafe[i]" << endl ; for(int i = 0 ; i < nbPers; i++) cout << setw(5) << i << ") " << setw(8) << setprecision(2) << taille[i] << setw(10) << setprecision(2) << poids[i] << setw(8) << nbCafe[i] << endl; cout << endl << endl ; } void permuter(float & a, float & b) { float tempo = a; a = b; b = tempo; } void permuter(int & a, int & b) { int tempo = a; a = b; b = tempo; } void trier(float taille[], float poids[], int nbCafe[], int nbPers) { for (int i = 0; i < nbPers-1; i++) { int indMin = i; for(int j = i+1; j < nbPers; j++) if (taille [j] < taille[indMin]) indMin= j ; if (indMin != i) { permuter(taille[i], taille[indMin]); permuter(poids[i], poids[indMin]); permuter(nbCafe[i], nbCafe[indMin]); } } } int main() { float taille[] = { 1.72, 1.68, 1.53, 1.81, 1.75 }, poids[] = { 65.3, 52.1, 78.4, 56.9, 61.7 }; int nbCafe[] = { 3, 1, 0, 4, 2, 2 } ; int nbPers = sizeof(taille) / sizeof(float) ; afficher(taille, poids, nbCafe, nbPers); trier(taille, poids, nbCafe, nbPers); afficher(taille, poids, nbCafe, nbPers, "apres le tri selon les tailles"); continuer(); return 0; } /* Exécution : Contenu de trois tableau au debut: indice taille[i] poids[i] nbCafe[i] 0) 1.72 65.30 3 1) 1.68 52.10 1 2) 1.53 78.40 0 3) 1.81 56.90 4 4) 1.75 61.70 2 Contenu de trois tableau apres le tri selon les tailles: indice taille[i] poids[i] nbCafe[i] 0) 1.53 78.40 0 1) 1.68 52.10 1 2) 1.72 65.30 3 3) 1.75 61.70 2 4) 1.81 56.90 4 Appuyez sur une lettre suivie de Entree*/