issues

This is a discussion on issues within the C Programming forums, part of the General Programming Boards category; Hey every1, need a little help with the following code... been kinda breakin my head on this for the past ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    49

    issues

    Hey every1, need a little help with the following code... been kinda breakin my head on this for the past couple days... having some issues with the tables mainly as they're reporting erroneous info.
    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
    
    }
    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.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    forgot to mention what the program should be doing...
    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

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's just luck that your poids are displaying 0.00000 -- they may or may not be exactly zero, or they most likely are random data that happens to be smaller than 1e-6. If you want your arrays to be 0, you must set them as such, for example:
    Code:
    int age[nbrAnimaux] = {0};

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For your array you have set the size to the following:

    int nbrAnimaux=0;

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    hey a hat trick...

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    Quote Originally Posted by slingerland3g View Post
    For your array you have set the size to the following:

    int nbrAnimaux=0;
    it is :|

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    Quote Originally Posted by tabstop View Post
    It's just luck that your poids are displaying 0.00000 -- they may or may not be exactly zero, or they most likely are random data that happens to be smaller than 1e-6. If you want your arrays to be 0, you must set them as such, for example:
    Code:
    int age[nbrAnimaux] = {0};
    i get an error message when i do that
    variable-sized object may not be initialized
    had attempted that previously with no avail unfortunately :S

    edit: i thought of a loop to initialize the table but that didn't work either

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by OrAnGeWorX View Post
    it is :|
    So that means you have no data at all! Exciting!

    If you want to use the fancy variable array size thing, you have to get the array size first. Your arrays won't magically resize when the value of nbrAnimaux changes.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    49

    ok changes in code...

    was working with several files and got kinda of lost

    This is the most functional one i have to date...

    i'll give u a rundown of what's working and what not... the "debug" section is to print out everything in the tables for verification.
    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.

    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
    
    }

    here are the results of the last run:

    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)?


    Thanks, I really appreciate the help guys... i think i woulda lost my mind... or might already have

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    Quote Originally Posted by tabstop View Post
    So that means you have no data at all! Exciting!

    If you want to use the fancy variable array size thing, you have to get the array size first. Your arrays won't magically resize when the value of nbrAnimaux changes.
    I'm initializing the nbrAnimaux to 0 but then the user "changes" that when they enter that number at the beginning of the program

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    also regarding the "coordinates"
    if I'm not mistaken, i believe we're asked to give out the element position in the table, i.e. in this example it says 1 for first row.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by OrAnGeWorX View Post
    I'm initializing the nbrAnimaux to 0 but then the user "changes" that when they enter that number at the beginning of the program
    That's way too late. If it hasn't happened yet when you have the line "int age[nbrAnimaux]" it doesn't count.

  13. #13
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    Quote Originally Posted by tabstop View Post
    That's way too late. If it hasn't happened yet when you have the line "int age[nbrAnimaux]" it doesn't count.
    ok so basically that means that when the user is inputting the value of nbrAnimaux, it's not taking it (because i specified 0 on the int declaration)
    but then how's the program running ?
    i'm confused now

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's taking it -- but it's not using to set aside any variables in the age array. You asked for 0 ints, and you got 'em. Now when you try to use age[1] (for example), it's just accessing random memory that isn't yours, but apparently isn't important enough for your computer to actually crash if you use it.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    49
    That's really bizarre because it's working for the other tables...

    edit: since i removed the =0 from the int nbrAnimaux, the program crashes as soon as it's launched
    edit2: put the equals 0 back and it works again... hmmm
    Last edited by OrAnGeWorX; 11-18-2008 at 04:33 PM.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitmap scroll issues
    By Gerread in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2007, 06:18 AM
  2. Solution to culling issues?
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 03-14-2006, 06:59 PM
  3. Directx issues
    By dpro in forum Game Programming
    Replies: 7
    Last Post: 03-30-2005, 01:58 PM
  4. gphoto2 issues
    By axon in forum Tech Board
    Replies: 3
    Last Post: 03-21-2004, 08:33 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21