HW Question: We will simulate the throwing of dice. Again we will use Top-Down_Design to improve the readability, etc.
Generate 20 dice throws of two dies. Each die can generate a number of dots from 1 to 6. Add the two numbers together to get the value of the throw.
In one pass generate the 20 throws and store the numbers in an array.
In a second pass calculate the average of the numbers and display that on the console.
Seed the Random Number Generator with 8193 one time before getting any random numbers.
NOTE : We have not talked about passing a Array to functions. So for this assignment you can make the array of Dice throws global.
//I'm just confused to the concepts of adding the random generated numbers to arrays and then averaging them through the Top Down method.
#include <iostream>
#include <cstdlib>
using namespace std;
void Gen_20_Throws_Of_2_Die_And_Add_Values();
void Output_Avg(); //Calculates the average of the sum of the 20 rolls
int ArraySum[13]; // array for the numbers of 0-12 (13 total). Will ignore index array 0 and 1 later. array[13] = 12
int main()
{
Gen_20_Throws_Of_2_Die_And_Add_Values();
Output_Avg;
cin.get();
return 0;
}
void Gen_20_Throws_Of_2_Die_And_Add_Values()
{
srand(8193); //seed random number generator with 8193
int Dice_Throw_Number, Die1, Die2, Sum_Of_Two_Die;
for (int Dice_Throw_Number = 1; Dice_Throw_Number <= 20; Dice_Throw_Number++)
{
Die1 = (rand() % 6 + 1);
Die2 = (rand() % 6 + 1);
Sum_Of_Two_Die = Die1 + Die2;
ArraySum[Sum_Of_Two_Die] += 1;
}
}
void Output_Avg()
{
int Total_Of_20_Rolls, Average_Of_Rolls;
for (int i = 2; i <= 12; i++) //ignores index of 0 and 1
{
Total_Of_20_Rolls += ArraySum[i];
}
Average_Of_Rolls = Total_Of_20_Rolls / 20;
cout << "The average of 2 die rolled 20 times is " << Average_Of_Rolls;
}
Your code is a little confusing to follow, but I think I understand what's happening. Lets start with your Gen_20_Throws_Of_2_Die_And_Add_Values
method.
int Dice_Throw_Number, Die1, Die2, Sum_Of_Two_Die;
for (int Dice_Throw_Number = 1; Dice_Throw_Number <= 20; Dice_Throw_Number++)
{
Die1 = (rand() % 6 + 1);
Die2 = (rand() % 6 + 1);
Sum_Of_Two_Die = Die1 + Die2;
ArraySum[Sum_Of_Two_Die] += 1;
}
You don't need to necessarily initialize int Dice_Throw_Number
outside of the for loop. Feel free to make it inside the for loop. Also, I personally always find it easier to understand to start from zero and go up to only a < condition rather than a <=. So you'll have:
int Die1, Die2;
for (int Dice_Throw_Number = 0; Dice_Throw_Number < 20; Dice_Throw_Number++)
{
Die1 = (rand() % 6 + 1);
Die2 = (rand() % 6 + 1);
//increment position of sum by 1
ArraySum[Die1 + Die2] += 1;
}
Now in your Output_Average
function, your logic is a lot off. You want to calculate what the average result was of throwing 2 die? Right now you're only adding how many times a certain total came up, not the total itself. So for example if you rolled 12 5 times, you'll adding 5 to the Total_Of_20_Rolls
, not 60. That's easy to change, you just need to multiply.
int Total_Of_20_Rolls, Average_Of_Rolls;
for (int i = 2; i < 13; i++) //ignores index of 0 and 1
{
Total_Of_20_Rolls += ArraySum[i] * i;
}
Average_Of_Rolls = Total_Of_20_Rolls / 20;
cout << "The average of 2 die rolled 20 times is " << Average_Of_Rolls;
That should help you out!