From 33613a85afc4b1481367fbe92a17ee59c240250b Mon Sep 17 00:00:00 2001 From: Sven Eisenhauer Date: Fri, 10 Nov 2023 15:11:48 +0100 Subject: add new repo --- Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp | 122 ++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp (limited to 'Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp') diff --git a/Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp b/Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp new file mode 100644 index 0000000..8f481ff --- /dev/null +++ b/Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp @@ -0,0 +1,122 @@ +// 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 -- cgit v1.2.3