Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C Biased Coin Problem Write a simulator in which one round of sim

ID: 3782985 • Letter: P

Question

Programming in C Biased Coin Problem

Write a simulator in which one round of simulation involves flipping a set of ten unfair coins (or

the same unfair coin ten times) in which there is a fixed likelihood of any given flip coming up
heads. The result of each round is the number of times that a head appeared, so if in a
particular round there were four heads, the result for that round would be four.
You will ask the user for the bias (the percentage chance that a coin will come up heads) and for
the number of rounds to perform. After running the simulation you will report how many times
each outcome resulted as well as the percentage of the time it occurred. Your output should
look very similar to the following (in particular, it should be well formatted):


What is the coin bias (percent chance of coming up heads)? 75
How many rounds? 1000000


COINS..... 10
BIAS...... 75.00%
ROUNDS.... 1000000


SUM ROUNDS PERCENTAGE
0    0             ( 0.00% )
1       24            ( 0.00% )
2      360           ( 0.04% )
3     3187          ( 0.32% )
4    16221         ( 1.62% )
5    58108         ( 5.81% )
6   146347        ( 14.63% )
7   249823        ( 24.98% )
8   281878        ( 28.19% )
9   187655        ( 18.77% )
10 56397          ( 5.64% )

Add a histogram to the output.
Following each line in the report you will print out a line of asterisks whose length is
proportional to the percentage of the time that result came up. If the frequency was less than
1%, do not print any asterisks. Then print an additional asterisk for each additional two
percentage points (so the boundaries should be odd percentage points). What happens should
an amount fall exactly on the boundary (say 53% exactly) is not important as far as whether a
new asterisk is added or not.


The output should now look something like:


What is the coin bias (percent chance of coming up heads)? 63.4
How many rounds? 10000


COINS..... 10
BIAS...... 63.40%
ROUNDS.... 10000


SUM ROUNDS PERCENTAGE
0       0               ( 0.00% )
1       7               ( 0.07% )
2      57              ( 0.57% )
3     261             ( 2.61% )   *
4     822             ( 8.22% )   ****
5     1765           ( 17.65% ) *********
6     2436           ( 24.36% ) ************
7     2372           ( 23.72% ) ************
8    1604            ( 16.04% ) ********
9     572               ( 5.72% ) ***
10   104               ( 1.04% ) *

Explanation / Answer

#include <stdio.h>
int fin_result[10];
float bias=0;
int rnds =0;
void flip();
int main()
{
   //printf("Test %d ",fin_result[10]);
printf( " What is the coin bias (percent chance of coming up heads)? :");
scanf( "%f", &bias);
printf( " How many rounds? :");
scanf("%d", &rnds);
int i;
for(i=0;i<11;i++)
{
       fin_result[i]=0;
   }
for(i=0;i<rnds;i++)
{
flip();
}
  
   printf(" COINS..... 10");
   printf(" BIAS......%.2f %",bias);;

   printf(" SUM || ROUND || PERCENT ");
   for(i=0;i<=10;i++)
   {
       float percent;
       percent=((float)fin_result[i]/rnds)*100;
       printf(" %d || %d || %.2f ",i,fin_result[i],percent);
      
       int j;
       for(j=0;j<(int)percent/3;j++)
       {
           printf("*");
       }
      
   }
  
return 0;
}
void flip(){
float out=0;
int result[9];
  
int i=0;
int j;
for(j=0;j<10;j++)
{
out=rand()%100;
if (out>(100-bias))
{
result[i]=1;
}
else
{
result[i]=0;
}
i++;
}

int heads=0;
int tails=0;
int k;
for(k=0;k<10;k++)
{
if(result[k]==1)
{ heads++; }
else
{ tails++; }
//printf("%d || %d ||%d || %d ",k,result[k],heads,tails);
}
//printf(" %d",heads);
fin_result[heads]++;

}