I'm working on a project and am completely at a loss on how to even really get started. Here is the project instructions for the initial project which I had no trouble with, and the second project which builds upon the first.
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:
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.
I've read through the instructions over and over and still can't really understand how the aHistogram class will be set up and what the methods are doing. The aDie class is simple enough and I've got the method working that simulates a roll. If anyone could outline what I need to do here step by step I would greatly appreciate the help.
Your aHistogram class will need an array to keep track of how many times each die face appears.
Suggestion:
int count[7] = {0}; //0-6, entry 0 not used
Per the instructions, each time you roll the die, you need to call the update() function with the face value of the roll. update(), updates the appropriate member of count.
As a suggestion to get started, write your headers for each class. Your instructions are very clear about what the signatures should look like for your class member functions. Post your headers and any further questions if you need more help.
I went back and scrapped everything and am trying to start over based off of your suggestion.
So far I have
aDie.h:
# include <iostream>
# include <cstdlib>
# include <ctime>
# include <vector>
#define ADIE_H
class aDie {
public:
int roll();
aDie(); //default consturctor
int dieRoll;
private:
};
aDie.cpp:
#include "aDie.h"
using namespace std;
aDie::aDie() {
int dieRoll = 0;
return;
}
int aDie::roll() {
int seednum = 4;
srand(seednum);
dieRoll = ((rand() % 6) + 1);
return dieRoll;
}
aHistogram.h
# include <iostream>
# include <cstdlib>
# include <ctime>
# include <vector>
#define AHISTOGRAM_H
class aHistogram {
public:
aHistogram();
aHistogram(int lowestValue, int MaximumValue);
void reset();
int update(int number);
void printArray(int arr[], int size);
private:
int longestLine;
int lowVal;
int maxVal;
int count[];
};
aHistogram.cpp:
#include "aHistogram.h"
using namespace std;
aHistogram::aHistogram() {
int lowVal = 0;
int maxVal = 0;
int longestLine = 0;
int count[7];
}
aHistogram::aHistogram(int lowestValue, int maximumValue) {
lowVal = lowestValue;
maxVal = maximumValue;
int longestLine = 60;
}
void aHistogram::reset() {
for (int i = 0; i < 6; ++i) { count[i] = 0; }
}
int aHistogram::update(int number) {
count[number]++;
return 0;
}
void aHistogram::printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << ' ';
}
cout << endl;
}
Main.cpp:
# include <iostream>
# include <cstdlib>
# include <ctime>
# include <vector>
#include <algorithm>]
#include "aDie.h"
#include "aHistogram.h"
using namespace std;
int main() {
aDie roll;
aHistogram hist;
for (int i = 0; i < 6000; ++i) {
hist.update(roll.roll());
}
system("pause");
}
No real clue where to go from this point honestly, this is much more complex than anything I've done thus far and is really the first time I've used OOP. I have noreal clue as to what these functions are doing,
aHistogram::aHistogram(int lowestValue, int maximumValue) {
lowVal = lowestValue;
maxVal = maximumValue;
int longestLine = 60;
}
void aHistogram::reset() {
for (int i = 0; i < 6; ++i) { count[i] = 0; }
}
I know that I need to create a function that will take the amount of instances of each face and then print the number of occurrences for each one, and I also need to create the function that prints the histogram. Any suggestions on how to proceed would be appreciated.