Tag: bean machine

Simple Bean Machine program done in Powershell

Powershell Header

In the last article I posted the C++ Code for a simple Bean Machine output. Now I did the same in Powershell. I know this is not really a fantastic Powershell script, but its good to show others how things get done in Powershell.

Like in the C++ bean machine it works like this:

Bean MachineAnd the Output should look like this:

Bean Machine outputAnd here is how you do this in Powershell:

#Config
[int]$ballCount = 100
[array]$box = @(0, 1, 2, 3, 4, 5)
[string]$line = " +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-->"
[string]$numbers = " 0 5 10 15 20 25 30 35 40 45 50"
[object]$random = New-Object System.Random
 
#count
for([int]$i = 0; $i -lt $ballCount; $i++){
[int]$counter = 0
 
for([int]$j = 0; $j -lt 5; $j++){
$leftorright = $random.next(0,2)
$counter = $counter + $leftorright
}
$box[$counter] = $box[$counter] + 1
}
 
#Output
Write-Host $numbers
Write-Host $line
for ([int]$t = 0; $t -lt 6; $t++){
[string]$Statusline = ""
for ([int]$u = 0; $u -lt $box[$t]; $u++){
[string]$Statusline += "#"
}
Write-Host $t "|" $Statusline $box[$t]
Write-Host $line
}


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