Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Exercise Chapter 7 Problem 17 Create a virtual. digital Galton Board

ID: 3914080 • Letter: P

Question

Programming Exercise Chapter 7 Problem 17

Create a virtual. digital Galton Board.

Your code will use a function - char dropMarble() to determine the marble’s direction when it hits any individual peg. It will use input from the user to determine how many marbles are dropped. Your virtual Galton Board will have 7 rows of pegs and 8 slots for the marbles to drop into. The information about how many marbles will drop into each slot will be stored in an array – int slots[8]. Your code will output the contents of each slot after all the (virtual) marbles have dropped. You will need to use selection structures, loops, and nested loops to solve this problem.

I am stuck because all I need to output is that for slot 0-8 there are # of balls in each depending on how many balls you drop. I think I am trying to make it more complicated than it should be.

Here is what I have so far:

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

//Return a L or R at random
int marblePos(string str, char r)
{
int count = 0;

for(int i = 0; i < str.size(); i++)
{
if(str[i] == r)count++;
}

return count;
}

char dropMarble(int slot[8], int numOfSlots)
{
char marblePath = "";
for(int i = 0; i < numberOfSlots - 1; i++)
{
int r = (rand() * 10 + 1) % 2;

if (r > 0)
{
marblePath + "R";
}
else
{
marblePath = marblePath + "L";
}
}
int position = marblePos(marblePath, 'R');
slot[position]++;

return marblePath;
}

bool isRowEmpty(int slots[8], int rowNumb, int numberOfSlots)
{
for (int i = 0; i < numberOfSlots; ++i)
{
if(slots[i] != 0)
{
return false;
}
}
return true;
}

bool isRowEmpty(int slots[8], int rowNumb)
{
for(int i = 0; i < numberOfSlots; ++i)
{
if(slots[i] == rowNum)
{
return false;
}
}
return true;
}

void arrayOfIntegers(int slots[], int numberOfMarbles, int numberOfSlots)
{
while(!isEmpty(slots, numberOfSlots))
{
if(isRowEmpty(slots, numberOfBalls, int numberOfSlots))
{
numberOfMarbles--;
continue;
}

numberOfMarbles --;
cout << "" << endl;
}
}

int main()
{
srand(time(NULL));

int numberOfMarbles;
int numberOfRows = 7;
int arrayOfIntegers;
char marblePath;


cout << "Enter the number of balls to drop: ";
cin >> numberofBalls;

string marblePaths[numberOfMarbles];

int slots[8];

for(int i = 0; i < numberOfSlots; ++i)
{
slots[i] = 0;
}

for (int i = 0; i < numberOfMarbles; i++)
{
marblePath[i] = dropMarble(slots, numberOfSlots);

}

cout << "" << endl;

print arrayOfIntegers(slots, numberOfMarbles, numberOfSlots);

return 0;
}

Explanation / Answer

Yes, it can be done in a much simpler way. If you notice the sum of number of movements the marble makes to reach any column will always be 8. Meaning, say a marble is dropped and it reaches column #2, for this to happen the marble can take the path L R L L L L L L (The number of movements is 8).

So "i" being the number of movements in the Left direction and "j" being the number of movements in the Right direction, if (i + j) is 8, it means that the marble has reached a particular column. Now if you count the columns from the left, the marble has only moved right once, so the marble should fall in the column #2. If the marble had taken the path as L L L L L L L L then the marble would fall in column #1. The number of "Right" moves the marble makes determines the column in which the marble is gonna fall into.

I coded up a simpler implementation of the problem. I have also added comments to explain what each function does

#include <iostream>
using namespace std;

char direction(){
   //decide the direction the marble is gonna fall
   if(rand() % 2){//rand() % 2 can take values 0 or 1.
       return 'L';//if 1 then return L
   }
   else{
       return 'R';//if 0 then return R
   }
}

void dropMarble(int columns[], int cols){

   int i = 0, j = 0;
   char LorR;
  
   while((i + j) != cols){
      
       LorR = direction();
      
       cout << LorR << " ";//printing the path of the marble (remove this line if you don't want to print the path)
      
       if(LorR == 'R'){
           ++j;//the marble goes right
       }
       else{
           ++i;//the marble goes left
       }
   }

   cout << endl;

   ++columns[j];//increment the count of the marbles in the column

}

void printcolumns(int columns[], int cols){//printing the number of marbles in each columns
   for(int i = 0; i < cols; ++i){
       cout << columns[i] << " ";
   }
   cout << endl;
}

int main(){

   srand(time(NULL));  
   int numberofBalls, no_of_columns = 8;
   int slots[no_of_columns];

   //initializing the count of marbles in the columns to zero.
   for(int i = 0; i < no_of_columns; ++i){
        slots[i] = 0;
   }

   cout << "Enter the number of balls to drop: " << endl;
   cin >> numberofBalls;

   for(int i = 1; i <= numberofBalls; ++i){
       cout << "Dropping marble #" << i << endl;
       dropMarble(slots, no_of_columns);
       printcolumns(slots, no_of_columns);
   }

   return 0;
}

I hope this helps you. If you want to change the number of columns just change the value of the variable named "no_of_coumns".

I would also like to point out that the function you have written for dropMarble has a return type of char and you have defined a char variable named marblePath, but you are using mablePath like a string, which causes an error additionally single quotes are used to define a character variable and double quotes are used to define a string variable in C++.