diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp')
| -rw-r--r-- | Bachelor/Prog1/Prakt4/prg1p4_4/main.cpp | 122 |
1 files changed, 122 insertions, 0 deletions
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 <iostream>
+
+using std::cin;
+using std::cout;
+using std::endl;
+
+#include <iomanip>
+
+using std::setprecision;
+using std::setw;
+
+// contains function prototypes for functions srand and rand
+#include <cstdlib>
+
+#include <ctime>
+
+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." <<endl;
+ for (int n=1;n<=nrBox;n++)
+ {
+ cout << setw(10) << n << setw(10) << resArray[n] << setw(16)<<static_cast <double>(resArray[n])/balls<<endl;
+ // sum+=resArray[n];
+ }
+ //cout << endl << "Sum: " << sum << endl << endl;
+}// end function textOutput
+
+void graphicalOutput(int resArray[], const int balls, const int nrBox)
+{
+ const int maxColumns=75;
+
+ for (int n=1;n<=nrBox;n++)
+ {
+ cout << setw(2) << n << ": ";
+ for (int i=0;i<static_cast <int>(((static_cast <double> (resArray[n]))/balls)*maxColumns);i++)
+ cout<<"*";
+ cout << endl;
+
+
+ }
+}// end function graphicalOutput
|
