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_3/main.cpp | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp')
| -rw-r--r-- | Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp b/Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp new file mode 100644 index 0000000..fb7e564 --- /dev/null +++ b/Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp @@ -0,0 +1,102 @@ +// Programmieren 1, Praktikum 4, Aufgabe 3
+// Sven Eisenhauer
+// 14.12.2004
+//
+// file: main.cpp
+//
+// purpose: Analyse multiple rolls of multiple dice
+//
+
+#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>
+
+void roll(const int, const int, int[]);
+void getPara(int &, int &);
+void printResultTbl(const int[], const int, const int);
+void printResultGrph(const int[], const int, const int);
+
+const int maxDice=20,
+ maxEyes=6,
+ minEyes=1;
+
+int main()
+{
+
+
+ int nrDice,
+ nrRolls;
+
+ int result[(maxDice*maxEyes)+1]={0};
+
+ // initialize random number generator
+ srand(time(0));
+
+ getPara(nrDice,nrRolls);
+
+ roll(nrDice,nrRolls,result);
+
+ printResultTbl(result, nrDice, nrRolls);
+ printResultGrph(result, nrDice, nrRolls);
+
+ return 0;
+}// end main
+
+void getPara(int &nrDice,int &nrRolls)
+{
+ cout << "Please enter number of dice to roll (max. "<<maxDice<<"): ";
+ cin >> nrDice;
+ cout << "Please enter number of rolls: ";
+ cin >> nrRolls;
+} // end function getPara
+
+void roll (const int nDice, const int nrRolls, int resultArray[])
+{
+ int sum;
+
+ for (int n=1;n<=nrRolls;n++)
+ {
+ sum=0;
+ for (int i=1;i<=nDice;i++)
+ sum+=(rand()%6)+1;
+ ++resultArray[sum];
+ }
+
+} // end fnction roll
+
+void printResultTbl(const int resArray[],const int nrDice, const int nrRolls)
+{
+ cout << setw(5) << "Sum" << setw(12) << "Frequency" << setw(20) << "norm. Frequency"<< endl;
+
+ for (int n=(nrDice*minEyes);n<=(nrDice*maxEyes);n++)
+ {
+ cout << setw(5) << n << setw(12) << resArray[n] << setprecision(2)<< setw(20) << (static_cast <double> (resArray[n]))/nrRolls << endl;
+ }
+}// end function printResultTbl
+
+void printResultGrph(const int resArray[],const int nrDice, const int nrRolls)
+{
+ cout << endl;
+
+ const int maxColumns=75;
+
+ for (int n=(nrDice*minEyes);n<=(nrDice*maxEyes);n++)
+ {
+ cout << setw(2) << n << ": ";
+ for (int i=0;i<static_cast <int>(((static_cast <double> (resArray[n]))/nrRolls)*maxColumns);i++)
+ cout<<"*";
+ cout << endl;
+ }
+}// end function printResultGrph
\ No newline at end of file |
