Reputation: 49
This is the code:
#include <stdio.h>
int group, regular_group, regular_members, shutdown, special_group,
special_members;
float average_special, average_regular;
main()
{
do
{
printf("How many members are in the group?\n");
scanf("%d", &group);
if (group >= 15)
{
regular_group = regular_group + 1;
regular_members = regular_members + group;
printf("Do you want to add another group? yes = 1, no = 0 \n");
scanf("%d", &shutdown);
}
else
{
special_group = special_group + 1;
special_members = special_members + group;
printf("Do you want to add another group? yes = 1, no = 0 \n");
scanf("%d", &shutdown);
}
}
while (shutdown == 1);
average_regular = regular_members / regular_group;
average_special = special_members / special_group;
printf("\nTotal regular members:\n%d\n", regular_members);
printf("\nTotal special members:\n%d\n", special_members);
printf("\nAverage regular members in a group:\n%f\n", average_regular);
printf("\nAverage special members in a group:\n%f\n", average_special);
fflush(stdin);
getchar();
}
If a group is larger than 15 its automatically a regular group but if its less than its a special group. I need the total amount of memebrs of each catergory. The total amount of members is working fine. But I also need the average size of a group of both catergories. But the average is always 0,000000. But I cant find the error.. Please help me
Thank you very much !
Upvotes: 1
Views: 95
Reputation: 106022
This is because
regular_members/regular_group
which gives an integer after division. If numerator will become less than denominator then you will get 0
as a result. regular_group
, regular_members
, special_group
, special_members
, average_special
and average_regular
.Instead of using
average_regular = regular_members/regular_group;
average_special = special_members/special_group;
try this
average_regular = (float)regular_members/regular_group;
average_special = (float)special_members/special_group;
Also remove fflush(stdin)
. It will invoke undefined behavior.
Declare your variables
int group, regular_group, regular_members, shutdown, special_group,
special_members;
float average_special, average_regular;
inside main
function
main()
{
int group, regular_group, regular_members, shutdown, special_group, special_members;
float average_special, average_regular;
....
....
}
and do not forget to initialize the variables: regular_group
, regular_members
, special_group
, special_members
, average_special
, average_regular
to 0
.
Here is your working code (after some modifications)
#include <stdio.h>
int main()
{
int group, regular_group = 0, regular_members = 0, shutdown, special_group = 0,
special_members = 0;
float average_special = 0, average_regular = 0;
do
{
printf("How many members are in the group?\n");
scanf("%d", &group);
if (group >= 15)
{
regular_group = regular_group + 1;
regular_members = regular_members + group;
printf("Do you want to add another group? yes = 1, no = 0 \n");
scanf("%d", &shutdown);
}
else
{
special_group = special_group + 1;
special_members = special_members + group;
printf("Do you want to add another group? yes = 1, no = 0 \n");
scanf("%d", &shutdown);
}
} while (shutdown != 0);
if(regular_group != 0) //to stop division by zero
average_regular = (float)regular_members / regular_group;
if(special_group != 0) //to stop division by zero
average_special = (float)special_members / special_group;
printf("\nTotal regular members:\n%d\n", regular_members);
printf("\nTotal special members:\n%d\n", special_members);
printf("\nAverage regular members in a group:\n%f\n", average_regular);
printf("\nAverage special members in a group:\n%f\n", average_special);
return 0;
}
Upvotes: 2
Reputation: 5290
Division regular_members/regular_group
yields a integer result so any fraction becomes 0.
Cast one of the variables to float and the division will result in a float.
average_regular = regular_members/(float)regular_group;
And similar for the other version average_special
.
-Also your main is declared incorrectly, is should be at least int main( void )
-And fflush(stdin);
is not a correct way to clear the buffer. It may result in undefined behavior.
-You are might invoke division by zero here average_regular = regular_members / regular_group;
Upvotes: 1
Reputation: 1017
there no problem with your code . Only problem is Global virable are initialised to 0 so when you do not have any special or regular group then
regular_group or special_group value will be 0.
and any no divided by 0 give floating point exception . I hope you got the point .
main() must return int .
rest there is no problem .
Upvotes: 1
Reputation: 2882
It is because you are doing divisions between integers. Also if the result variable is float this is not sufficient to have the operation will be executed as you expect.
You need to use type casts like this:
average_regular = (float)regular_members/(float)regular_group;
average_special = (float)special_members/(float)special_group;
Actually it could be sufficient to put the cast only on one of the two members of each operations to have the operation compiled as float division:
average_regular = (float)regular_members/regular_group;
average_special = (float)special_members/special_group;
Upvotes: 2