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

Don\'t forget to use templates in place of the character arrays! Problem #1 (Ran

ID: 3626702 • Letter: D

Question

Don't forget to use templates in place of the character arrays!

Problem #1 (Random Sequence)
Create a class that
returns a random number from the following set,
{16,34,57,79,121}. Loop 100000 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,34,57,79,121};
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
34 occured 19952 times
57 occured 20035 times
79 occured 20039 times
121 occured 19929 times
The total number of random numbers is 100000

Note: Your results are not expected to be exactly the
same! After all these are pseudo-random number sequences
with different seeds.

extra props if you can also convert this to java

Explanation / Answer

hope this will help u out public class Prob1Random { private byte set[]; //The set of numbers to draw random numbers from private byte nset; //The number of variables in the sequence private int freq[]; //Frequency of all the random numbers returned private int numRand; //The number of random numbers called /** Creates a new instance of Prob1Random */ public Prob1Random(byte n, byte s[]) { set = new byte [nset]; set = s; nset = n; } public byte randFromSet() //Returns a random number from the set { int tempRand = (int)(Math.random()*5); freq = new int [nset]; freq[tempRand]++; getNumRand(); return set[(byte)(tempRand)]; } public int [] getFreq() //Returns the Frequency histogram { return freq; } public byte [] getSet() //Returns the Set array { return set; } public int getNumRand() //Returns the number of times randFromSet() was called { return numRand++; } } //========================================================= // the driver public class Prob1 { public static void main(String[] args) { System.out.println("Entering Problem #1"); byte n=5; byte rndseq[]={16,34,57,79,121}; int ntimes=100000; Prob1Random a=new Prob1Random(n,rndseq); for(int i=1;i
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