C++ C++;虚拟高尔顿板
我在创建Galton Board的过程中已经走了这么远,但是我不知道如何修复我的代码,因为j和[j]列不在主范围内。我知道我没有在main函数中声明它们,但不确定如何正确地这样做以获得结果 我如何将其修复,以便输出我的意图 我的意图很简单,当代码运行以输出我的数据时,如下所示: 插槽0中的大理石数量为2。 插槽1中的大理石数量为4。 以此类推,直到7 以下是我的代码:C++ C++;虚拟高尔顿板,c++,C++,我在创建Galton Board的过程中已经走了这么远,但是我不知道如何修复我的代码,因为j和[j]列不在主范围内。我知道我没有在main函数中声明它们,但不确定如何正确地这样做以获得结果 我如何将其修复,以便输出我的意图 我的意图很简单,当代码运行以输出我的数据时,如下所示: 插槽0中的大理石数量为2。 插槽1中的大理石数量为4。 以此类推,直到7 以下是我的代码: #include <iostream> #include <ctime> using namespace
#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;
}
#包括
#包括
使用名称空间std;
//随机决定L或R方向
char dropMarble()
{
//如果是1,那么是L
如果(rand()%2)
{
返回“L”;
}
//如果是2,那么是R
其他的
{
返回'R';
}
}
void dropMarble(int列[],int列)
{
int i=0,j=0;
运煤车;
而((i+j)!=cols)
{
LorR=滴落大理石();
如果(罗尔=‘R’)
{
//大理石是对的
++j;
}
其他的
{
//大理石向左走
++一,;
}
}
cout不太确定您想要实现什么,但您可以尝试像这样重写第二个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);
}
for(intj=0;j这就是你想要做的吗
#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;
}
有趣的想法。关键的变化实际上是通过引用传递一个向量。你不必使用向量,你可以通过一个指针解析一个数组,这个指针也可以工作
另一件事是更改大理石打印逻辑,使其处于自己的循环中,并将列数而不是大理石数解析为其他函数。请澄清道尔顿板的定义。我的互联网搜索没有显示任何物理板:-(它也被称为豆子机。这似乎是它更为常见的名称。不相关(不会影响此程序的运行,但下次您可能会感到意外)://如果2,则R
不正确。rand()%2
的输出将是[0,1]。rand()%N
将导致[0,N],假设Ncolumns
和j
用于引用dropMarble
和printColumns
函数中的内容。这些变量仅限于在其中声明的函数,因此不能在main()中使用它们
。改用slot
。因此,当我运行代码时,它会多次输出数据。如何停止此操作并让它只输出一次?我已经修改了代码,您只需将内部for循环移动到外部for循环之后,还需要将printColumns函数移出!
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