Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here\'s the code I have so far: //randint.cpp #include \"randint.h\" #include us

ID: 672579 • Letter: H

Question

Here's the code I have so far:

//randint.cpp

#include "randint.h"
#include
using namespace std::chrono;

//linear congruential pseudorandom number generator
int randint(){
//use the clock for an initial pseudorandom number
static long x = time_point_cast(
system_clock::now()).time_since_epoch().count();
//calculate the next pseudorandom number
x = (1103515245 * x + 12345) & 0x7fffffff; //parameters from glibc(?)
return x;
}

//hw4pr1.cpp

#include "std_lib_facilities_4.h"
#include "randint.h"
using std::cout;
int main()
{
   int i;
int digitCount[10] = {}; // counts for the digits 0-9
for (int i = 0; i < 100000; ++i)
++digitCount[randint() % 10];
for (int i = 0; i < 10; ++i) // increments trials for i before it reaches 10
cout << "There were " << digitCount[i] << " " << i << "'s ";
}

How would I edit this code to make my program

print a histogram of the results that looks something like this:

Digit        Frequency

0 99875          ***********************************

1 103248          ***************************************

2 90802          ********************************

and so on, using a statement like

        cout << string(frequency / 2000, '*');

to print the right number of asterisks.  

Explanation / Answer

//randint.h

#ifndef RANDINT_H
#define RANDINT_H

int randint();

#endif

//randint.cpp

#include "randint.h"
#include <chrono>
using namespace std::chrono;

//linear congruential pseudorandom number generator
int randint(){
    //use the clock for an initial pseudorandom number
    static long x = time_point_cast<microseconds>(
                        system_clock::now()).time_since_epoch().count();
    //calculate the next pseudorandom number
    x = (1103515245 * x + 12345) & 0x7fffffff; //parameters from glibc(?)
    return x;
}

//filename.cpp

#include <iostream>
#include "randint.h"
using std::cout;

int main()
{
    int digitCount[10] = {};
    for (int i = 0; i < 1000; ++i)
        ++digitCount[randint() % 10];
    for (int i = 0; i < 10; ++i)
        cout << "There were " << digitCount[i] << " " << i << "'s ";
}