Note that there are some explanatory texts on larger screens.

plurals
  1. PODice program, need to either eliminate array or allow for dynamic array size
    primarykey
    data
    text
    <p>I'm almost done with a dice rolling program and my only issue is that I'm unable to input more than 5000 rolls, due to the size of the array I've specified. While I suppose I could simply increase the size of the array to some ridiculous number, I'd prefer not to and instead use a dynamically sized array based on the input <code>remainingRolls.</code> Can anyone help?</p> <p>NOTE: This is the edited and final code that works.</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;conio.h&gt; using namespace std; int roller(){ // loop to simulate dice roll int die1, die2; int total; die1 = rand()%6+1; die2 = rand()%6+1; total=die1+die2; return total; } int main(){ int numberOfRolls; int remainingRolls; int eights=0; // declare counter array to hold frequencies of dice results int i; int value; float percentage; int currentRoll; // declare array for dice values currentRoll= 0; cout &lt;&lt; "How many times will the dice be rolled?" &lt;&lt; endl; cin &gt;&gt; remainingRolls;// user input # of dice rolls numberOfRolls = remainingRolls;// variable to hold number of rolls (for output) for (i=0; remainingRolls &gt;0; remainingRolls--){// loop to count frequency of each value currentRoll = roller();// activate diceRoll function if (currentRoll == 8){ eights++; } } percentage = (eights*100/numberOfRolls); cout &lt;&lt; "The dice were rolled " &lt;&lt; numberOfRolls &lt;&lt; " times." &lt;&lt; endl; cout &lt;&lt; "The value 8 came up " &lt;&lt; eights &lt;&lt; " times or " &lt;&lt; percentage &lt;&lt; "% of the time." &lt;&lt; endl; getch(); return 0; } </code></pre>
    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