/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Auteur(s) : Nicole Leblanc IFT 1810 B Groupe 2 + + Jean Tremblay IFT 1810 B Groupe 2 + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + But du programme : Ce programme permet de . . . + + + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Dernière mise à jour : + +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Fichier Ex1_return.c Quelques fonctions avec return et un petit peu avec fonction de type void */ #include float moyenne(float tableau[], int nbElem) { int i ; float somme = 0.0; for(i = 0; i < nbElem; i++) somme += tableau[i]; return somme / nbElem; } int nombre(char statutVoulu, char statut[], int nbPers) { int n = 0, i; for(i = 0; i < nbPers; i++) if (statut[i] == statutVoulu) n++; return n; } float mini(float tab[], int nbElem) { float plusPetit = tab[0]; int i; for(i = 0; i < nbElem; i++) if (tab[i] < plusPetit) plusPetit = tab[i]; return plusPetit; } void trier(float taille[], char statut[], float poids[], int nbPers) { int i, j, indMin; float tempo1 ; char tempo2 ; /* On fait le tri */ for (i = 0; i < nbPers-1 ; i++) { indMin = i ; for (j = i+1; j < nbPers; j++) if (taille[j] < taille[indMin]) indMin = j; if(indMin != i) { tempo1 = taille[i]; taille[i] = taille[indMin]; taille[indMin] = tempo1; tempo1 = poids[i]; poids[i] = poids[indMin]; poids[indMin] = tempo1; tempo2 = statut[i]; statut[i] = statut[indMin]; statut[indMin] = tempo2; } } } void afficher(float taille[], float poids[], char statut[], int nbPers, char message[]) { int i; printf("Contenu des 3 tableaux %s\n", message); printf(" indice taille poids statut\n"); for ( i = 0; i < nbPers; i++) { printf("%5d%8.2f %9.1f", i, taille[i], poids[i]); switch (statut[i]) { case 'M' : printf(" marie\n"); break; case 'C' : printf(" celibataire\n"); break; case 'D' : printf(" divorce\n"); break; case 'S' : printf(" separe\n"); break; case 'V' : printf(" veuf\n"); break; case 'A' : printf(" autre\n"); } } } int main() { char statut[] = { 'M', 'C', 'D', 'M', 'S', 'M', 'A' }; float taille[]= { 1.72, 1.65, 1.59, 1.80, 1.82, 1.68, 1.75}, poids[] = { 68.3, 52.1, 72.9, 70.4, 81.5, 65.4, 70.3 } ; int nbPers = sizeof(statut) / sizeof(char); int i; /* Bloc 1 : afficher le contenu de 3 tableaux : */ afficher(taille, poids, statut, nbPers, "au debut"); printf("La taille moyenne : %.2f metre\n", moyenne(taille, nbPers)); printf("Le poids moyen : %.1f kg\n", moyenne(poids, nbPers)); printf("Le nombre de pers. separees : %d\n", nombre('S', statut, nbPers)); printf("Le nombre de pers. nariees : %d\n", nombre('M', statut, nbPers)); printf("Le nombre de pers. celibataires : %d\n", nombre('C', statut, nbPers)); printf("La taille minimale : %.2f metre\n", mini(taille, nbPers)); printf("Le poids minimum : %.1f kg\n", mini(poids, nbPers)); trier(taille, statut, poids, nbPers); afficher(taille, poids, statut, nbPers, "apres le tri"); printf("\n"); system("pause") ; return 0; } /* Exécution: Contenu des 3 tableaux au debut indice taille poids statut 0 1.72 68.3 marie 1 1.65 52.1 celibataire 2 1.59 72.9 divorce 3 1.80 70.4 marie 4 1.82 81.5 separe 5 1.68 65.4 marie 6 1.75 70.3 autre Le nombre de personnes mariees : 3 Le nombre de personnes veuves : 0 Appuyez sur une touche pour continuer... */