Coin Flipping Madness – Solution

I hope you didn’t “flip out” over this month’s Exercise. Flipping a coin can be done by hand or by using a computer. Using a computer is easier because it can be programmed to record each flip and it saves you hand muscle molecules for flipping and the chore of writing down the results.

Flipping requires a simple expression in C: rand()%2

But first, use the srand() function to seed the randomizer: srand( (unsigned)time(NULL) ) This step ensures that the numbers aren’t identical each time you run the program.

One hundred sets of 100 flips requires nested loops. Further, the flip totals for each run must be recorded. To meet this end, I use two arrays in my solution, heads[] and tails[]. Flips are tallied within the nested loop by using variables t and h for heads and tails. Here is the nested loop:

    /* 100 sets of 100 flips */
    for( x=0; x<iterations; x++ )
    {
        /* process 100 flips */
        for( y=t=h=0; y<iterations; y++ )
        {
            if( rand()%2 )
                t++;    /* odd is "tails" */
            else
                h++;    /* even is "heads" */
        }
        /* output this round's results */
        printf("Set %3d: %2d Heads, %2d Tails\n",x+1,h,t);
        /* store the data */
        heads[x] = h;
        tails[x] = t;
    }

I set constant int value iterations equal to 100. This constant allows me to use the value 100 and potentially change it later without having to hunt and sift through the code.

The outer for loop spins 100 times. It outputs the results for each set and stores the results in arrays heads[] and tails[].

The inner for loop flips the coins. Variable y is the looping variable, but variables h and t are also initialized in the for loop statement. An if-else structure tallies the heads (zero) and tails (one) flips.

The second part of the challenge is to interpret and output the results. Originally I wrote separate for loops for each item requested:

  • Average of all heads/tails
  • Number of times it was 50/50 heads tails
  • The greatest number of heads in a set
  • The greatest number of tails in a set

Then I realized I could do all the math in one for loop, shown here:

    /* gather data */
    h_total = t_total = even = h_max = t_max = 0;
    for( x=0; x<iterations; x++ )
    {
        /* obtain totals for averages */
        h_total += heads[x];
        t_total += tails[x];
        /* count even splits */
        if( heads[x]==50 )
            even++;
        /* store maximums */
        if( heads[x] > h_max )
            h_max = heads[x];
        if( tails[x] > t_max )
            t_max = tails[x];
    }

Variables h_total and t_total accumulate the total heads and tails flips for all 10,000 tries.

Variable even counts the number of times the split is 50:50.

Variables h_max and t_max set the highest number of heads and tails for each set.

All these variables are first initialized to zero. Then a for loop gathers the data. The only additional statements calculate the averages:

h_avg = (float)h_total/iterations;
t_avg = (float)t_total/iterations;

The float cast ensures a real number result, and matches the data type of h_avg and t_avg.

Finally, a series of printf() statements output the results.

You can click here to view the full code on GitHub.

I hope your solution met with success. I had fun coding this puzzle, which are originally just going to be a coin flipping simulation. Then I went nuts. Hopefully you went nuts as well.

Leave a Reply