// Programmieren 1, Praktikum 4, Aufgabe 4 // Sven Eisenhauer // 14.12.2004 // // file: main.cpp // // purpose: Simulate a Galton Board // #include using std::cin; using std::cout; using std::endl; #include using std::setprecision; using std::setw; // contains function prototypes for functions srand and rand #include #include const int maxBoxes=100; const int minBoxes=1; int fall(int &,int &); void getInput(int &,int &, char &); void textOutput(int[], const int, const int); void graphicalOutput(int[], const int, const int); int main() { srand(time(0)); int nrBalls; int minBox,maxBox,nrBox; int box; int result[maxBoxes+1]={0}; char outputMode; getInput(nrBalls,nrBox,outputMode); for (int i=1;i<=nrBalls;i++) { minBox=minBoxes; maxBox=nrBox; box=fall(minBox,maxBox); result[box]++; } if (outputMode=='t' || outputMode=='T') textOutput(result,nrBalls,nrBox); else graphicalOutput(result,nrBalls,nrBox); return 0; }// end function main void getInput(int& nrBa, int& nrBo, char &outpMode) { cout << "Please enter number of boxes (max." << maxBoxes <<"): "; cin>>nrBo; cout << "Please enter number of balls: "; cin>>nrBa; cout << "Please enter output mode: [T/t] table, [G/g] graphical: "; cin >> outpMode; }// end function getInput int fall(int &minB, int &maxB) { // if we are not over last 2 boxes if((maxB-minB)>1) { if (0==rand()%2) // ball falls to the right => we cannot reach the most left box anymore minB++; else // ball falls to the left => we cannot reach the most right box anymore maxB--; fall(minB,maxB); //return 0; } // only 2 possible boxes left... the base case else { if (0==rand()%2) // left box return minB; else // right box return maxB; } } void textOutput(int resArray[], const int balls,const int nrBox) { //int sum=0; cout << endl << setw(10) << "Box" << setw(10) << "Balls" << setw(16) << "Norm." <(resArray[n])/balls<(((static_cast (resArray[n]))/balls)*maxColumns);i++) cout<<"*"; cout << endl; } }// end function graphicalOutput