C ++虚拟高尔顿板

时间:2018-07-18 15:48:27

标签: c++

我在创建我的高尔顿板时已经走了很远,但是却不了解如何解决不在j和column [j]范围之内的代码。我知道我尚未在main函数中声明它们,但不确定如何正确地获取它们。

如何解决此问题,使其输出我的意图?

我的意图仅仅是在代码运行时输出我的数据,如下所示:

0号插槽中的大理石数量为2。 插槽1中的弹珠数量为4。 直到7。

这是我的代码:

#include <iostream>
#include <ctime>
using namespace std;

//Decide a L or R direction at random
char dropMarble()
{
    //If 1 then L
    if(rand() % 2)
    {
        return 'L';
    }

    //If 2 then R
    else
    {
        return 'R';
    }
}

void dropMarble(int columns[], int cols)
{
    int i = 0, j = 0;
    char LorR;

    while((i + j) != cols)
    {
        LorR = dropMarble();

        if(LorR == 'R')
        {
        //Marble goes right
             ++j;
        }

        else
        {
        //Marble goes left
            ++i;
        }
    }

cout << endl;

//Increment the count of marbles in the columns

++columns[j];
}

void printColumns(int columns[], int cols)
{
    for(int i = 0; i< cols; ++i)
    {
         cout << columns[i] << "";
    }
    cout << endl;
}

int main()
{
    srand(time(NULL));
    int numberOfMarbles;
    int numberOfColumns = 8;
    int slots[numberOfColumns];

    //Initialize the count of marbles in the columns to zero
    for(int i = 1; i <= numberOfColumns; ++i)
    {
        slots[i] = 0;
    }
    cout << "Enter the number of marbles to drop: " << endl;
    cin >> numberOfMarbles;

    for(int i = 1; i <= numberOfMarbles; ++i)
    {
        cout << "The number of marbles in slot " << j << " is " << columns[j] 
         << endl;

        dropMarble(slots, numberOfMarbles);

        printColumns(slots, numberOfMarbles);
     }
    return 0;
}

2 个答案:

答案 0 :(得分:0)

不太确定要实现什么,但是您可以尝试像这样重写2nd for循环-

 for(int j = 0; j <= numberOfColumns; ++j) {
        cout << "The number of marbles in slot " << j << " is " << slots[j] 
         << endl;

       dropMarble(slots, numberOfColumns);

        printColumns(slots, numberOfColumns);
  }

答案 1 :(得分:0)

这是您要做什么吗?

#include <iostream>
#include <ctime>
#include <vector>
using namespace std;

//Decide a L or R direction at random
bool dropMarble()
{
  if(rand() % 2)
    {
      return false;
    }
  else
    {
      return true;
    }
}

void dropMarble(vector<int> &columns, int cols)
{
  int i = 0, j = 0;
  bool GoneRight=true;

  while((i + j) != cols)
    {
      GoneRight = dropMarble();

      if(GoneRight)
        {
      //Marble goes right
      ++j;
        }

      else
        {
      //Marble goes left
      // i is height small i is high, large i is low
      ++i;
        }
    }

  //Increment the count of marbles in the columns
  ++columns[j];
}

void printColumns(vector<int> &columns, int cols)
{
  for(int i = 0; i< cols; ++i)
    {
      cout << columns[i] << "";
    }
  cout << endl;
}

int main()
{
  srand(time(NULL));
  int numberOfMarbles;
  int numberOfColumns = 8;
  vector<int> slots;
  slots.resize(8);

  //    int columns[numberOfColumns];

  //Initialize the count of marbles in the columns to zero
  for(int i = 1; i <= numberOfColumns; ++i)
    {
      slots[i] = 0;
    }
  cout << "Enter the number of marbles to drop: " << endl;
  cin >> numberOfMarbles;

  for(int i = 1; i <= numberOfMarbles; ++i)
  {    
      dropMarble(slots, numberOfColumns);
  }

  for(int j = 0; j<numberOfColumns;++j){
    cout << "The number of marbles in slot " << j << " is " << slots[j] 
     << endl;
  }

  printColumns(slots, numberOfColumns);
  return 0;
}

带1000颗弹珠:

The number of marbles in slot 0 is 7
The number of marbles in slot 1 is 30
The number of marbles in slot 2 is 100
The number of marbles in slot 3 is 213
The number of marbles in slot 4 is 262
The number of marbles in slot 5 is 225
The number of marbles in slot 6 is 122
The number of marbles in slot 7 is 34

73010021326322512234

有趣的想法。真正的关键改变是通过引用传递一个向量。您不必使用向量,可以通过也可以使用的指针解析数组。

另一件事是更改大理石打印逻辑,使其处于自己的循环中,并且还要解析列数而不是其他功能的大理石数。