DESCRIPTION Write a C++ program that simulates the rolling of 100 dice and store
ID: 3756264 • Letter: D
Question
DESCRIPTION Write a C++ program that simulates the rolling of 100 dice and stores the tallies of how many 1's, 2's, 3's, 4's, 5's, and 6's in an array. It then prints out the results as a histogram of 6 bars, one for each side. Follow the following steps: 1 Add the following scoped enumeration and structure at the top of your program. enum class Side l: struct Bar ONE, TWO, THREE, FOUR, FIVE, SIX int value Side label; 2. Define a function named rollDice () with the following prototype: void rollDice (Bar[) h, int n - 100); This function calls the built-in function rand) defined in the header file in 2's, 3's, 4's, 5's, and 6's happen are saved into the value member of each bar element of the array h In C++, to generate random numbers between 0 and an integer n -1 use the expression: rand () % n. See example Files/example_programs/chpo5/ex10-coin_toss.cpp for an example of using rand )Explanation / Answer
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <stdlib.h>
#include<time.h>
#define MAX 100 // Defines maximum size
using namespace std;
// Defines an enumeration
enum Side{ONE, TWO, THREE, FOUR, FIVE, SIX};
// Defines a structure
struct Bar
{
int value;
Side label;
};// End of structure
// Function to generate random number between 1 - 6
// Number of times based on parameter n value
void rollDie(Bar h[], int n = 100)
{
// Use current time as seed for random generator
srand(time(0));
// Loops till n times
for(int x = 0; x < n; x++)
{
// Generates random number between 1 and 6
h[x].value = rand() % 6 + 1;
// Assigns number stored at h index position of value to
// h index position of label after converting it to enumeration type Side
h[x].label = (Side)h[x].value;
}// End of for loop
}// End of function
// Function to calculate frequency and generate histogram
string getHistogram(Bar h[], int n, char c = '*')
{
// Creates an array to store frequency and assigns zero
int frequency[6] = {0,0,0,0,0,0};
// Creates a string to store histogram assigns null initially
string histogram = "";
// Creates a string array to store the labels
string label[] = {"One: ", "Two: ", "Three: ", "Four: ", "Five: ", "Six: "};
// Loops n times
for(int x = 0; x < n; x++)
{
// Checks the label value stored at x index position of object h of structure Bar
// Minus one because enumeration constant starts from 0
switch(h[x].label-1)
{
case ONE:
frequency[ONE]++;
break;
case TWO:
frequency[TWO]++;
break;
case THREE:
frequency[THREE]++;
break;
case FOUR:
frequency[FOUR]++;
break;
case FIVE:
frequency[FIVE]++;
break;
case SIX:
frequency[SIX]++;
break;
}// End of switch case
}// End of for loop
// loops 6 times for frequency array
for(int x = 0; x < 6; x++)
{
// Declares an object of stringstream class
stringstream ss;
// Concatenates the label at index position x to histogram
histogram += label[x];
// Converts the integer number stored at x index position of frequency array
ss << frequency[x];
// Loops till the value of x index position of frequency array to generate character parameter '*'
for(int y = 0; y < frequency[x]; y++)
// Concatenates the character parameter to histogram
histogram += c;
// Concatenates the frequency number in string form
histogram = histogram + "(" + ss.str() + ") ";
}// End of for loop
// Returns the histogram string
return histogram;
}// End of function
// Function to display menu, accepts user choice and returns choice
char menu()
{
// To store user choice
char ch;
// Displays menu
cout<<" DICE ROLLING SIMULATION";
cout<<" ==============================";
cout<<" r. Roll dice.";
cout<<" h. Display histogram.";
cout<<" q. Quit program.";
// Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
// Returns user choice
return ch;
}// End of function
// main function definition
int main()
{
// Declares an array of object of structure Bar of size MAX
Bar histogram[MAX] = {0, (Side)0};
// Loops till user choice is not 'q' or 'Q'
do
{
// Calls the function menu() to accept user choice
// Based on the return value calls appropriate function
switch(menu())
{
case 'r':
case 'R':
// Calls the function to generate random roll of dice and stores in histogram array of object
rollDie(histogram, 100);
break;
case 'h':
case 'H':
// Calls the function to generate histogram string and displays it
cout<<getHistogram(histogram, 100);
break;
case 'q':
case 'Q':
exit(0);
default:
cout<<" Invalid choice.";
}// End of switch case
}while(1); // End of do while loop
}// End of main function
Sample Output:
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: h
One: (0)
Two: (0)
Three: (0)
Four: (0)
Five: (0)
Six: (0)
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: r
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: H
One: ****************(16)
Two: *****************(17)
Three: ******************(18)
Four: ******************(18)
Five: **************(14)
Six: *****************(17)
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: R
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: h
One: *******************(19)
Two: *************(13)
Three: ****************(16)
Four: *****************(17)
Five: ****************(16)
Six: *******************(19)
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: t
Invalid choice.
DICE ROLLING SIMULATION
==============================
r. Roll dice.
h. Display histogram.
q. Quit program.
Enter your choice: q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.