将随机生成的数字添加到数组+平均这些数组
Adding random generated numbers to arrays + averaging those arrays
HW Question:我们将模拟掷骰子。我们将再次使用 Top-Down_Design 来提高可读性等。
生成 20 次骰子投掷两个骰子。每个骰子可以产生从 1 到 6 的点数。将两个数字相加得到掷出的值。
在一次传递中生成 20 次掷球并将数字存储在数组中。
在第二遍计算数字的平均值并将其显示在控制台上。
在获得任何随机数之前,用 8193 为随机数生成器播种一次。
注意:我们还没有讨论过将数组传递给函数。因此,对于此作业,您可以使 Dice throws 数组成为全局数组。
//我只是对将随机生成的数字添加到数组然后通过自上而下方法对其进行平均的概念感到困惑。
#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;
}
您的代码有点难以理解,但我想我明白发生了什么。让我们从您的 Gen_20_Throws_Of_2_Die_And_Add_Values
方法开始。
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;
}
您不一定需要在 for 循环之外初始化 int Dice_Throw_Number
。随意将它放在 for 循环中。此外,我个人总是发现从零开始并只达到 < 条件而不是 <= 更容易理解。所以你将拥有:
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;
}
现在在您的 Output_Average
函数中,您的逻辑有很大偏差。你想计算投掷 2 个骰子的平均结果是多少?现在你只是添加某个总数出现的次数,而不是总数本身。因此,例如,如果您掷出 12 5 次,您将向 Total_Of_20_Rolls
添加 5,而不是 60。这很容易更改,您只需要相乘即可。
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;
这应该能帮到你!
HW Question:我们将模拟掷骰子。我们将再次使用 Top-Down_Design 来提高可读性等。
生成 20 次骰子投掷两个骰子。每个骰子可以产生从 1 到 6 的点数。将两个数字相加得到掷出的值。
在一次传递中生成 20 次掷球并将数字存储在数组中。
在第二遍计算数字的平均值并将其显示在控制台上。
在获得任何随机数之前,用 8193 为随机数生成器播种一次。
注意:我们还没有讨论过将数组传递给函数。因此,对于此作业,您可以使 Dice throws 数组成为全局数组。
//我只是对将随机生成的数字添加到数组然后通过自上而下方法对其进行平均的概念感到困惑。
#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;
}
您的代码有点难以理解,但我想我明白发生了什么。让我们从您的 Gen_20_Throws_Of_2_Die_And_Add_Values
方法开始。
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;
}
您不一定需要在 for 循环之外初始化 int Dice_Throw_Number
。随意将它放在 for 循环中。此外,我个人总是发现从零开始并只达到 < 条件而不是 <= 更容易理解。所以你将拥有:
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;
}
现在在您的 Output_Average
函数中,您的逻辑有很大偏差。你想计算投掷 2 个骰子的平均结果是多少?现在你只是添加某个总数出现的次数,而不是总数本身。因此,例如,如果您掷出 12 5 次,您将向 Total_Of_20_Rolls
添加 5,而不是 60。这很容易更改,您只需要相乘即可。
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;
这应该能帮到你!