C programming

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #16
    Originally posted by r035198x
    Are you suggesting then, that age should be declared as float if average ages need to be calculated?
    weaknessforcats is referring to mathematical principles that are independent of whether you solve a problem with a computer, or with a sliderule, or with pencil and paper.

    For example, I happen to know as a rule of thumb that Indianapolis is about 200 miles from Chicago. Suppose I drive to Indianapolis and then fill my gas tank by buying 11 gallons of gas. How many miles/gallon did I get from my car?

    If I divide 200 by 11 on my calculator I get 18.18181818, but all those decimal digits are a false precision. The distance was only approximately 200 miles; and my gas tank wasn't full when I left, so fuel usage was less than 11 gallons.

    The rules of significant digits suggest that the more correct answer is 20 miles per gallon. "200 miles" has one significant digit and "11 gallons" has two significant digits. The final answer should have no more significant digits than the most inexact input; that is, one significant digit.

    Referring to your specific problem ... age is entered as an integer. Unless today is the animal's birthday, an integer value is only an approximation of the animal's age. If all the animal ages are between 0 and 10, then that's one significant digit. It is false precision to compute an average age that has more than one significant digit.

    On the other hand, your teacher may be expecting to see decimal digits and might reduce your grade if they are missing.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #17
      I understand and appreciate all the rules of significant numbers that you are describing above. Perhaps the misunderstandin g (certainly from my part) comes from what you are defining as average. The most common meaning (which I had assumed) is arithmetic mean which is always a positive real number. Arithmetic mean is not closed over integers.

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #18
        I have tested your program, and it all works correctly if you allocate space for the maximum number of animals allowed.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #19
          Originally posted by r035198x
          The most common meaning (which I had assumed) is arithmetic mean which is always a positive real number. Arithmetic mean is not closed over integers.
          Yes it is. That is, if you are working with integers then your results have to be integers for them to exist in the set of integers.

          Real numbers are not integers. Now you can map integers like 1,2,3 to real numbers 1.0, 2.0, 3.0 but the most you can say is that the set of real numbers has a higher cardinality than the set of integers. That is, the set of integers is bounded but the number of elements is indeterminate.

          The problem occurs because eveyone learns how to use real numbers in school. That would correspond to the computer floating-point. So use floating-point exclusively.

          Were we taught integers in school we would have learned that the average of integers is an integer.

          Comment

          • orangeworx
            New Member
            • Nov 2008
            • 18

            #20
            I appreciate your input guys.... I've managed to work that out by using a MAX value for the tables... since dynamic tables aren't what we were asked, i was going around in circles but as soon as I had changed the table initialization to a finite number of elements, everything worked out great.

            here's the final version if any1 is interested
            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
            #define MAX 10
            
            int main()
            {
            	// initialisation des variables
            	int nbrAnimaux;			// Valeur que specifie l'usager pour creer les tableaux
                char condition;         // Oui ou Non, redemarrer le programme
            
                // Boucle do ... while, pour ne pas devoir repartir le programme pour chaque calcul
                do
                  {
            	   int nbrMale=0;						   // compteur animaux males
                   int nbrFemelle=0;					   // compteur des femelles
            	   int nbrFemelleL=0;					   // compteur des femelles < 1kg
                   int nbrMaleG=0;                         // compteur des males > 30cm
            	   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;               // les ages moyens males et femelles
                   float taillePlusCourte=5000;            // variable utilisee pour le tri
                   float taillePlusGrande=0;               // variable utilisee pour le tri
                   float taille[MAX], poids[MAX];          // tableaux pour la taille et le poids
                   int age[MAX];                           // tableau des ages
                   char sexe[MAX];                         // tableau des sexes
            
            
            
            	// Saisi et lecture des donnees
            	   printf("SVP entrez le nombre d'animaux pour vos calculs:\n");
            	   scanf("%d", &nbrAnimaux);
                   printf ("Entrez les infos de cette maniere:\nSexe (M ou F) Age Poids Taille\n\n");
            
            	// 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);
                    fflush(stdin);
                    scanf ("%c%d%f%f", &sexe[i], &age[i], &poids[i], &taille[i]);
                    }
            
                    for (j = 1; j <= nbrAnimaux; j++)
                        {
                        // Compteurs males et femelles
                        if (toupper(sexe[j]) == 'M')
                           {
                           nbrMale++;
                           ageTotalM += age[j];
                           }
                        else
                           {
                           nbrFemelle++;
                           ageTotalF += age[j];
                           }
            
                        // Condition: Femelle(s) pesant(s) moins de 1.0kg
                        if (toupper(sexe[j]) == 'F' && poids[j] < POIDS)
            				nbrFemelleL++;
            
                        // Pour trouver la taille la plus courte ainsi que la plus longue
                        if (taille[j] < taillePlusCourte)
                            taillePlusCourte = taille[j];
                        if (taille [j] > taillePlusGrande)
                            taillePlusGrande = taille[j];
                        }
            
                // Calculs des ages moyens males et femelles
                    ageMoyenM = ageTotalM / nbrMale;
                    ageMoyenF = ageTotalF / nbrFemelle;
            
                // Afficher les tableaux
                    printf("\nVos donnees telles qu'entrees:\n");
                    printf("Sexe Age Poids Taille\n");
                    for (i = 1; i <=nbrAnimaux; i++)
                        {
                        printf("%4c", sexe[i]);
                        printf("%4d", age[i]);
                        printf("%6.2f", poids[i]);
                        printf("%7.2f\n", taille[i]);
                        }
            
                // afficher les resultats selon les conditions predefinies
                    printf("Un male de plus 30cm se trouve au:\n");
                    for (i = 1; i <=nbrAnimaux; i++)
                    {
                    // Condition: Male(s) mesurant(s) plus de 30cm de long
                    if (toupper(sexe[i]) == 'M' && taille[i] > HAUTEUR)
                       printf ("Rang %d\n", i-1);
                    // i-1 parce que tableau commence a element 0
                    }
                    printf("Nombre de femelle(s) pesant moins de 1kg: %d\n", nbrFemelleL);
                    printf("L\'age moyen des males: %d\nL\'age moyen des femelles: %d\n", ageMoyenM, ageMoyenF);
                    printf("La taille plus grande: %6.2f\nLa aille plus courte: %6.2f\n", taillePlusGrande, taillePlusCourte);
            
                 // 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
            
            }
            
            /*
            
            Résultats:
            ==========
            - Avec les donnees du TP
            ------------------------
            SVP entrez le nombre d'animaux pour vos calculs:
            3
            Entrez les infos de cette maniere:
            Sexe (M ou F) Age Poids Taille
            
            Animal 1
            F 45 1.05 12.7
            Animal 2
            M 32 1.10 17.6
            Animal 3
            M 90 1.56 22.3
            
            Vos donnees telles qu'entrees:
            Sexe Age Poids Taille
               F  45  1.05  12.70
               M  32  1.10  17.60
               M  90  1.56  22.30
            Un male de plus 30cm se trouve au:
            Nombre de femelle(s) pesant moins de 1kg: 0
            L'age moyen des males: 61
            L'age moyen des femelles: 45
            La taille plus grande:  22.30
            La aille plus courte:  12.70
            
            Voulez vous faire un autre calcul, (o/n)?o
            
            Avec nos propres donnees
            ------------------------
            SVP entrez le nombre d'animaux pour vos calculs:
            5
            Entrez les infos de cette maniere:
            Sexe (M ou F) Age Poids Taille
            
            Animal 1
            M 12 32.4 12.5
            Animal 2
            M 56 12.12 34.56
            Animal 3
            F 6 .75 32
            Animal 4
            F 90 .95 12
            Animal 5
            M 27 3.6 30.01
            
            Vos donnees telles qu'entrees:
            Sexe Age Poids Taille
               M  12 32.40  12.50
               M  56 12.12  34.56
               F   6  0.75  32.00
               F  90  0.95  12.00
               M  27  3.60  30.01
            Un male de plus 30cm se trouve au:
            Rang 1
            Rang 4
            Nombre de femelle(s) pesant moins de 1kg: 2
            L'age moyen des males: 31
            L'age moyen des femelles: 48
            La taille plus grande:  34.56
            La aille plus courte:  12.00
            
            Voulez vous faire un autre calcul, (o/n)?n
            
            */
            Thanks again for taking the time, really appreciated and lesson learned :P

            Comment

            Working...