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

Problem #1 (Random Sequence) Create a class that returns a random number from th

ID: 3901931 • Letter: P

Question

Problem #1 (Random Sequence)

Create a class that

returns a random number from the following set,

{16,33,56,77,126}. Loop 100,000 times with this

procedure and print the frequency of each of the

5 numbers obtained. The following is the

specification for the class.

Specification

class Prob1Random

{

private:

char *set; //The set of numbers to draw random numbers from

char nset; //The number of variables in the sequence

int *freq; //Frequency of all the random numbers returned

int numRand; //The total number of times the random number function is called

public:

Prob1Random(const char,const char *); //Constructor

~Prob1Random(void); //Destructor

char randFromSet(void); //Returns a random number from the set

int *getFreq(void) const; //Returns the frequency histogram

char *getSet(void) const; //Returns the set used

int getNumRand(void) const; //Gets the number of times randFromSet has

//been called

};

Driver program to return a random sequence

char n=5;

char rndseq[]={16,33,56,77,126};

int ntimes=100000;

Prob1Random a(n,rndseq);

for(int i=1;i<=ntimes;i++)

{

a.randFromSet();

}

int *x=a.getFreq();

char *y=a.getSet();

for(int i=0;i<n;i++)

{

cout<<int(y[i])<<" occured "<<x[i]<<" times"<<endl;

}

cout<<"The total number of random numbers is "<<a.getNumRand()<<endl;

Sample Output

16 occured 20045 times

33 occured 19952 times

56 occured 20035 times

77 occured 20039 times

126 occured 19929 times

The total number of random numbers is 100000

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks.

//Code

#include<iostream>

#include<stdlib.h>

#include<time.h>

using namespace std;

class Prob1Random

{

private:

char *set; //The set of numbers to draw random numbers from

char nset; //The number of variables in the sequence

int *freq; //Frequency of all the random numbers returned

int numRand; //The total number of times the random number function is called

public:

Prob1Random(const char,const char *); //Constructor

~Prob1Random(void); //Destructor

char randFromSet(void); //Returns a random number from the set

int *getFreq(void) const; //Returns the frequency histogram

char *getSet(void) const; //Returns the set used

int getNumRand(void) const; //Gets the number of times randFromSet has

//been called

};

//constructor

Prob1Random::Prob1Random(const char n, const char* _set){

                srand(time(NULL));//seeding random number generator

                nset=n;

                //initializing the dynamic array set

                set=new char[n];

                //copying all elements in _set to set

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

                                set[i]=_set[i];

                }

                //initializing the dynamic array freq

                freq=new int[n];

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

                                freq[i]=0;//setting all frequency count to 0

                }

                numRand=0;

}

//returns a random element from the set

char Prob1Random::randFromSet(){

                //generating a random index between 0 and nset-1

                int randomIndex=rand()%nset;

                freq[randomIndex]++;//incrementing the frequency

                numRand++;//incrementing the counter

                return set[randomIndex];//returning the random element

}

//returns the frequency array

int *Prob1Random::getFreq() const{

                return freq;

}

//returns the set array

char* Prob1Random::getSet() const{

                return set;

}

//returns the number of simulations run

int Prob1Random::getNumRand() const{

                return numRand;

}

//destructor

Prob1Random::~Prob1Random(){

                //deallocating the memory

                delete set;

                delete freq;

}

//main method to test the above code

int main(){

                char n=5;

                char rndseq[]={16,33,56,77,126};             

                int ntimes=100000;

                Prob1Random a(n,rndseq);

                for(int i=1;i<=ntimes;i++)

                {

                                a.randFromSet();

                }

                int *x=a.getFreq();         

                char *y=a.getSet();

                for(int i=0;i<n;i++)

                {

                                cout<<int(y[i])<<" occured "<<x[i]<<" times"<<endl;

                }

                cout<<"The total number of random numbers is "<<a.getNumRand()<<endl;

                return 0;

}

/*OUTPUT*/

16 occured 20072 times

33 occured 19884 times

56 occured 20055 times

77 occured 20069 times

126 occured 19920 times

The total number of random numbers is 100000

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