Give dice i, implement the function void prob3(int n, int m); lln: number of dic
ID: 3792588 • Letter: G
Question
Give dice i, implement the function void prob3(int n, int m); lln: number of dice: m: number of rolls which takes two integer parameters n and m where n is the number of dice and m is the total number of time that n dice are rolled. This function prob3 will print out the frequency counts for the sum obtained by adding the points from rolling n dice for a total of m times. Here is sample output when prob3(2, 1000000) and prob303, 10000000 are executed. "Dice.h" #ifndef DICE H #define DICE H #include Kiostream> #includeExplanation / Answer
PROGRAM CODE:
Dice.h
#ifndef DICE_H
#define DICE_H
#include <iostream>
#include <ctime>
using namespace std;
class Dice
{
int face;
int value;
public:
int getFace()
{
return face;
}
int getValue()
{
return value;
}
int roll()
{
value = (rand()%face) +1;
return value;
}
Dice()
{
face = 6;
srand((unsigned)time(NULL));
value = roll();
}
Dice(Dice& other)
{
face = other.getFace();
value = other.getValue();
}
int operator+(Dice &rhs)
{
int sum = value + rhs.getValue();
return sum;
}
};
#endif
Dice.cpp
#include <iostream>
#include "Dice.h"
using namespace std;
void prob3(int n, int m)
{
int sum[m];
int frequency[m];
int count = 0;
bool isPresent = false;
for(int i=0; i<m; i++)
{
int add = 0;
for(int i=0; i<n; i++)
{
Dice dice1;
add += dice1.roll();
}
for(int x = 0; x<count; x++)
{
if(sum[x] == add)
{
frequency[x]++;
isPresent = true;
break;
}
}
if(!isPresent)
{
sum[count] = add;
frequency[count] = 1;
isPresent = false;
count++;
}
}
for(int i=0; i<count; i++)
{
cout<<"Sum="<<sum[i]<<" Frequency: "<<frequency[i]<<endl;
}
}
int main() {
prob3(2, 1000);
prob3(3, 1000);
return 0;
}
OUTPUT:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.