Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with getting statistics on rolling a natural yahtzee in C
    primarykey
    data
    text
    <p>I am making this program for an assignment at school, however we need to get the highest number of rolls it takes to obtain a Yahtzee and the lowest number of rolls it takes to get a Yahtzee. Then we need to find the average number of rolls. At the moment I am kind of confused on where I went wrong with determining the highest and lowest number of rolls to obtain a Yahtzee. Can anyone help me out, I would greatly appreciate it. The program runs until 100 natural Yahtzees are obtained.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; #include &lt;limits.h&gt; int main( void ) { // MARKS THE BEGINNING OF THE main( ) BLOCK OF STATEMENTS int die1 = 0 , die2 = 0 , die3 = 0 , die4 = 0, die5 = 0 , roll = 0 ; int lownr = INT_MAX , highnr = 0 , yahtzee = 0 , averagenr; int roll_total = 0 ; srandom ( (unsigned) time (NULL) ) ; printf( "\nWelcome User, this program demonstrates the " ) ; printf( "number of rolls it takes to obtain" ) ; printf( "\na natural Yahtzee.\n" ) ; die1 = random ( ) % 6 + 1; die2 = random ( ) % 6 + 1; die3 = random ( ) % 6 + 1; die4 = random ( ) % 6 + 1; die5 = random ( ) % 6 + 1; printf( "\nYou rolled: %d, %d, %d, %d, %d.\n\n" , die1 , die2 , die3 , die4 , die5 ) ; while (yahtzee &lt; 100) { ++roll; ++roll_total; die1 = random ( ) % 6 + 1; die2 = random ( ) % 6 + 1; die3 = random ( ) % 6 + 1; die4 = random ( ) % 6 + 1; die5 = random ( ) % 6 + 1; if ((die1==die2) &amp;&amp; (die2==die3) &amp;&amp; (die3==die4) &amp;&amp; (die4==die5)) { yahtzee = yahtzee +1; /* If you have yahtzee, compare roll with previous values of highnr/lownr */ if (roll&gt;highnr) highnr = roll; if (roll&lt;lownr) lownr = roll; /* Reset the roll counter, for next yahtzee */ roll = 0; } } averagenr = roll_total / yahtzee ; printf("\n The total number of yahtzees you rolled is: %d.",yahtzee); printf("\n The total number of times you rolled is: %d.",roll_total); printf("\n The highest number of rolls to get a yahtzee is: %d",highnr); printf("\n The lowest number of rolls to get a yahtzee is: %d", lownr); printf("\n The average number of rolls to obtain a yahtzee is: %d", averagenr); printf ( "\n\n Thank you for using this program. " ) ; printf ( "GoodBye.\n\n " ) ; return ( 0 ) ; } </code></pre> <p>Sample Output</p> <pre><code>Welcome User, this program demonstrates the number of rolls it takes to obtain a natural Yahtzee. You rolled: 5, 5, 3, 2, 1. The total number of yahtzees you rolled is: 100. The total number of times you rolled is: 116704. The highest number of rolls to get a yahtzee is: 5284 The lowest number of rolls to get a yahtzee is: 18 The average number of rolls to obtain a yahtzee is: 1167 Thank you for using this program. GoodBye. </code></pre> <p><strong>SOLVED</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload