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.
#include 
#include 
#include 
#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
                

Replies

  • OrAnGeWorX
    OrAnGeWorX
    Been trying to tweak the code, still to no avail (maybe a little better than before)
    i tried to print out the values of the tables before running the program and the int/char tables are displaying weird stuff

    here's some info
    SVP entrez le nombre d'animaux pour vos calculs:
    4
    tableau sexe: ┘
    tableau age: 37813980
    tableau poids: 0.000000
    tableau poids: 0.000000
    tableau sexe: ┌
    tableau age: 37813984
    tableau poids: 0.000000
    tableau poids: 0.000000
    tableau sexe: █
    tableau age: 37813988
    tableau poids: 0.000000
    tableau poids: 0.000000
    tableau sexe: ▄
    tableau age: 37813992
    tableau poids: 0.000000
    tableau poids: 0.000000

    my tableau sexe is a char table... it's displaying weird caracters
    and the age table should be 0'd out as well right? like the age and poids tables.
  • OrAnGeWorX
    OrAnGeWorX
    Any1 got some ideas ? pretty please ?
  • OrAnGeWorX
    OrAnGeWorX
    new code... works a whole lot better, just a couple glitches

    
    /* Ce programme saisi des données sur des animaux etudiés par une biologiste:
          - 1ere ligne, leur nombre, pour créer les tableaux qui contiendront
            les infos
          - 2eme ligne et + (jusqu'au nombre saisi à la première ligne), leur sexe
                 (M ou F, char), leur age (en jours, int), leur poids (en kg, réel)
                 et leur taille (en cm, réel)
       et affiche des résultats selon ces critères:
          - les coordonnées des males de plus de 30cm
          - le nombre de femelles qui pèsent moins de 1.0 kg
          - l'age moyen des femelles et celui des males
          - la longueur minimale et maximale des animaux */
    
    #include 
    #include 
    #include 
    #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 coordMG=0;                            // compteur des animaux > 30cm de longueur
        int nbrFemelleL=0;                        // compteur des femelles < 1kg
        int i, j;                                    // compteur boucle
        int ageTotalM=0;                       // age total et moyen des males
        int ageTotalF=0;                       // age total et moyen des femelles
        float taillePlusCourte=5000;
        float taillePlusGrande=0;
        char condition;                           // Oui ou Non
        float taille[nbrAnimaux], poids[nbrAnimaux];
        int age[nbrAnimaux];
        char sexe[nbrAnimaux];
    
        // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
        do
          {
        // 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++)
                {
                age[i] == 0;
                }
            
            for (i = 1; i <= nbrAnimaux; i++)
            {
            printf("Animal %d\n", i);
            printf("entrez sexe (M/F)\n");
            fflush(stdin);
            scanf("%c", &sexe[i]);
            printf("entrez l age en jours\n");
            fflush(stdin);
            scanf("%d", &age[i]);
            printf("entrez le poids en kg\n");
            fflush(stdin);
            scanf("%f", &poids[i]);
            printf("entrez la longueur en cm\n");
            fflush(stdin);
            scanf("%f", &taille[i]);
            }
    
                    // debug
    
            for (i = 1; i <=nbrAnimaux; i++)
                {
                printf("sexe: %c ", sexe[i]);
                printf("age: %d", age[i]);
                printf("poids: %f", poids[i]);
                printf("taille: %f\n", taille[i]);
                }
    
            // end debug
    
            for (j = 1; j <= nbrAnimaux; j++)
                {
                if (toupper(sexe[j]) == 'M')                // Compter les males et femelles
                   {
                   nbrMale++;
                   ageTotalM += age[j];
                   }
                else
                   {
                   nbrFemelle++;
                   ageTotalF += age[j];
                   }
    
                if (toupper(sexe[j]) == 'F' && poids[j] < POIDS)
                    nbrFemelleL++;
    
                if (toupper(sexe[j]) == 'M' && taille[j] > HAUTEUR)
                    printf ("Coordonnes du repere Male > 30cm: %d\n", j);
    
                if (taille[j] < taillePlusCourte)
                    taillePlusCourte = taille[j];
                if (taille [j] > taillePlusGrande)
                    taillePlusGrande = taille[j];
      }
    
    
        printf("Nombre de males: %d\n", nbrMale);
        printf("Nombre de femelles: %d\n", nbrFemelle);
        printf("Nombre de femelles pesant moins de 1kg: %d\n", nbrFemelleL);
        // printf("Age moyen des mal%& %d% et l\'age moyen des femelles: %d\n", ageTotalM/nbrMale, ageTotalF/nbrFemelle);
        printf("Taille plus grande: %6.2f et taille plus courte: %6.2f\n", taillePlusGrande, taillePlusCourte);
        // debug
    
      //  for (i = 0; 1
    and the results after running:

    SVP entrez le nombre d'animaux pour vos calculs:
    3
    Animal 1
    entrez sexe (M/F)
    M
    entrez l age en jours
    12
    entrez le poids en kg
    3
    entrez la longueur en cm
    34
    Animal 2
    entrez sexe (M/F)
    M
    entrez l age en jours
    123
    entrez le poids en kg
    1
    entrez la longueur en cm
    3
    Animal 3
    entrez sexe (M/F)
    F
    entrez l age en jours
    134
    entrez le poids en kg
    .14
    entrez la longueur en cm
    4

    // debug section
    sexe: M age: 1107820544poids: 34.000000taille: 34.000000
    sexe: M age: 1077936128poids: 3.000000taille: 3.000000
    sexe: F age: 1082130432poids: 4.000000taille: 4.000000
    // end of debug section

    Coordonnes du repere Male > 30cm: 1
    Nombre de males: 2
    Nombre de femelles: 1
    Nombre de femelles pesant moins de 1kg: 0
    Taille plus grande: 34.00 et taille plus courte: 3.00

    Voulez vous faire un autre calcul, (o/n)?


    what works and what doesn't:
    Works
    - program picks up proper number of males and females
    - program gives shortest and longest heights
    - program gives the coordinates of the males > 1 (just not sure if that's what the teacher meant)
    Doesn't work
    - program will not give good reading for females under 1kg ( the condition is probably the problem (or maybe my for loop with the conditions in it)
    - program doesn't/can't give out proper readings for average male and female ages.
    - also i have issues flushing the memory after the user selects "o" to restart the program... it seems to keep all the data from the previous run.


    thanks again

You are reading an archived discussion.

Related Posts

CEans, May father & I have been discussing this topic over the tea now. Is it the new face of terrorism that directly affects the common man? Or is this...
CEans, I overheard a discussion that dealt with ways to fight with the ongoing world recession. People were saying that living frugally and saving as much as you can is...
CEans, Wall Street Journal reports that Jerry Yang is all set to step down as CEO of Yahoo Inc., USA. What do you think is next in line for Yahoo?
Write a program to evaluate a determinant of any order n*n. Input would be the value of n and the determinant itself.The output is the value of the determinant. 😁
Hi Can any one tell me how to represent a Sparse Matrix using arrays. and also how to add two sparse matrices.😲