C programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • orangeworx
    New Member
    • Nov 2008
    • 18

    C programming

    Been getting some help from you guys and i really appreciate it and hope some1 will have some idea y i'm getting oddities

    Here's my code and i'll explain in between.

    Code:
    #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
    
    }
    Well as i'm tryin some numbers, i found out that in my sexe table, there's 1 female no matter what...
    say for instance i put 2 animals
    and input the info for the animals, the print out will be 1 male one female, despite entering info for 2 males...
    the calculation for the average age, ageMoyen, is just not workin 1 bit...

    i'm sure i have issues with my loops there but can't really pin point where it might be.

    any help is greatly appreciated :)
    Marc
  • orangeworx
    New Member
    • Nov 2008
    • 18

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

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      When you divide two integers the result is not always an integer. Some of your int variables need to be float and your division expressions need to be made aware of that fact.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by r035198x
        When you divide two integers the result is not always an integer. Some of your int variables need to be float and your division expressions need to be made aware of that fact.
        Not necessarily. As a general rule you try to avoid mixing integers and floating point. Floating point should be reserved for scientific work where neihter spped or accuracy are big concerns.

        Division is a good example. The average of 3 and 4 is 3. It can't be 3.5 since a) 3 represents all values between 3 and <4 and b) 3.5 can't be an int since integers have no decimals. For the average to be 3.5 you have to start out with 3.0 and 4.0 and then the average is 3.5.

        Financial applications use integers.

        A rule of significant figures also states that a result cannot be more accurate than the coarsest of its components. That is, you do not make things more accurate by tacking on decimal places.

        Comment

        • orangeworx
          New Member
          • Nov 2008
          • 18

          #5
          i'm still having some problems coding this...
          i'm not sure where to "look" to fix whatever i'm trying to do....
          i really don't understand why the age table is being filled with bogus info so is the sex one... it's storing some weird characters instead...

          edit: i didn't really explain what the program should be doing so here it is

          first, the user inputs an int which is used to initialize the tables (should but not working for the sex & age tables)
          second, the user is then asked to input the sex (M or F), the age (in days), the weight (in kg) and the height (in cm)
          lastly, once these are entered for the given number of animals the program computes the following:
          - the coordinates of the males with height over 30cm
          - the number of females under 1.0kg
          - the average age (in days) for males and females (separate obviously)
          - the shortest and longest height
          with print out

          Comment

          • orangeworx
            New Member
            • Nov 2008
            • 18

            #6
            new code... works a whole lot better, just a couple glitches

            Code:
            /* 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 <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 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<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
            
            }

            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

            Comment

            • boxfish
              Recognized Expert Contributor
              • Mar 2008
              • 469

              #7
              You wrote
              Code:
              for (i=0; i < nbrAnimaux; i++) 
                  { 
                  age[i] == 0; 
                  }
              using the comparison operator instead of the assignment operator. You'll have to fix that, but it doesn't seem to be the real problem with the program. I can't figure out what's wrong with it.

              Comment

              • vmpstr
                New Member
                • Nov 2008
                • 63

                #8
                Code:
                	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];
                Code:
                	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;
                            }
                just because you changed what number nbrAnimaux contains, does not mean that your arrays (taille, poids, age, sexe) have changed size :)

                You should allocate the arrays to the maximum value that you are willing to accept (or do dynamic allocation).

                Comment

                • orangeworx
                  New Member
                  • Nov 2008
                  • 18

                  #9
                  Originally posted by boxfish
                  You wrote
                  Code:
                  for (i=0; i < nbrAnimaux; i++) 
                      { 
                      age[i] == 0; 
                      }
                  using the comparison operator instead of the assignment operator. You'll have to fix that, but it doesn't seem to be the real problem with the program. I can't figure out what's wrong with it.
                  yeah my bad, i had noticed and changed that but the reason i had this loops is to clear the age table as it's giving erroneous data.... but then took it out again as it proved useless

                  Comment

                  • orangeworx
                    New Member
                    • Nov 2008
                    • 18

                    #10
                    Originally posted by vmpstr
                    Code:
                    	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];
                    Code:
                    	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;
                                }
                    just because you changed what number nbrAnimaux contains, does not mean that your arrays (taille, poids, age, sexe) have changed size :)

                    You should allocate the arrays to the maximum value that you are willing to accept (or do dynamic allocation).
                    if i don't specify nbrAnimaux to 0 the whole program crashes when run (MS send report window)
                    the weird part of this is that the other tables are fine...

                    also what do u mean by dynamic allocation?

                    Comment

                    • boxfish
                      Recognized Expert Contributor
                      • Mar 2008
                      • 469

                      #11
                      If you don't know what dynamic allocation is, don't bother with it. The sizes of taille, poids, age, and sexe are detemined by nbrAnimaux, which is not allowed. The size of an array must be constant, not a variable like nbrAnimaux. What you should do is define a macro for the maximum number of animals allowed, and make the arrays that size. It does waste space, but it will work.

                      Comment

                      • orangeworx
                        New Member
                        • Nov 2008
                        • 18

                        #12
                        Originally posted by boxfish
                        If you don't know what dynamic allocation is, don't bother with it. The sizes of taille, poids, age, and sexe are detemined by nbrAnimaux, which is not allowed. The size of an array must be constant, not a variable like nbrAnimaux. What you should do is define a macro for the maximum number of animals allowed, and make the arrays that size. It does waste space, but it will work.
                        the only issue with this is that i don't have a max number of animals... you're saying that having constraints is the only option ?

                        Comment

                        • orangeworx
                          New Member
                          • Nov 2008
                          • 18

                          #13
                          i just ran some more tests...

                          here's my code for now...
                          - the "females under 1kg" counter is not work at all now, i'm guessing it's not the condition since it worked earlier but i have no idea what change in the code affected it
                          - the age is completely stupid.
                          - still need to figure out the average age calculation since the commented lines are causing my program to crash (windows send report window) when executing

                          Code:
                          #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 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
                              // int ageMoyenM, ageMoyenF;
                              float taillePlusCourte=5000;
                              float taillePlusGrande=0;
                              char condition;                           // Oui ou Non
                                  float poids[nbrAnimaux];
                                  float taille[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 = 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)             // something wrong with this... always returns 0
                          				nbrFemelleL++;
                          
                                      if (toupper(sexe[j]) == 'M' && taille[j] > HAUTEUR)          // this works on the other hand
                          				printf ("Coordonnes du rang Male > 30cm: %d\n", j);
                          
                                      if (taille[j] < taillePlusCourte)
                                          taillePlusCourte = taille[j];
                                      if (taille [j] > taillePlusGrande)
                                          taillePlusGrande = taille[j];
                            }
                              // ageMoyenM = ageTotalM / nbrMale;                    // added these for average
                              // ageMoyenF = ageTotalF / nbrFemelle;                 // male and female age
                          
                          	//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 males %d et l\'age moyen des femelles: %d\n", ageMoyenM, ageMoyenF);         // commented this line because it's crashing the program
                              printf("Taille plus grande: %6.2f et taille plus courte: %6.2f\n", taillePlusGrande, taillePlusCourte);
                              // 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
                          
                          }
                          results from testing
                          for only males

                          SVP entrez le nombre d'animaux pour vos calculs:
                          2
                          Animal 1
                          entrez sexe (M/F)
                          M
                          entrez l age en jours
                          12
                          entrez le poids en kg
                          34
                          entrez la longueur en cm
                          56
                          Animal 2
                          entrez sexe (M/F)
                          M
                          entrez l age en jours
                          98
                          entrez le poids en kg
                          12
                          entrez la longueur en cm
                          20
                          sexe: M age: 1113587712 poids: 56.000000 taille: 56.000000
                          sexe: M age: 1101004800 poids: 20.000000 taille: 20.000000
                          >>> poids and taille can't be the same >>> how's that happening ?
                          Coordonnes du rang Male > 30cm: 1
                          Nombre de femelles pesant moins de 1kg: 0
                          Taille plus grande: 56.00 et taille plus courte: 20.00

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

                          ran it for only females
                          ===============
                          SVP entrez le nombre d'animaux pour vos calculs:
                          2
                          Animal 1
                          entrez sexe (M/F)
                          F
                          entrez l age en jours
                          12
                          entrez le poids en kg
                          .5
                          entrez la longueur en cm
                          14
                          Animal 2
                          entrez sexe (M/F)
                          F
                          entrez l age en jours
                          432
                          entrez le poids en kg
                          1.2
                          entrez la longueur en cm
                          43
                          sexe: F age: 1096810496 poids: 14.000000 taille: 14.000000
                          sexe: F age: 1110179840 poids: 43.000000 taille: 43.000000
                          >>> age is wrong, poids and taille are same here as well
                          Nombre de femelles pesant moins de 1kg: 0
                          >>> should be 1 (that was working earlier)
                          Taille plus grande: 43.00 et taille plus courte: 14.00

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

                          i'm thinking i should just give up at this point... i feel like it's going nowhere.

                          Comment

                          • donbock
                            Recognized Expert Top Contributor
                            • Mar 2008
                            • 2427

                            #14
                            Referring to the source code in post #17 ...

                            Line 10 sets nbrAnimaux to 0, while lines 22-25 define several arrays of dimension nbrAnimaux. Later (line 35), the value of nbrAnimaux changes. Unfortunately, the arrays are already defined, so their dimension can't change. I'm surprised you don't have compiler warnings for the array definitions. You should either
                            a) define the arrays for some large maximum value of nbrAnimaux and then verify at line 35 that the entered value is less than that; or
                            b) use malloc to dynamiclly allocate space for the arrays after line 35 (but don't forget to free them before you loop back for a new nbrAnimaux).
                            The easiest option is (a).

                            You don't have any error checking following any of your scanf calls. What if the user sets nbrAnimaux to a negative value at line 35? You permit the user to enter similarly ridiculous values for the other scanf calls too.

                            Lines 40, 61, and 71 should iterate from (i=0; i<nbrAnimaux; i++) because the indices for an array of dimension D range from 0 to D-1.

                            If the user decides to run the program again at lline 116, do you want to reset all of your counters, sums, minima, and maxima? Right now you don't.

                            I don't think I've said anything that hasn't already been said.

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by weaknessforcats
                              ...

                              A rule of significant figures also states that a result cannot be more accurate than the coarsest of its components. That is, you do not make things more accurate by tacking on decimal places.
                              Are you suggesting then, that age should be declared as float if average ages need to be calculated?

                              Comment

                              Working...