Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please finish my code use simple c++ language for this is my second c++ course. Also, give an explanation of the writehistogram function, please. the

Please finish my code use simple c++ language for this is my second c++ course. Also, give an explanation of the writehistogram function, please. the comments at the end of my code are the general flow I would like it to be done

Q:

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.

// Starter file for Homework 1, Question 1; due before class 1/15/20 // [YOUR NAME HERE]

#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) { out << val << ":"; for (int i = 0; i < nRolls; i++) out << "*"; 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 < N_ROLLS; 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 */ }

/*data.txt record the rolls producehistogram... data.txt input to histogram and hist.txt output 200 stars open data file as a ifstream(inputfile) and open hist.txt as a ofstream 2 to 12 go line by line get one data value and for do while that goes untill end of data.txt prints line i of hist */

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Focus On Geodatabases In ArcGIS Pro

Authors: David W. Allen

1st Edition

1589484452, 978-1589484450

More Books

Students also viewed these Databases questions