I only need to know how to do project 2. Project #1 Write a program to simulate
ID: 3680807 • Letter: I
Question
I only need to know how to do project 2.
Project #1
Write a program to simulate a uniform (meaning every number is equally likely to appear as the next) integer random number. Your program will be a console application which will simulate a 6-sided die. You will “throw” your simulated die N times. The number N should be obtained by prompting the user with the question “How many rolls?”
Also, since you will be using rand() provide a prompt requesting that the user provide a seed number. The prompt can be as simple as "Please enter a seed number:"
Then use the seed provided by the user to seed the random number generator. This will also help you test your program.
We will use 6000 times as an example.
Show the frequency distribution of your numbers when 6,000 tosses are made and the random number is instantiated to generate numbers between 1 and 6 (inclusive). In other words, show how many times the numbers 1,2,3,….and 6 have appeared.
The output displayed on the screen should look something like this:
1 ----- 900
2 ----- 950
Up to
6 ---- 1011
Meaning that the number one appeared 900 times (your mileage may vary), and so on.
The output should also have a section displaying a histogram.
The histogram should look something like this:
1 xxxxxxxxxxxxxx
2 xxxxxxxxxxxxxx
.
.
.
6 xxxxxxxxxxxxxx
Where the ‘x’ represent some number of occurrence of a particular face. Since you are likely to get 100’s of occurrences of a face you will not be able to use one x for one occurrence. Therefore you will have to scale your display so that ‘x’ represents some number of occurrences and so that the largest number of occurrences will fit on a standard console line of 80 characters.
You might want to find the number with the maximum occurrence and represent that maximum occurrence with 60 “x”’s. This means that should the die side appearing the most comes out 1200 times out of 6000, you would want to represent its count with 60 ‘x’’s. This means that each x, in that particular run would represent 20 occurrences of a side. Thus 1200 occurrences are represented by 20 X’s If, on the other hand, the largest number of occurrences of a particular side had been 1800, then you would need each ‘X’ to represent 30 occurrences so that 1800 occurrences would be represented by 60 X’s.
Basically, you want to normalize your graph in such a way that the largest number of occurrences is represented by 60 X’s.
Make sure that your x’s line up and start on the same column.
Then display the histogram obtained when you roll 2 dice and add up the numbers that come up (2 – 12). As in the first part, you will roll the dice N times.
Create 2 outputs as above. 1 output to display the counts for each sum and one output to display the x’s for each sum
In project 2
you will recreate the same application that you created in project #1. Whereas project #1 had you solve the problem using a functional approach, you now have to use classes to solve the problem and use an object-oriented approach.
You will have to have the following classes:
aDie, to represent a die.
aHistogram to represent a histogram.
The classes must be designed so that they have the following methods with the following signatures:
aDie::aDie() as the default constructor
int aDie::roll() to simulate rolling a die. This method should return a number between 1 and 6 inclusive.
For the aHistogram class, you must have:
aHIstogram::aHistogram() as the default constructor
aHistogram::aHistogram(int lowestValue, int maximumValue) a constructor to instantiate a histogram that will keep track of how many time the numbers from lowestValue to maximumValue appear.
Use the "Rule of three" as explained in your Zybook in 11.13
void aHistogram::reset() to clear all the entries in the histogram
int aHistogram::update(int number) to increment the count representing the number of times the argument (number) has appeared. This method returns the number of times the argument has occured.
You will also have to have a stand-alone function to output the contents of the histogram as described in project #1 so that 60 X's are displayed for the count of the number with the largest number of occurrences. This stand-alone function should have the signature:
ostream& operator<<(ostream& os, const aHistogram& hist)
This function will allow you to perform the following:
cout << hist << endl; // Where hist is a histogram
As you may already have surmised, you might need some variable in your aHistogram class to establish how many X's you want for the largest count. In our case we use 60, but you will want to allow for a variable number. Perhaps, for some application, that number should be 50 or 70. As a hint, a constructor would be a good place to establish that parameter.
We will use a member variable to represent the number of X's we want for the largest count to be:
int longestLine; // In our case we will use 60, but having this variable means that we could change that value for a larger or smaller console.
use an accessor to access this member variable. Call it: int getLongestLine() const; // Yes, it is a const method as it doesn't modify the object ot calls.
Explanation / Answer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i,a[6],n,b,c,d,m,j;
cout<<"How many rolls?";
cin>>n;
for(i=0;i<6;i++)
a[i]=0;
for(i=0;i<n;i++)
{
b=rand()%6+1;
if(b==1)
a[0]=a[0]+1;
if(b==2)
a[1]=a[1]+1;
if(b==3)
a[2]=a[2]+1;
if(b==4)
a[3]=a[3]+1;
if(b==5)
a[4]=a[4]+1;
if(b==6)
a[5]=a[5]+1;
}
m=a[0];
for(i=0;i<6;i++)
{
cout<<i+1<<"____"<<a[i]<<endl;
if(a[i]>m)
m=a[i];
}
c=m/70;
for(i=0;i<6;i++)
{
d=a[i]/c;
cout<<i+1<<"=";
for(j=0;j<d;j++)
cout<<"x";
cout<<endl;
}
getch();
}