"exited, floating point Exception" error in C

Asked

Viewed 51 times

-1

I solved this exercise but it keeps giving the error "exited, floating point Exception", I saw that this problem can be caused by division by zero... but I do not find the error, if anyone can help me...

Write a program that reads a data set containing height and sex (male and female) of 5 people and then calculate and write:

  • the largest and smallest height of the group
  • the average height of women
  • number of men and the percentage difference between these and women

#include <stdio. h> #include<string. h> int main(){ char sex[5]; float height[5], maiorAltura=height[0], lessAltura=height[0]; float mediaAltMusives, differentPerc=0; int sexFem=0, sexMasc=0;

for(int i=0; i<5; i++){
  printf("Sexo: ");
  scanf("%c", &sexo[i]);

  printf("Altura: ");
  scanf("%f", &altura[i]);
  getc(stdin); //impedir erro de buffer

  //maior altura
  if(altura[i]>maiorAltura){
    maiorAltura=altura[i];
  };

  //menor altura
  if(altura[i]<menorAltura){
    menorAltura=altura[i];
  };

  //soma de altura de mulheres
  if(strcmp(sexo, "f")==0){
    sexoFem+=1;
    mediaAltMulheres+=altura[i];
  } else if(strcmp(sexo, "m")==0){
    sexoMasc+=1;
  } 
}

//media de altura de mulheres
mediaAltMulheres=(mediaAltMulheres/sexoFem);

//diferença percentual
if(sexoMasc>sexoFem){
  diferencaPerc=((sexoMasc-sexoFem)/sexoFem)*100;
} else{
  diferencaPerc=((sexoFem-sexoMasc)/sexoMasc)*100;
}

printf("\nA maior altura é %f", maiorAltura);
printf("\nA menor altura é %f", menorAltura);
printf("\nO numero de homens é %d, e a diferença percentual é %f%%", sexoMasc, diferencaPerc);

}

1 answer

0


There are three logical errors in your program.

Compare char as String

if(strcmp(sexo, "f")==0){
    sexoFem+=1;
    mediaAltMulheres+=altura[i];
} else if(strcmp(sexo, "m")==0){
    sexoMasc+=1;
}

In this piece of code there is a logical error, because you are comparing a char as if it were a string. The function strcmp serves to compare two strings and not variables of the type char.

At first reading when you do strcmp(sexo, "f")==0, you check whether the string/vetor sexo is equal to f, that is, if all the characters in the vector sexo is the same and in the same sequence as f. When we only have 1 single character in this vector this may even be true (if the position sexo[1] be it \0), but when you add one more element to the vector then the if will always be false. Example:

If you have only one position then the comparison is like this:

"f" == "f"

That’s true, but when we add one more element then it will look like this:

"fm" == "f"

And this is false, one more reading and the string will look like this:

"fmf"

Notice that because of this the if will only be activated once, so sexoMasc or sexoFem will be equal to zero while the other will be equal to one (you can even put one printf after reading to observe this).

To solve this you need to compare the vector position sexo who has just received f or m, being like this:

if(sexo[i] == 'f'){
    sexoFem+=1;
    mediaAltMulheres+=altura[i];
} else if(sexo[i] == 'm'){
    sexoMasc+=1;
}

sexo[i] == 'f' this is the correct way to compare char.

Assigning memory junk to a variable

Look at this bit of code:

float altura[5], maiorAltura=altura[0], menorAltura=altura[0];

You created a size 5 vector and left maiorAltura and menorAltura equal to the first position of altura, but the question is: what is the value of altura[0]?

Where I’m running the code the value altura[0] is always zero and this causes a logical error in the program.

Note that like the menorAltura starts at zero so the lowest height shown will always be zero, because all values typed will always be in the range of 1.5 to 2 (and zero is actually less than these values). But let’s assume that the value of altura[0] for 645456, then the mistake will be in maiorAltura. Because of that we can not do that assignment, leave the code like this:

float altura[5], maiorAltura, menorAltura;

