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_3/main.cpp | 102 ++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp (limited to 'Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp') 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 + +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 + +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. "<> 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 (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 (resArray[n]))/nrRolls)*maxColumns);i++) + cout<<"*"; + cout << endl; + } +}// end function printResultGrph \ No newline at end of file -- cgit v1.2.3