2 Replies - 773 Views - Last Post: 21 May 2013 - 05:52 PM Rate Topic: -----

#1 Austen944  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 08-February 13

C++ "Bean Machine Game" Problem printing histogram

Posted 21 May 2013 - 02:50 PM

Hello, Just started learning C++ not too long ago so my coding is not very good

Anyways, the problem I have to do is to simulate the "Bean Machine Game" or rather the output of it where balls are dropped from the opening of a board. Every time a ball hits a nail, it has a 50% chance to fall to the left or to the right. The piles of balls are accumulated in the slots at the bottom of the board.

I must display the path of each ball (eg.LLLRLRR) and the final buildup of the balls in the slots in a histogram.


An example of the output :

LRLRLRR
RRLLLRR
LLRLLRR
RRLLLLL
LRLRRLR

O
O
OOO

Also, the number of R's in a path is the position of the slot where the ball falls. Eg. LRLRLRR is slot 4


Here's my code so far


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

int path (int numberOfSlots);
int max (int slots []);
void printHistogram (int slots []);

//Main function
int main ()
{
	int numberOfBalls;
	int numberOfSlots;
	const int size = 50;
	int slots[size];

	//Get number of balls
	cout << "Enter the number of balls to drop: " << endl;
	cin >> numberOfBalls;

	//Get number of slots
	cout << "Enter the number of slots: " << endl;
	cin >> numberOfSlots;

	//Print the paths
	for (int i = 0; i < numberOfBalls; i++)
	{
		slots[path(numberOfSlots)]++;
	}

	//Print the histogram
	printHistogram(slots);
}

//Function to determine position and print the paths
int path (int numberOfSlots)
{
	int position = 0;

	for (int i = 1; i < numberOfSlots; i++)
	{
		int direction = rand() % 2;
		if (direction == 1)
		{
			cout << "L";
		}
		else
		{
			cout << "R";
			position++;
		}
	}
	cout << endl;

	return position;
}


//Function to print the histogram
void printHistogram (int slots [])
{
	int maxSlotHeight = max(slots);
	for (int i = maxSlotHeight; i > 0; i--)
	{
		for (unsigned int j = 0; j < sizeof(slots)/sizeof(slots[0]); j++)
		{
			if (slots[j] < i)
				cout << " ";
			else
				cout << "O";
		}
		cout << endl;
	}
}

//Function to get max element in slots
int max (int slots [])
{
	int result = slots[0];

	for (unsigned int i = 1; i < sizeof(slots)/sizeof(slots[0]); i++)
	{
		if (result < slots[i])
			result = slots[i];
	}
	return result;
}




I can display the paths, however I am having some difficulties with displaying the histogram correctly.
Any insight in the right direction is appreciated Q_Q

Is This A Good Question/Topic? 0
  • +

Replies To: C++ "Bean Machine Game" Problem printing histogram

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4316
  • View blog
  • Posts: 12,096
  • Joined: 18-April 07

Re: C++ "Bean Machine Game" Problem printing histogram

Posted 21 May 2013 - 03:50 PM

Well you have an array of slots which, after the paths have run their course, contain a number representing the number of balls in that slot. So all you need to do is run through the slots array, grab the number from each slot, use that to then print a string of "O" equal to that number. To help this I recommend you pass in the size of the slots array in addition to the array...

void printHistogram(int slots[], int sizeOfSlots) {
   for (int i = 0; i < sizeOfSlots; i++) {
       int numOfBallsInSlot = slots[i];

       if (numOfBallsInSlot > 0) {
           for (int j = 0; j < numOfBallsInSlot; j++) {
               cout << "O";
           }
           cout << endl;
       }
       else {
           // Zero balls, so just bring a blank line
           cout << endl;
       }
   }
}



That should give you the basics to work with. Tweak it as needed. :)
Was This Post Helpful? 1
  • +
  • -

#3 Austen944  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 08-February 13

Re: C++ "Bean Machine Game" Problem printing histogram

Posted 21 May 2013 - 05:52 PM

Thank you! Got it too work. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1