#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define NUM 20.0
void percentages (int,float);
int flip (int);
int main()
{
int numTosses=1000;
int heads,i;
float addHeads=0.0,aveHeads;
for(i=1;i<=20;i++)
{
heads=flip(numTosses);
percentages(numTosses,heads);
addHeads+=heads;
}
aveHeads=addHeads/NUM;
printf("下面输出平均百分比");
percentages(numTosses,aveHeads);//输出平均百分比
return 0;
}
int flip(int numTimes)
{
int randValue;
int heads=0;
int i;
srand(time(NULL));
for(i=1;i<=numTimes;i++)
{
randValue=1+(int)rand()%100;
if (randValue >50)
heads++;
}
return heads;
}
void percentages (int numTosses,float heads)
{
int tails;
float perheads,pertails;
printf("%d",heads);
tails=numTosses-heads;
printf("Number of coin tosses:%d\n",numTosses);
printf("Heads:%d Tails :%d\n",heads,tails);
perheads=(float)heads/numTosses*100.0;
pertails=(float)(numTosses-heads)/numTosses*100.0;
printf("Heads came up %6.2f percent of the time.\n",perheads);
printf("Tails came up %6.2f percent of the time.\n",pertails);
}
补二楼~I