|
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. -
#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
| |
Share:
|
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.
| | 8TB |
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.
| | Expert Mod 8TB |
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.
| | |
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
| | |
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 <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
| | Expert 256MB |
You wrote - 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.
| | | -
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];
-
-
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).
| | |
You wrote - 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
| | | -
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];
-
-
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?
| | Expert 256MB |
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.
| | |
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 ?
| | |
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 -
#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.
| | Expert 2GB |
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.
| | 8TB |
...
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?
| | Expert 2GB |
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.
| | 8TB |
I understand and appreciate all the rules of significant numbers that you are describing above. Perhaps the misunderstanding (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.
| | Expert 256MB |
I have tested your program, and it all works correctly if you allocate space for the maximum number of animals allowed.
| | Expert Mod 8TB |
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.
| | |
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 -
/* 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
| | Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
5 posts
views
Thread by Martin |
last post: by
|
12 posts
views
Thread by G. |
last post: by
|
3 posts
views
Thread by user |
last post: by
|
134 posts
views
Thread by evolnet.regular |
last post: by
|
7 posts
views
Thread by Robert Seacord |
last post: by
|
30 posts
views
Thread by Jakle |
last post: by
|
47 posts
views
Thread by Thierry Chappuis |
last post: by
|
111 posts
views
Thread by Enteng |
last post: by
|
14 posts
views
Thread by deko |
last post: by
|
17 posts
views
Thread by CoreyWhite |
last post: by
| | | | | | | | | | |