I'm trying to create a program that rolls a die a few thousand times and print out how many times it lands on each face and then print out a graph using x's as a visual representation using classes.
I need a method called int count(int face) inside my class that returns the number of times a particular face has appeared, I just don't know how to incorporate this. I think I should use a constructor and destructor but i'm new with those and knowing when to actually use them.
Here is what I have so far that shows how it should look when printed, just struggling to convert using a class.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
usingnamespace std;
class aDie {
public:
int roll();
int count(int face);
private:
int numRolled;
int faceRolled;
};
int aDie::roll() {
numRolled = ((rand() % 6) + 1);
return numRolled;
}
int aDie::count(int face) {
???
}
int main() {
srand(time(NULL));
int numRolls = 12000;
int dieSides = 6;
vector<int> face(dieSides);
vector<int> copyFace(dieSides);
aDie getRan; //creates object getRan
for (int i = 0; i < numRolls; ++i) { //gets 12000 rolls
++face.at(getRan.roll() - 1); // increments element in vector that matches roll
}
for (int i = 0; i < dieSides; ++i) {
copyFace.at(i) = face.at(i); //copies each element of vector face into copyFace so that it can be
} //manipulated without affecting the original vector
for (int i = 0; i < (dieSides - 1); ++i) {
if (face.at(i) > copyFace.at(i + 1)) { // compares the first element with the next to see which is bigger
int temp = copyFace.at(i); //if it is bigger, swap elements
copyFace.at(i) = copyFace.at(i + 1);
copyFace.at(i + 1) = temp;
}
}
int average = 0;
average = copyFace.at(5) / 60; //takes highest face rolled and finds ratio relative to 60
for (int i = 0; i < dieSides; ++i) {
cout << (i + 1) << " --- " << face.at(i) << " ";
face.at(i) = face.at(i) / average; //converts each element to its conversion ratio so that the highest number is 60
for (int j = 0; j <= face.at(i); ++j) {
cout << "x"; //prints and x each for every "average" amount of stars so that highest count = 60 x's
if (j == face.at(i)) { //after last x is printed, ends line for next element in vector face
cout << endl;
}
}
}
return 0;
}