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;
}
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.
– Luciano Balestrin Correa
@Lucianobalestrincorrea I updated the answer with the explanation
– hkotsubo