I have a project for school in which I have to write a program based on probability where a "coin" is flipped 10,000 times. The 10,000 flips are divided into 1,000 trials of 10 flips each. I'm stuck on a couple bits of code. The following is what I have:
I need to figure out what code goes where I have the comment (//flips[10] and increment (#3)). My teacher says to "Between each trial (which means inside the outer loop and outside the inner loop): Switch on the number of heads for this trial, with a case for 0, 1, 2 … 10. Within each case of the switch, add one to the appropriate counter. For example, in the case of the switch for 3 heads, add 1 to the relevant outcome counter variable. In my example, this would bean threeOfTen = threeOfTen + 1 or some other way of adding one to the counter ( ++ will also work). If you’re using an array to hold the heads counts, you can use the number of heads (out of 10) asCode:#include <stdio.h> #include <stdlib.h> int main(void){ int flips[10], numHeadsThisTime, trialNumber, flipNumber; for (trialNumber = 0; trialNumber < 1000; trialNumber++){ //flips[10] and increment (#3) for (flipNumber = 0; flipNumber <10; flipNumber++){ int thisSample; thisSample = rand(); if (thisSample < RAND_MAX/2){ //heads } if (thisSample > RAND_MAX/2){ //tails } }} system("pause"); return 0; }
the subscript into that array and increment the array element. Be sure to reset the number of heads to zero, after each set of ten flips (between the outer and inner loops) so that numHeadsThisTime will only range from 0 to 10, and not keep counting
heads across trials." I'm using the array "flips[10]." Any hints of what I need to put here?
I'm also not sure what to put in after the "if" statements. I think this is where I generate my graph of results. I need to "After these loops have run create code to generate a graph of your results. The Chapter Notes for Chapter 7 show an example of making a horizontal bar chart using printf and printing asterisks. You can use that as a starting point. Note that you will need to scale the value of each count so that a sane number of asterisks are printed. You can probably just divide each count by a constant to get how many stars to print. You’re free to produce any sort of graph that suits your fancy, as long as it accurately conveys the information from your simulation. Possible embellishments I left out include labeling the bars and adding counts to the ends giving the values of each counter. But these alterations are optional and you can receive full credit for the project with output no more detailed than mine."