掷2骰子1000次

时间:2018-07-10 03:26:52

标签: c++ dice

我的教授要求我们编写一个程序,该程序:

  1. 使用循环来模拟一对骰子的滚动一千次(这里我认为for循环会很有用)。

  2. 对于每次迭代,循环都需要计算每个值从2到12的次数(这里我在想是否可以使用/ else语句)

  3. 循环结束时,必须显示每个值(从2到12)出现的次数。

他的任务结构如下:

他希望我们使用一个进入1000次循环的函数,该函数每个函数调用两次调用另一个函数(以模拟抛出两个骰子)。

让我解释一下我已经放下的东西

//
//  main.cpp
//  RollingDice

#include <iostream>
#include <ctime>
using namespace std;
int roll();
int rollDice();

int main(int argc, const char * argv[])
{

    for (int i = 1; i < 1000; i++)
    {

        rollDice(); //This is the function that is supposed to call the roll(); 
                    //function two times. This makes sense to me that TWO DICE 
                    //are being rolled 1000 times. 

    }

    int result; //These two statements was where I was just hoping for something 
                //to work. I put these variable statements outside of the for 
                //loop because I was thinking that the int rollDice(); function 
                //definition(below) wouldn't know how to use it otherwise. I 
                //know that doesn't make sense, but I just can't explain why. 

    result = rollDice();

}

int roll()
{ //This function was provided to us by my professor. 
    static bool randomInitialized = false;

    int points;

    if (!randomInitialized)
    {
        srand((unsigned int) time(NULL));
        randomInitialized = true;

    }
    points = (rand() % 6) + 1;
    return points;
}

int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice. 
  //The return statement in this function was my attempt of returning the sum
  //of the values of the two dice.
    roll();
    roll();

    return result;
}

除了程序的这一部分无法正常工作外,我还有另一个问题是确定一种为出现的每个值都拥有一个计数器的方法(但是,我正在想象程序的这一部分属于for循环。几乎所有我所知道的。)。自昨天以来,我一直在对该程序进行深思。我今天回到它,希望能有一个新的思路来解决它,但是我仍然在努力。任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:2)

表达式roll()的计算结果为数字。要添加数字,我们使用+。要返回一个值,我们使用return

将它们放在一起,我们得到了一个简单的功能,可以将两卷相加

int rollDice() { return roll() + roll(); }

如果您对事物进行了序列编号,并且数字彼此靠近且从0开始,则标准库的SequenceContainer之一适合整个序列。

在这里,事情是特定掷球的计数。我们预先确切知道可用值(包括2-12),因此std::array是合适的。任何至少可容纳1000的整数值都适合进行计数。我在这里选择std::size_t

#include <array>
#include <iostream>

int main()
{
    std::array<std::size_t, 13> counts {}; 

这将给我们13个0,从位置0开始

    for (std::size_t i = 0; i < 1000; ++i)
    {
         ++counts[rollDice()]; 

我们用rollDice选择哪个数字,并用它来选择要递增的计数

    }

    for (std::size_t i = 2; i < 13; ++i)
    {

我们现在可以遍历结果,显示计数

         std::cout << "The count for " << i << " is " << counts[i] << std::endl;
    }
}

答案 1 :(得分:1)

1-使用地图计算每个数字从2到12的次数:(最实用)

int sumOfDice;
map <int,int> diceOccurances;
for (int i=0; i < 1000; i++)
{ 
    sumOfDice=rollDice();
    diceOccurances[sumOfDice];
    // Here you are storing how many times each of the dice values occured. Here's 
    // how you access the map;
}
for (auto const& x : socks)
{
        cout <<" Dice Total Number: " << x.first ;
        cout <<" Dice Number of Occurances: "<< x.second<<endl;
}
int rollDice()
{ //This I wrote myself. I'm imagining this is how you call a function twice. 
  //The return statement in this function was my attempt of returning the sum
  //of the values of the two dice.
    int die1,die2;
    die1= roll();
    die2= roll();
    result = die1+die2;
    return result;
}

2-使用if / else(或switch);

int two, three, four, five, six, seven, eight, nine, ten ,eleven ,twelve;
two=three=four=five=six=seven=eight=nine=ten=eleven=twelve=0;
for (int i=0; i < 1000; i++)
    { 
        if ( rollDice()==2) two++;
        if (rollDice()==3) three++;
        if (rollDice()==4) four++;
        // and so forth until twelve++;
    }
int rollDice()
    { //This I wrote myself. I'm imagining this is how you call a function twice. 
      //The return statement in this function was my attempt of returning the sum
      //of the values of the two dice.
        int die1,die2;
        die1= roll();
        die2= roll();
        result = die1+die2;
        return result;
    }

答案 2 :(得分:0)

您可以执行以下操作(需要进行一些更改才能适合您Proff的其余代码)

int rollDice()

int main(int argc, const char * argv[])
{
  srand(time(0));// randomly seed every time you run the code
  int dice1;
  int dice2;
  int storageData[11];//stores the number of times the values from 2 to 12   appears;
  for(int i=0; i<1000; i++)
  {
    dice1=rollDice();
    dice2=rollDice();
    int sum=dice1+dice2;
    storageData[sum-2]+=1;  // updates the number of times the sum has appeared.

  }
cout << "the value x has appeared "<< storageData[x-2] <<" times"<< endl; // change the value of x depending on which sum number you want.
}

int rollDice()
{
  int x=rand()%6+1;// rand()%6 produces a no. from 0-5. rand()%6+1 produces a number from 1-6.
  return x;
}

注意,在上面的代码中,我们对每个元素cos减去(-2),总和从2开始而不是0。

相关问题