#ifndef DIE_H // allows for additional
#define DIE_H
class Die
{
public: // available outside of class
Die(); // SMF to set value
void Roll(); // member functions
int GetFaces();
private: // not available outside class
int Face;
};
#endif
#include "Die.h" // for processing die face
#include <cstdlib> // for the library rand() function
Die::Die() //Initializes Face data member
{
Face = 1;
}
void Die::Roll()
{
Face = rand() % 6 + 1;
}
int Die::GetFaces()
{
return Face;
}
#ifndef DIEROLL_H // allows for additional
#define DIEROLL_H
#include "Die.h"
class DiceRoll // classs that specifies a collection of 3 contained Die objects
{
public:
void RollDice(); // Calls Roll() function on each contained die
int GetRollFaces(); // Returns the sum of the current Face value of Die1, Die2, & Die3
private:
Die Die1; // The three die contained in this -i.e
Die Die2; // objects of this class automatically contain
Die Die3; // three dice
};
#endif
#include <iostream> // for cin,cout
#include <cstdlib> // for the library rand() function
#include "DieRoll.h" // for processing die roll
#include "Die.h"
void DiceRoll::RollDice()
{
Die1.Roll();
Die2.Roll();
Die3.Roll();
}
int DiceRoll::GetRollFaces()
{
int result;
result = Die1.GetFaces()+Die2.GetFaces()+Die3.GetFaces();
return result;
}
#ifndef SHELL_H // Avoid duplicate compilations #define SHELL_H // void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[]); // 1st global function prototype void DisplayResults(int ResultsArray[ ], int ResultsArraySize ); //2nd global function prototype void createHistogram(int frequency[], int range); // function to display histogram #endif
#include <iostream> // for cin,cout
#include <iomanip>
#include <cstdlib> // for the library rand() function
#include <cmath>
#include <ctime>
#include "FreeFunction.h" // for processing the roll
#include "Die.h" // for processing die face
#include "DieRoll.h" // for processing die roll
using namespace std;
void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[])
{
for ( int i = 0; i <200; i++)
{
switch ( RollsArray[ i ] )
{
case 3: ResultsArray[ 0 ] += 1; break;
case 4: ResultsArray[ 1 ] += 1; break;
case 5: ResultsArray[ 2 ] += 1; break;
case 6: ResultsArray[ 3 ] += 1; break;
case 7: ResultsArray[ 4 ] += 1; break;
case 8: ResultsArray[ 5 ] += 1; break;
case 9: ResultsArray[ 6 ] += 1; break;
case 10: ResultsArray[ 7 ] += 1; break;
case 11: ResultsArray[ 8 ] += 1; break;
case 12: ResultsArray[ 9 ] += 1; break;
case 13: ResultsArray[ 10 ] += 1; break;
case 14: ResultsArray[ 11 ] += 1; break;
case 15: ResultsArray[ 12 ] += 1; break;
case 16: ResultsArray[ 13 ] += 1; break;
case 17: ResultsArray[ 14 ] += 1; break;
case 18: ResultsArray[ 15 ] += 1;
}
}
}
void DisplayResults(int ResultsArray[ ], int ResultsArraySize )
{
for ( int i = 0; i < 200; i++)
{
cout<< "\n\nOut of 200 Rolls of 3 Dice: \n\n"<< endl;
cout << "The number " << i + 1 << " was rolled " << ResultsArray[i] << " times." << endl;
}
}
//Creates Histogram of scores
void createHistogram(int frequency[], int range)
{
for(int i = 0;i<=range;i++)
{
cout<<setw(3)<<i<<setw(3)<<frequency[i];
for(int j = 1;j<=frequency[i];j++)
{
cout<<"*";
cout<<endl;
}
}
}
#include <iostream> // for cin,cout
#include <iomanip>
#include <cstdlib> // for the library rand() function
#include "FreeFunction.h" // for processing the roll
#include "DieRoll.h" // for processing die roll
using namespace std;
int main ()
{
const int NUM_RANGE = 15;
int frequency[NUM_RANGE+1];
int RollsArray[100];
DiceRoll TheIvories;
for (int i = 0; i < 100; i++)
{
TheIvories.RollDice(); // Roll the dice
RollsArray[i] = TheIvories.GetRollFaces(); // Record the result of the roll end loop
}
for (int j = 0; j< 100; j++)
{
cout << RollsArray[j]<< endl;
}
return 0;
}

New Topic/Question
Reply



MultiQuote





|