Read multiple values and average

Asked

Viewed 169 times

0

In this algorithm if I write 10, 10 and then -1 to shut down he calculates 9.5.

No division gives exactly the result it should be, or is it my algorithm that is spelled wrong?

That would be the exercise:

Write an algorithm that calculates the average age of a group of people. In the end, the number of people and their average age should be shown. Algorithm closes when typed -1 for age.

Note: I need to use the while to solve. The code itself is working, but the division does not give an exact number, or at least closer than it should be.

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

float media=0;
int pessoas=0, idade=0;

int main()
{
    do{
        printf("Digite a sua idade (-1 para encerrar): ");
        scanf("%d", &idade);
        media = media + idade;
        pessoas++;

    if (idade < -1){
        printf("Digite uma idade valida\n");
        pessoas--;
    }

    }while(idade != -1);

    printf("A idade media do grupo e: %.1f\n", media/(pessoas-1));
    printf("O numero de pessoas no grupo e: %d\n", pessoas-1);

   return 0;
}

2 answers

2


The problem is that when it is typed -1, you add this value to the average, then in your test (typing 10, 10 and -1) the value of the variable media is 19. So the average ends up being 9.5.

You should only add the value and increment the people counter if the age is valid, so you don’t need to decrease the number of people in invalid cases and the code becomes simpler:

int idade, qtd_pessoas=0, soma=0;
while(1){
    printf("Digite a sua idade (-1 para encerrar): ");
    scanf("%d", &idade);
    if (idade == -1) break;
    if (idade < -1){
        printf("Digite uma idade valida\n");
    } else {
        soma += idade;
        qtd_pessoas++;
    }
}
printf("A idade media do grupo e: %.1f\n", (float) soma / qtd_pessoas);
printf("O numero de pessoas no grupo e: %d\n", qtd_pessoas);

while(1) creates an infinite loop (since non-zero values are considered "true"), and it is only interrupted by break (which in turn only occurs if the age is -1, which is the stopping condition of the loop).

Note that at the end of the loop the people counter will have the correct value and you will not need to subtract 1.

I also renamed the variable from media for soma, because that’s what it stands for. The average is only calculated at the end, by dividing the sum by the number of people (and if it is just to print and nothing else, nor need a variable for it).

The detail is that soma is a whole, as I am adding up the ages that are also whole. But when doing the division I did a cast for float otherwise the division would not have decimal places.

  • Ah! got it, Oce could just explain me pq no while Oce put only (1)? if that’s what I’m thinking it might help in the other exercises.. Because I just learned to use the while with some condition inside.

  • 1

    @Lucianobalestrincorrea I updated the answer with the explanation

0

The error is happening because you are passing -1 to the variable idade, therefore its average is passing the value 19 to the printf. To fix this place a structure if within your do..while;

Your code will look like this:

  do{
        printf("Digite a sua idade (-1 para encerrar): ");
        scanf("%d", &idade);

    if(idade >= 0){
        pessoas++;
        media = media + idade;
        }

    else if (idade < -1){
        printf("Digite uma idade valida\n");
        pessoas--;

         }

    }while(idade != -1);
  • What I did up there just worked, 10, 10 and -1, now it’s 10. however if now I put 10, 10, -2, -1 the average age gets an error: 1.$ and the number of people gets 1

  • @Lucianobalestrincorrea ta perfect answer Hkotsubo! implement it, unless your teacher asked to do with do..while and change my answer, if not based on the other answer

  • 1

    Actually, my teacher asked either while or while

  • 1

    @Lucianobalestrincorrea about your while doubt, in thick words: C interprets 0 as false and all other numbers as TRUE, then while will ETERNALLY repeat, perhaps you ask me: and how does it come out of the loop? Through instruction break.

Browser other questions tagged

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