Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in C++, thanks. Your completed program should simulate rolls of several pairs of dice and store them in a data file, and then it should
in C++, thanks.
Your completed program should simulate rolls of several pairs of dice and store them in a data
file, and then it should read from the data file to produce a histogram file with a simple text representation of a histogram. The simulation is already done for you; your task is to produce a histogram, formatted as on the reverse sheet. You are encouraged to use the helper functions in the starter file as guidance, but you dont have to as long as you produce a correct histogram.
Some useful code:
// Starter file
#include
#include
#include
#include
using namespace std;
const int N_ROLLS = 200; // how many rolls to simulate
const string DATA_FILENAME = "data.txt";
const string HIST_FILENAME = "hist.txt";
int getRoll() {return (rand()%6+rand()%6+2);}
// returns the value (between 2 and 12) of a toss of a pair of random dice
void simulateAndRecord();
// Pre: No meaningful data is in either file
// Post: Results of simulation of N_ROLLS pairs of dice are in DATA_FILENAME
void produceHistogram();
// Pre: DATA_FILE contains (at most) N_ROLLS values 2-12;
// HIST_DATA has no meaningful info; both files are closed
// Post: For each val 2-12, HIST_FILE contains a line of stars
// counting the # times that val appears in DATA_FILE
void writeLineOfStars(int rolls[], int nRolls, int val, ostream& out);
// Pre: rolls array contains nRolls simulations; out is an opened output file
// Post: The val line of the histogram has been appended to out, e.g., if val=3
// and 3 occured 5 times, the line would be "3: *****"
int main() {
simulateAndRecord();
produceHistogram();
return 0;
}
// simulate rolling N_ROLLS pairs of dice; record values in DATA_FILENAME
void simulateAndRecord() {
ofstream datafile(DATA_FILENAME); // open DATA_FILENAME
for (int i=0; i
datafile << getRoll() << endl; // simulate 1 roll and output it to datafile
}
datafile.close(); // close the file
}
void produceHistogram() {
/* TODO: implement this function; define writeLineOfStars helper */
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started