Problem with calculating the mean in C language

Asked

Viewed 390 times

0

I’m doing this exercise:

4 - Write an algorithm that reads a set of 50 chips, each one containing the height and sex of a person (1 = male and 2 = female), and calculates and prints:

  • The biggest and smallest height of the class;

  • The average height of women;

  • The average height of the class.

Everything is running smoothly, except the average woman.

#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <conio.h>
#include <time.h>
#include <locale.h>

int main ()
{
    setlocale(LC_ALL, "PORTUGUESE");
    
    int i, somamulher, somaturma, genero[4];
    float maioralt=0, menoralt=999, mediaaltmulher, medaltturma, altura[4];

    
    printf("\n\n #######################################");
    printf("\n\n           ANÁLISE DE FICHAS");
    printf("\n\n #######################################");
    
    
    for(i=0; i<4; i++)
    {
        printf("\n\n ------------------------------------------------------------------");
        printf("\n\n (%i) - Qual seu gênero (1 - Masculino / 2 - Feminino): ", i+1);
        scanf("%i", &genero[i]);
        
        printf("\n (%i) - Insira sua altura: ", i+1);
        scanf("%f", &altura[i]);
        
        somaturma = somaturma + altura[i];
        system("cls");
    }
    
    //MEDIA MULHER
    if(genero[i] = 2)
    {
        somamulher = somamulher + 1;
    }
    
    mediaaltmulher = somamulher / 4;
    
    //MAIOR ALTURA TURMA
    for(i=0; i<4; i++)
    {
        if(altura[i] > maioralt)
        {
            maioralt = altura[i];
        }
    }
    
    //MENOR ALTURA TURMA    
    for(i=0; i<4; i++)
    {
        if(altura[i] < menoralt)
        {
            menoralt = altura[i];
        }
    }
    
    //MEDIA ALTURA TURMA
    medaltturma = somaturma / 4;

    
    printf("\n\n ****************************************************");
    printf("\n\n               R E S U L T A D O S");
    printf("\n\n ****************************************************");
    printf("\n A média de altura das mulheres é: %0.1f", mediaaltmulher);
    printf("\n A maior altura da turma é: %0.1f", maioralt);
    printf("\n A menor altura da turma é: %0.1f", menoralt);
    printf("\n A média de altura da turma é: %0.1f", medaltturma);      
}
  • Note: I put the counter up to 4 [for(i=0; i<4; i++)] just to make the test faster.

1 answer

2


The code is very confusing and this helps make it difficult to understand. The biggest problem is that you are counting the number of women, but not accumulating the heights, there is no way to calculate the average correctly. Simply put, the code would look like this:

#include <stdio.h>
#define QUANTIDADE 4

int main() {
    printf("#######################################");
    printf("\n           ANÁLISE DE FICHAS");
    printf("\n#######################################");
    int contaMulher = 0;
    float maiorAltura = 0, menorAltura = 999, somaMulher = 0, somaAltura = 0;
    for (int i = 0; i < QUANTIDADE; i++) {
        printf("\n ------------------------------------------------------------------");
        printf("\n (%d) - Qual seu gênero (1 - Masculino / 2 - Feminino): ", i + 1);
        int genero;
        scanf("%d", &genero);
        printf("\n (%d) - Insira sua altura: ", i + 1);
        float altura;
        scanf("%f", &altura);
        somaAltura += altura;
        if (genero == 2) {
            somaMulher += altura;
            contaMulher++;
        }
        if (altura > maiorAltura) maiorAltura = altura;
        if (altura < menorAltura) menorAltura = altura;
    }
    printf("\n****************************************************");
    printf("\n               R E S U L T A D O S");
    printf("\n****************************************************");
    printf("\n A média de altura das mulheres é: %0.1f", somaMulher / contaMulher);
    printf("\n A maior altura da turma é: %0.1f", maiorAltura);
    printf("\n A menor altura da turma é: %0.1f", menorAltura);
    printf("\n A média de altura da turma é: %0.1f", somaAltura / QUANTIDADE);      
}

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

  • It worked, thank you!

Browser other questions tagged

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