This still doesn’t solve the problem, because the variables are still with memory junk.

To solve the problem we can do the following:

for(int i = 0; i < 5; i++)
{
    printf("Sexo: ");
    scanf("%c", &sexo[i]);

    printf("Altura: ");
    scanf("%f", &altura[i]);
    getc(stdin); //impedir erro de buffer

    if(i == 0)
    {
        maiorAltura = altura[i];
        menorAltura = altura[i];
    }
    else
    {
        //maior altura
        if(altura[i] > maiorAltura)
        {
            maiorAltura = altura[i];
        }

        //menor altura
        if(altura[i] < menorAltura)
        {
            menorAltura = altura[i];
        }
    }
    // Resto do código

Repair the new if will only be activated on first reading, then menorAltura and maiorAltura will equal altura[0] (the difference now is that this position of the vector has no more garbage but a valid value for our program). From the second reading on the else will always be activated and with this the program will compare the sizes.

Error when calculating the percentage

Another error in your program is:

//diferença percentual
if(sexoMasc > sexoFem) {
  diferencaPerc = ((sexoMasc - sexoFem) / sexoFem) * 100;
} else {
  diferencaPerc = (( sexoFem - sexoMasc) / sexoMasc) * 100;
}

Note that first the program will subtract two integers (sexoMasc and sexoFem), Let’s assume three men and two women were typed, so sexoMasc - sexoFem:

3 - 2 = 1

As the two variables are of the type int then the answer will also be int. Now let’s split the answer (which is kind int) by 2:

1 / 2 = 0

When we divide an integer the answer is also an integer and the decimal part is lost, this is occurring in your code. For the answer is 0,5 the 1 would have to be like float (notice that it is not because it is the result of two int, so he is int).

To solve this problem we could change the type of variables sexoMasc and sexoFem. But there is another way, we can use the (float) to say that the sum result must be of the float, thus:

diferencaPerc = ((float)(sexoMasc - sexoFem) / (sexoFem)) * 100;

Here we are saying that the result of the sum should be float (float)(sexoMasc - sexoFem).

Your code is thus with the aforementioned changes:

#include<stdio.h>
#include<string.h>

int main(void)
{
    char sexo[5];
    float altura[5], maiorAltura, menorAltura;
    float mediaAltMulheres, diferencaPerc = 0;
    int sexoFem = 0, sexoMasc = 0;

    for(int i = 0; i < 5; i++)
    {
        printf("Sexo: ");
        scanf("%c", &sexo[i]);

        printf("Altura: ");
        scanf("%f", &altura[i]);
        getc(stdin); //impedir erro de buffer

        if(i == 0)
        {
            maiorAltura = altura[i];
            menorAltura = altura[i];
        }
        else
        {
            //maior altura
            if(altura[i] > maiorAltura)
            {
                maiorAltura = altura[i];
            }

            //menor altura
            if(altura[i] < menorAltura)
            {
                menorAltura = altura[i];
            }
        }

        //soma de altura de mulheres
        //if(strcmp(sexo, "f") == 0)
        if(sexo[i] == 'f')
        {
            sexoFem += 1;
            mediaAltMulheres += altura[i];
        }
        //else if(strcmp(sexo, "m") == 0)
        else if(sexo[i] == 'm')
        {
            sexoMasc += 1;
        } 
    }

    //media de altura de mulheres
    mediaAltMulheres = (mediaAltMulheres / sexoFem);

    //diferença percentual
    if(sexoMasc > sexoFem)
    {
        diferencaPerc = ((float)(sexoMasc - sexoFem) / (sexoFem)) * 100;
    }
    else
    {
        diferencaPerc = ((float)(sexoFem - sexoMasc) / sexoMasc) * 100;
    }

    printf("\nA maior altura é %f", maiorAltura);
    printf("\nA menor altura é %f", menorAltura);
    printf("\nO numero de homens é %d, e a diferença percentual é %f%%", sexoMasc, diferencaPerc);

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.