Specifying students height and sex with a "while"

Asked

Viewed 232 times

1

The exercise asks me to read 20 students and their respective heights and gender, and then show the average height and how many men and women we have.

What I did was this:

int main() {
    float altura, media;
    char gen, mediam, mediaf;
    int contador;

    contador = 0;

    printf("Cálculo de alunos e alunas e média de suas alturas\n");
    while (contador < 3) {
        printf("Digite a altura e o sexo do aluno\n");
        scanf("%f", &altura);
        media = media + altura;
        scanf("%s", &gen);
        if (gen = 'm') {
            mediam = mediam + gen;
        } else {
            mediaf = mediaf + gen;
        }
        contador++;


    }

    printf("A média de altura dos alunos é: \n%f", media);
    printf("Temos %s alunos homens e %s alunos mulheres\n", mediam, mediaf);


    return 0;
}+

I was able to get the program to read the total height of the students, but when I type in the share of the total male and female students, the very line of reading the heights gives a mistake of Segmentation fault.

  • 1

    Is this the same statement? The code does not do what it is asking and does what it is not.

  • Yes, I managed to get the code to read the height. But maybe I didn’t use the right logic to read the number of men and women.

  • The statement does not ask for this.

  • Sorry, the statement asks the average height and how many men and women have. I did not realize that I wrote wrong.

1 answer

0


There are several errors. If you want to count the number of men and women should increase the variable, the same made in contador, after all they do not cease to be an accountant. The variable contador could be eliminated, but I don’t think it pays.

The variable names are bad, because you’re calling it an average, which is not an average, it’s an accumulator.

Can’t use = for comparison, this is the assignment operator. To buy use ==. And test all possibilities and avoid doing something with wrong information, so I made the conditions of if fuller.

The average is only calculated at the end.

There were wrong formatting of printf().

That’s how it works:

#include <stdio.h>

int main() {
    float alturas = 0.0f;
    int contador = 0, masculinos = 0, femininos = 0;
    printf("Cálculo de alunos e alunas e média de suas alturas\n");
    while (contador < 3) {
        float altura;
        char gen;
        printf("Digite a altura e o sexo do aluno\n");
        scanf("%f", &altura);
        scanf("%s", &gen);
        if (gen == 'm' || gen == 'M') masculinos++;
        else if (gen == 'f' || gen == 'F') femininos++;
        else continue;
        alturas += altura;
        contador++;
    }
    printf("A média de altura dos alunos é: %f\n", alturas / contador);
    printf("Temos %d alunos homens e %d alunos mulheres\n", masculinos, femininos);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 1

    I understand now, I still have a little difficulty in creating variables... I’m practicing this. Perfect code, I’ll study it further to see my mistakes and fix. Thank you guy!

Browser other questions tagged

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