Written by 1:35 pm School • One Comment

Simple C++ Bean machine program @KTSI

This is a simple Bean machine program done in C++ for KTSI.

This is how it works, you drop 100 balls and they fall in 6 different boxes.

Bean MachineAnd this is how the C++ output should look like:

Bean Machine outputAnd here is how its done:

#include <iostream>
#include <cstdlib>
#include <ctime>
 
using namespace std;
 
int main (int argc, char * const argv[]) {
 
 
	// initial
	int ballCount = 100;
	int box[6] = { 0, 0, 0, 0, 0 };
	string line = " +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-->";
	string numbers = " 0     5     10     15     20     25     30     35     40     45     50";
	int leftorright = 0;
 
	// Need for Random Numbers
	srand ( time(NULL) );
 
	// Rund ballCount (100) zahl = rand() % 2;
	for (int i=0; i<ballCount; i++) {
			int counter = 0;
 
		for (int j=0; j<5; j++) {
			// left or right
			leftorright = rand() % 2;
			counter = counter + leftorright;
		}
		box[counter] = box[counter]++;
 
	}	
 
	// Output
	cout << numbers << endl << line << endl;
	for (int t=0; t<6; t++) {
		cout << t << " |";
		for (int u=0; u < box[t]; u++) {
			cout << "#";
		}
		cout << " " << box[t] << endl << line << endl;
	}
	return 0;
}

I also did this program in Powershell

Tags: , , , , , , Last modified: September 28, 2010
Close Search Window
Close