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

Can you please help me with this problem by using C++ A . Here is a randint func

ID: 3837960 • Letter: C

Question

Can you please help me with this problem by using C++

A. Here is a randint function to use:

//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

         //parameters from glibc(?)

         x = long(long(1103515245 * x) + 12345)) & 0x7fffffff;   

         return x;

}

You do not have to understand this function for now (but feel free to figure

out or look up as much as you wish!); just call it to get a "random" number.

Put this code in a file named randint.cpp, and also write the matching header

file randint.h. Write a main named hw4pr1.cpp which calls randint 1000000 times and counts how many times the last digit (in decimal) is a 0, a 1, etc. The

output might look like this:

There were 99875 0's.

There were 103248 1's.

There were 90802 2's.

and so on. If randint is really pretty random, all the counts will be close to

100000, or the counts might be all over the place if it is not very random.

B.  Modify Part A to 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. Note: Run your program several times to see if you should adjust the divisor of 2000.

Explanation / Answer

//part program

#include"randint.h"
#include<iostream>
#include<string>
using namespace std;

int main()
{
   long number, frequency[10] = { 0 };
   long num;
  
  for (long i = 0; i < 1000000; i++)
   {
       number = randint();
       num = number % 10;
       switch (num)
       {
       case 0:
           ++frequency[0];
           break;
       case 1:
           ++frequency[1];
           break;
       case 2:
           ++frequency[2];
           break;
       case 3:
           ++frequency[3];
           break;
       case 4:
           ++frequency[4];
           break;
       case 5:
           ++frequency[5];
           break;
       case 6:
           ++frequency[6];
           break;
       case 7:
           ++frequency[7];
           break;
       case 8:
           ++frequency[8];
           break;
       case 9:
           ++frequency[9];
           break;

       }

   }
   for (int i = 0; i < 10; i++)
       cout << "There were " << frequency[i] << " " << i << "'s" << endl;

  
}

---------------------------------------------------------------------

//part1 output

There were 100112 0's
There were 99580 1's
There were 99923 2's
There were 100267 3's
There were 100282 4's
There were 100235 5's
There were 99740 6's
There were 100053 7's
There were 99943 8's
There were 99865 9's

--------------------------------------------------------------------------------

//part2 program

#include"randint.h"
#include<iostream>
#include<string>
using namespace std;

int main()
{
   long number, frequency[10] = { 0 };
   long num;
      
   cout << "Digit Frequency" << endl;
   for (long i = 0; i < 1000000; i++)
   {
       number = randint();
       num = number % 10;
       switch (num)
       {
       case 0:
           ++frequency[0];
           break;
       case 1:
           ++frequency[1];
           break;
       case 2:
           ++frequency[2];
           break;
       case 3:
           ++frequency[3];
           break;
       case 4:
           ++frequency[4];
           break;
       case 5:
           ++frequency[5];
           break;
       case 6:
           ++frequency[6];
           break;
       case 7:
           ++frequency[7];
           break;
       case 8:
           ++frequency[8];
           break;
       case 9:
           ++frequency[9];
           break;

       }

   }
   for (int i = 0; i < 10; i++)
   cout << frequency[i] << " " << string(frequency[i] / 2000, '*')<<endl;
      
  
}

-----------------------------------------------

//output2

Digit Frequency
100173 **************************************************
100648 **************************************************
100308 **************************************************
99608 *************************************************
99522 *************************************************
99837 *************************************************
99881 *************************************************
100061 **************************************************
100116 **************************************************
99846 *************************************************