Repetition system how to store a value

Asked

Viewed 193 times

1

Make a program that receives the age, height and weight of 25 people. Calculate and show:

The number of persons over the age of 50;

The average height of people between 10 to 20 years;

The percentage of persons weighing less than 40 KG among all persons analysed.

How do I store all the heights that the user type to then calculate the average of all values without needing to put a lot of printf() and scanf()?

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>

int main(){

    setlocale(LC_ALL, "Portuguese");

    int idade[25], i, sup50=0, peso40=0;
    float altura[25], media_altura, peso[25], porcentagem, idade10_20=0;

    for(i=0; i<25; i++)

    {

        printf("DIGITE A IDADE DA %d PESSOA: ", i+1);
        scanf("%d", &idade[i]);

        printf("DIGITE A ALTURA DA %d PESSOA: ", i+1);
        scanf("%f", &altura[i]);

        printf("DIGITE O PESO DA %d PESSOA: ", i+1);
        scanf("%f", &peso[i]);

        printf("\n");


            if(idade[i]>50)
            {
                sup50++;
            }

            if(idade[i]>=10 && idade[i]<=20)
            {
              // MINHA DÚVIDA É AQUI
            }
            if(peso[i]>40)
            {
                peso40++;
                porcentagem = 100*peso40/25;
            }

    }

    printf("\n\nA QUANTIDADE DE PESSOAS COM IDADE SUPERIOR Á 50 É %d\n\n", sup50);
    printf("A MÉDIA DAS ALTURAS DE PESSOAS ENTRE 10 E 20 ANO É %.2f\n\n",  ? );
    printf("A PORCENTAGEM DE PESSOAS COM PESO INFERIOR Á 40 KG É %.2f%%\n\n", porcentagem);

system("pause");
}

1 answer

2


The code does too much either in the mechanism or in the requirement. It has a lot of variables, and other things without need, but the main thing is storing things that exercise does not ask for. All you need to know is how many are over 50 and this has been done, you need to know how many people are young and the sum if their heights, with this two information is just to make a simple division for the average height between these people and finally the percentage of people over 40kg should only be calculated at the end of the loop, in it should only count how many people are.

It’s not about programming, it’s about interpreting text and proposing a mathematical solution.

#include <stdio.h>
#include <locale.h>

int main() {
    setlocale(LC_ALL, "Portuguese");
    int idade, sup50 = 0, peso40 = 0, jovens = 0;
    float altura, peso, alturas;
    for (int i = 0; i < 25; i++) {
        printf("DIGITE A IDADE DA %d PESSOA: ", i + 1);
        scanf("%d", &idade);
        printf("DIGITE A ALTURA DA %d PESSOA: ", i + 1);
        scanf("%f", &altura);
        printf("DIGITE O PESO DA %d PESSOA: ", i + 1);
        scanf("%f", &peso);
        printf("\n");
        if (idade > 50) sup50++;
        if (idade >= 10 && idade <= 20) {
            jovens++;
            alturas += altura;
        }
        if (peso > 40) peso40++;
    }
    printf("\n\nA QUANTIDADE DE PESSOAS COM IDADE SUPERIOR Á 50 É %d\n\n", sup50);
    printf("A MÉDIA DAS ALTURAS DE PESSOAS ENTRE 10 E 20 ANO É %.2f\n\n", alturas / jovens);
    printf("A PORCENTAGEM DE PESSOAS COM PESO INFERIOR Á 40 KG É %.2f%%\n\n", peso40 * 0.25);
}

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

  • As always you helping me, thank you very much friend !!!!

  • What I was cracking my head on was this operator += that’s what I needed to learn, I’m cracking my head on a lot of issues because of this operator that I didn’t know.

Browser other questions tagged

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