Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <ctime>
- #include <cstdlib>
- #include <vector>
- #include <fstream>
- #include <algorithm>
- #include <math.h>
- using namespace std;
- vector<int> modeVect (vector<int>& vect);
- double std_div(vector<int>& vec, double mean);
- //dice class, default rolls 6 side die. User specifies other types of dice
- class Dice
- {
- protected:
- int sides;
- int roll;
- public:
- Dice()
- {
- sides = 6;
- }
- Dice(int s)
- {
- sides = s;
- }
- void setsides(int s)
- {
- sides = s;
- }
- int getsides()
- {
- return sides;
- }
- void roll_dice()
- {
- roll = rand() % sides + 1;
- }
- int get_roll()
- {
- return roll;
- }
- };
- //derived class from Dice.
- class LoadedDice : public Dice
- {
- public:
- //constructor
- LoadedDice()
- {
- }
- LoadedDice(int s) : Dice(s)
- {
- }
- void roll_dice()
- {
- (roll = rand() % sides + 1);
- //Loaded die doubles the roll if it is initially lower than half the
- //number of sides. This keeps the rolls in the realm of possibility.
- if(roll <= (sides/2))
- {
- roll = roll * 2;
- }
- }
- int get_roll()
- {
- return roll;
- }
- };
- int main()
- {
- srand(time(0));
- const int ROLLS = 100;
- Dice die1;
- Dice die2(12);
- LoadedDice die3;
- LoadedDice die4(12);
- double sum_die1=0.0;
- double sum_die2=0.0;
- double sum_die3=0.0;
- double sum_die4=0.0;
- vector<int> die1_vect(ROLLS);
- vector<int> die2_vect(ROLLS);
- vector<int> die3_vect(ROLLS);
- vector<int> die4_vect(ROLLS);
- ofstream output("lab3.txt", ios::app);
- //cout << "This is for Dice default 6 sides: " << endl;
- for(int x=0; x < ROLLS; x++)
- {
- die1.roll_dice();
- sum_die1 = sum_die1 + die1.get_roll();
- die1_vect[x] = die1.get_roll();
- }
- //cout << "This is for Dice with " << die2.getsides() << " sides: " << endl;
- for(int x=0; x < ROLLS; x++)
- {
- die2.roll_dice();
- sum_die2 = sum_die2 + die2.get_roll();
- die2_vect[x] = die2.get_roll();
- }
- //cout << "This is for LoadedDice with default 6 sides: " << endl;
- for(int x=0; x < ROLLS; x++)
- {
- die3.roll_dice();
- sum_die3 = sum_die3 + die3.get_roll();
- die3_vect[x] = die3.get_roll();
- }
- //cout << "This is for LoadedDice with " << die4.getsides() << " sides: " << endl;
- for(int x=0; x < ROLLS; x++)
- {
- die4.roll_dice();
- sum_die4 = sum_die4 + die4.get_roll();
- die4_vect[x] = die4.get_roll();
- }
- //sorts the stored vectors of each 100 rolls from the respective die
- sort(die1_vect.begin(), die1_vect.end());
- sort(die2_vect.begin(), die2_vect.end());
- sort(die3_vect.begin(), die3_vect.end());
- sort(die4_vect.begin(), die4_vect.end());
- cout << "\n\n" << endl;
- cout << "1. The sum of 6 sided die of 100 rolls is " << sum_die1 << endl;
- cout << "2. The sum of the regular die with " << die2.getsides() << " sides and 100 rolls is " << sum_die2 << endl;
- cout << "2. The sum of the loaded die with " << die3.getsides() << " sides and 100 rolls is " << sum_die3 << endl;
- cout << "2. The sum of the loaded die with " << die4.getsides() << " sides and 100 rolls is " << sum_die4 << endl;
- double die1_mean = (sum_die1/ROLLS);
- double die2_mean = (sum_die2/ROLLS);
- double die3_mean = (sum_die3/ROLLS);
- double die4_mean = (sum_die4/ROLLS);
- cout << "\nThe means of the dice are as follows: " << endl;
- cout << "1. " << die1_mean << endl;
- cout << "2. " << die2_mean << endl;
- cout << "3. " << die3_mean << endl;
- cout << "4. " << die4_mean << endl;
- int die1_med = die1_vect.at(49);
- int die2_med = die2_vect.at(49);
- int die3_med = die3_vect.at(49);
- int die4_med = die4_vect.at(49);
- cout << "\nThe median of the dice are as follow: " << endl;
- cout << "1. " << die1_med << endl;
- cout << "2. " << die2_med << endl;
- cout << "3. " << die3_med << endl;
- cout << "4. " << die4_med << endl;
- vector<int> mode1(modeVect(die1_vect));
- vector<int> mode2(modeVect(die2_vect));
- vector<int> mode3(modeVect(die3_vect));
- vector<int> mode4(modeVect(die4_vect));
- cout << "\nThe mode of the dice are as follows: " << endl;
- cout << "1. ";
- for(int i =0; i < mode1.size(); i++)
- {
- cout << mode1[i] << " ";
- }
- int die1_mode = mode1.at(0);
- cout << endl;
- cout << "2. ";
- for(int i =0; i < mode2.size(); i++)
- {
- cout << mode2[i] << " ";
- }
- int die2_mode = mode2.at(0);
- cout << endl;
- cout << "3. ";
- for(int i =0; i < mode3.size(); i++)
- {
- cout << mode3[i] << " ";
- }
- int die3_mode = mode3.at(0);
- cout << endl;
- cout << "4. ";
- for(int i =0; i < mode4.size(); i++)
- {
- cout << mode4[i] << " ";
- }
- int die4_mode = mode4.at(0);
- double die1_stddiv;
- double die2_stddiv;
- double die3_stddiv;
- double die4_stddiv;
- die1_stddiv = std_div(die1_vect, die1_mean);
- die2_stddiv = std_div(die2_vect, die2_mean);
- die3_stddiv = std_div(die3_vect, die3_mean);
- die4_stddiv = std_div(die4_vect, die4_mean);
- cout << "\n\nThe standard deviation of each die are as follows: " << endl;
- cout << "1. " << die1_stddiv << endl;
- cout << "2. " << die2_stddiv << endl;
- cout << "3. " << die3_stddiv << endl;
- cout << "4. " << die4_stddiv << endl;
- cout << "\n\n" <<endl;
- //data text ouput
- output.open("lab3.txt", ios::app);
- if (output.fail()) {
- cout << "Error creating output file." << endl;
- }
- else {
- output << "**** Regular Dice 6 Sides Data ****\n";
- output << "Mean: " << die1_mean << "\n";
- output << "Median: " << die1_med << "\n";
- output << "Mode: " << die1_mode << "\n";
- output << "Standard Deviation: " << die1_stddiv << "\n";
- output << "\n";
- output << "**** Regular Dice 12 Sides Data ****\n";
- output << "Mean: " << die2_mean << "\n";
- output << "Median: " << die2_med << "\n";
- output << "Mode: " << die2_mode << "\n";
- output << "Standard Deviation: " << die2_stddiv << "\n";
- output << "\n";
- output << "**** Loaded Dice 6 Sides Data ****\n";
- output << "Mean: " << die3_mean << "\n";
- output << "Median: " << die3_med << "\n";
- output << "Mode: " << die3_mode << "\n";
- output << "Standard Deviation: " << die3_stddiv << "\n";
- output << "\n";
- output << "**** Loaded Dice 12 Sides Data ****\n";
- output << "Mean: " << die4_mean << "\n";
- output << "Median: " << die4_med << "\n";
- output << "Mode: " << die4_mode << "\n";
- output << "Standard Deviation: " << die4_stddiv << "\n";
- output << "\n";
- }
- output.close();
- return 0;
- }
- /*********************************************************
- * Entry: std::vector *
- * *
- * Exit: vector mode obtained *
- * *
- * Purpose: To determine the mode of the sequence *
- *********************************************************/
- vector<int> modeVect (vector<int>& vect)
- {
- int current; //current number being tallied
- int count = 0; //iteration
- int old_freq = 0; //compare to older mode
- vector<int> mode;
- do{
- int frequency = 0; //frequency a number appears
- current = vect[count];
- for(int i = 0; i < vect.size(); i++)
- {
- if(vect[i] > current)
- break;
- if(vect[i] == current)
- {
- frequency++;
- }
- }
- //if the current mode has the same frequency of the current number being tested
- if(frequency == old_freq)
- {
- for(int j = 0; j < mode.size(); j++)
- {
- if(mode[j] == current)
- goto same;
- }
- mode.push_back(current);
- }
- //if the current number has a greater frequency than the previous largest
- if(frequency > old_freq)
- {
- mode.clear();
- mode.push_back(current);
- old_freq = frequency;
- }
- same:
- count++;
- //as long as the count is less than the size of the parameter vector
- }while (count < vect.size());
- return mode;
- }
- double std_div(vector<int>& vec, double mean)
- {
- vector<double> holder(100);
- double holder_sum;
- double holder_div;
- double standard_div = 0;
- //squares the value minus the mean
- for(int x = 0; x < vec.size(); x++)
- {
- holder[x] = pow((vec[x] - mean), 2);
- }
- //sum of new vector
- for(int x=0; x < holder.size(); x++)
- {
- holder_sum += holder[x];
- }
- holder_div = (holder_sum/holder.size());
- standard_div = sqrt(holder_div);
- return standard_div;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement