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

Use the random number generator and an array to keep track of how many times a n

ID: 3552919 • Letter: U

Question

Use the random number generator and an array to keep track of how many times a number is generated.


Exercise

Write a program that generates 100 random numbers in the range of [0,9]. Use an array to keep track of the

frequency of each digit generated. Then display a frequency histogram to the console using asterisks to

represent the number of times the number was generated.

Printing format requirement:

? Number (number of times generated): one asterisks for every time generated. See example below.

0 (20): ********************

1 (7): *******

2 (12): ************

3 (7): *******

4 (11): ***********

5 (10): **********

6 (10): **********

7 (8): ********

8 (3): ***

9 (12): ************

For the example above, the number 0 was generated 20 times out of 100. The number 2 was generated 12

times and so on for every number.

Explanation / Answer

//language=C++


#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;


int main()

{

srand (time(NULL));

int a[10]={0,0,0,0,0,0,0,0,0,0};

for(int i=0;i<100;i++){

a[rand()%10]++;

}

for(int i=0;i<10;i++){

cout<<i<<"("<<a[i]<<"): ";

for(int j=0;j<a[i];j++)cout<<"*";

cout<<endl;

}

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote