hey guys, another question in for ya
I got some code i'm trying to write to determine some information (comparisons and counters) on user input
The user enters an integer (total number of animals to be entered in tables)
the user is then asked to input info on the first animal (sexe, a char,age, an int, poids, a float and taille a float as well
after the final animal information is entered, the program will then print out information based on the following conditions:
- element coordinates for males over 30 long (taille) (needs a little work... was only counting them)
- count and print out the number females under 1kg (poids)
- to calculate and print out average age of males and females (stuck there, just won't work properly)
- to calculate and print the shortest and tallest animal heights (needs a sorting algorithm.)
so the code without further adue.
The user enters an integer (total number of animals to be entered in tables)
the user is then asked to input info on the first animal (sexe, a char,age, an int, poids, a float and taille a float as well
after the final animal information is entered, the program will then print out information based on the following conditions:
- element coordinates for males over 30 long (taille) (needs a little work... was only counting them)
- count and print out the number females under 1kg (poids)
- to calculate and print out average age of males and females (stuck there, just won't work properly)
- to calculate and print the shortest and tallest animal heights (needs a sorting algorithm.)
so the code without further adue.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define HAUTEUR 30.0 #define POIDS 1.0 int main() { // initialisation des variables int nbrAnimaux=0; // Valeur que specifie l'usager pour creer les tableaux int nbrMale=0; // compteur animaux males int nbrFemelle=0; // compteur des femelles int nbrMaleG=0; // compteur des animaux > 30cm de longueur int nbrFemelleL=0; // compteur des femelles < 1kg int i, j; // compteur boucle int ageM=0; int ageMoyenM=0; // age total et moyen des males int ageF=0; int ageMoyenF=0; // age total et moyen des femelles int plusCourte=0; int plusLongue=0; char condition; // Oui ou Non float taille[nbrAnimaux], poids[nbrAnimaux]; int age[nbrAnimaux]; char sexe[nbrAnimaux]; nbrMale = 0; nbrFemelle = 0; nbrMaleG = 0; nbrFemelleL = 0; // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul do { nbrFemelle=0; nbrMale=0; // Saisi et lecture des donnees printf("SVP entrez le nombre d'animaux pour vos calculs:\n"); scanf("%d", &nbrAnimaux); // printf("Maintenant, SVP entrez les details comme ceci, \nSexe (M/F), Age, Poids et Longueur\n"); // for (i=0; i < nbrAnimaux; i++) { // printf("tableau sexe: %c\n", &sexe[i]);} for (i = 0; i < nbrAnimaux; i++) { fflush(stdin); printf("Animal %d\n", i+1); printf("entrez sexe (M/F)\n"); scanf("%c", &sexe[i]); printf("entrez l age en jours\n"); scanf("%d", &age[i]); printf("entrez le poids en kg\n"); scanf("%f", &poids[i]); printf("entrez la longueur en cm\n"); scanf("%f", &taille[i]); } for (j = 0; j < nbrAnimaux; j++) { if (toupper(sexe[j]) == 'M' && taille[j] > HAUTEUR) // Compter les males > 30cm de long nbrMaleG++; if (toupper(sexe[j]) == 'F' && poids[j] < POIDS) // Compter les femelles < 1k de poids nbrFemelleL++; if (sexe[j] == 'M') // ajouter l'age des males et femelles ageM += age[j]; else ageF += age[j]; if (toupper(sexe[j]) == 'M') // Compter les males et femelles nbrMale++; else nbrFemelle++; } ageMoyenM == ageM / nbrMale; ageMoyenF == ageF / nbrFemelle; printf("Non demande\n"); printf("===========\n"); printf("Nombre de males: %d\n", nbrMale); printf("Nombre de femelles: %d\n", nbrFemelle); printf("Demande\n"); printf("=======\n"); printf("Nombre de males mesurant plus de 30cm: %d\n", nbrMaleG); printf("Nombre de femelles pesant moins de 1kg: %d\n", nbrFemelleL); printf("Age moyen des males: %d et l\'age moyen des femelles: %d", ageMoyenM, ageMoyenF); // debug // for (i = 0; 1<nbrAnimaux; i++) // printf ("a la case %d, le sexe est %c, l'age est %d, le poids est %f et la longueur est %f", i, sexe[i], age[i], poids[i], taille[i]); // end debug // Condition requise pour repartir du début printf ("\nVoulez vous faire un autre calcul, (o/n)?"); fflush (stdin); // Fonction pour vider stdin condition = toupper(getchar()); // Capitaliser la letter et soumettre à condition } while ( condition == 'O'); // Repartir du début si O // fin du do while restart }
0