summaryrefslogtreecommitdiffstats
path: root/Bachelor/Prog1/Prakt4/prg1p4_3/main.cpp
blob: fb7e5649844ca745a52ed04f39fea7d4ec094834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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