:) Hi, Ive written a program that is supposed to count the number of people of different ages and calculate the average of valid ages. We exclude ages entered as 0, over 130 or negative.
The program does run but I sometimes don't have the right number in the different categories. I suppose this is because of my IF statements. Also the average doesn't calculate.
I don't see the problem, please help?
Here's what I have
#include <stdio.h>
int main()
{
// variable declaration
int i,nb, sum, total;
int min=0, adu=0, ret=0, cent=0, invalid=0;
float avg;
printf("Enter 7 ages followed by ENTER after each: \n");
scanf("%d", &nb);
//execution
for (i=1; i<7;++i){
scanf("%d", &nb);
if(nb>0 && nb<18){ //minors
total += nb;
min++;
}
if (nb>=18 && nb<55){ // adults
total+=nb;
adu++;
}
if (nb>=55 && nb<120){ // retirees
total +=nb;
ret++;
}
if (nb>=100 && nb<120){ // 100 & over
cent++;
}
else if(nb<=0 || nb>=120){ // invalid
invalid++;
}
}
// calculations
sum=(min+ret+adu);
avg=(total/sum;
// output
printf("\n Number of minors : %d",min);
printf("\n Number of retirees : %d",ret);
printf("\n Number of people 100 or over: %d",cent);
printf("\n Invalid ages : %d",invalid);
printf("\n Average age : %f", avg);
getch();
}
Thank you for any help!