Question: Working on a project in C++ and I've got 2/3 of the requirements done but I'm having trouble figuring out how to start the last
Working on a project in C++ and I've got 2/3 of the requirements done but I'm having trouble figuring out how to start the last part. It's a dice roller program for a 6 sided die. It will roll a random number then a counter will check the number and tally up and store the total number of occurances of the numbers 1 - 6 and display the results. That part I've got done and working. The last part I have to do is take those totalled numbers of each occurance and display a histogram with an x. I haven't used array's all that much and I'm not really sure how to use it to display a graph. On top of that it seems really complicated in how it needs to be done. Since the number of rolls could be any integer so the x's can't represent just a single occurance, it needs to represent multiple occurences so that it will fit in the standard number of characters a console can output.
Here are part of the instructions that deal with the histogram.
The output should also have a section displaying a histogram.
The histogram should look something like this:
1 xxxxxxxxxxxxxx
2 xxxxxxxxxxxxxx
.
.
.
6 xxxxxxxxxxxxxx
Where the x represent some number of occurrence of a particular face. Since you are likely to get 100s of occurrences of a face you will not be able to use one x for one occurrence. Therefore you will have to scale your display so that x represents some number of occurrences and so that the largest number of occurrences will fit on a standard console line of 80 characters.
You might want to find the number with the maximum occurrence and represent that maximum occurrence with 60 xs. This means that should the die side appearing the most comes out 1200 times out of 6000, you would want to represent its count with 60 xs. This means that each x, in that particular run would represent 20 occurrences of a side. Thus 1200 occurrences are represented by 20 Xs If, on the other hand, the largest number of occurrences of a particular side had been 1800, then you would need each X to represent 30 occurrences so that 1800 occurrences would be represented by 60 Xs.
Basically, you want to normalize your graph in such a way that the largest number of occurrences is represented by 60 Xs.
Make sure that your xs line up and start on the same column.
Then display the histogram obtained when you roll 2 dice and add up the numbers that come up (2 12). As in the first part, you will roll the dice N times.
Create 2 outputs as above. 1 output to display the counts for each sum and one output to display the xs for each sum.
Here is the code I have so far. I really don't know how to start making an array that would be able interpret the data into x's so if someone could point me in the right direction or give an example of how to start something like that I would appreciate it.
#include
using namespace std;
int main() {
const int MAX_SIDES = 6; //how many sides the die can have int diceRolls = 0; //user defined input for how many rolls
int count1 = 0; int count2 = 0; int count3 = 0; //count1 through count6 will keep track of what numbers are rolled int count4 = 0; int count5 = 0; int count6 = 0;
srand(time(NULL)); //makse rolls psudo random
int counter = 0;
cout << "Enter number of rolls: " << endl; //number of rolls from user cin >> diceRolls;
for (int i = 0; i < diceRolls; i++) { //takes user input for amount of rolls and begins the random generation part
int counter = rand() % MAX_SIDES + 1; //generates the numbers with respect to number of sides
if (counter == 1) { count1++; } if (counter == 2) { count2++; } if (counter == 3) { //these will check what number is generated count3++; //then increase the counter for each of the } //6 sides of the die if (counter == 4) { count4++; } if (counter == 5) { count5++; } if (counter == 6) { count6++; }
}
cout << "1 was rolled " << count1 << " times." << endl; cout << "2 was rolled " << count2 << " times." << endl; cout << "3 was rolled " << count3 << " times." << endl; //these will display results of the above cout << "4 was rolled " << count4 << " times." << endl; //if statements that increase the counter cout << "5 was rolled " << count5 << " times." << endl; cout << "6 was rolled " << count6 << " times." << endl;
int histo[6] = {count1, count2, count3, count4, count5, count6} //the start of the array reading the data collected earlier
system("pause>nul"); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
