How do I find the amount of times a Die landed on a certain face thats been roll
ID: 3567857 • Letter: H
Question
How do I find the amount of times a Die landed on a certain face thats been rolled X number times using a class in c++ programming?
I have a program that rolls a dice a few thousand times, randomly generating a number through a class each time and then storing each roll in a vector. How do I create a method inside the same class that will return the number that a particular side has appeared? Method will be called count(int) as in int count(int face). This is my code so far but I dont know how to do the next step.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
class aDie {
public:
int roll();
private:
int numRolled;
} ;
int aDie::roll() {
numRolled = (rand() % 6) + 1;
return numRolled;
}
int main() {
srand(time(NULL));
vector<int> totalRolls(5000);
aDie getRan;
for (int i = 0; i < 5000; ++i) {
getRan.roll();
totalRolls.at(i) = getRan.roll();
cout << totalRolls.at(i) << endl;
}
}
return 0;
}
Explanation / Answer
class aDie {
public:
int roll();
private:
int numRolled;
int countOfRoll[6];
} ;
int aDie::roll() {
numRolled = (rand() % 6) + 1;
countOfRoll[nulRolled]++; //this variable will keep count of how many times that face landed.
return numRolled;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.