Advertisement
Lawnknome

lab3.cpp

Apr 19th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <ctime>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <fstream>
  7. #include <algorithm>
  8. #include <math.h>
  9. using namespace std;
  10.  
  11. vector<int> modeVect (vector<int>& vect);
  12. double std_div(vector<int>& vec, double mean);
  13.  
  14. //dice class, default rolls 6 side die.  User specifies other types of dice
  15. class Dice
  16. {
  17.     protected:
  18.         int sides;
  19.         int roll;
  20.  
  21.     public:
  22.         Dice()
  23.         {
  24.             sides = 6;
  25.         }
  26.  
  27.         Dice(int s)
  28.         {
  29.             sides = s;
  30.         }
  31.        
  32.         void setsides(int s)
  33.         {
  34.             sides = s;
  35.         }
  36.  
  37.         int getsides()
  38.         {
  39.             return sides;
  40.         }
  41.  
  42.         void roll_dice()
  43.         {
  44.             roll = rand() % sides + 1;
  45.         }
  46.  
  47.         int get_roll()
  48.         {
  49.             return roll;
  50.         }
  51. };
  52.  
  53.  
  54. //derived class from Dice.  
  55. class LoadedDice : public Dice
  56. {
  57.     public:
  58.         //constructor
  59.         LoadedDice()
  60.         {
  61.  
  62.         }
  63.  
  64.         LoadedDice(int s) : Dice(s)
  65.         {
  66.  
  67.         }
  68.  
  69.         void roll_dice()
  70.         {
  71.             (roll = rand() % sides + 1);
  72.  
  73.             //Loaded die doubles the roll if it is initially lower than half the
  74.             //number of sides.  This keeps the rolls in the realm of possibility.
  75.             if(roll <= (sides/2))
  76.             {
  77.                 roll = roll * 2;
  78.             }
  79.         }
  80.  
  81.         int get_roll()
  82.         {
  83.             return roll;
  84.         }
  85. };
  86.  
  87.  
  88. int main()
  89. {
  90.     srand(time(0));
  91.  
  92.     const int ROLLS = 100;
  93.     Dice die1;
  94.     Dice die2(12);
  95.     LoadedDice die3;
  96.     LoadedDice die4(12);
  97.     double sum_die1=0.0;
  98.     double sum_die2=0.0;
  99.     double sum_die3=0.0;
  100.     double sum_die4=0.0;
  101.     vector<int> die1_vect(ROLLS);
  102.     vector<int> die2_vect(ROLLS);
  103.     vector<int> die3_vect(ROLLS);
  104.     vector<int> die4_vect(ROLLS);
  105.  
  106.     ofstream output("lab3.txt", ios::app);
  107.  
  108.     //cout << "This is for Dice default 6 sides: " << endl;
  109.     for(int x=0; x < ROLLS; x++)
  110.     {
  111.         die1.roll_dice();  
  112.         sum_die1 = sum_die1 + die1.get_roll();
  113.         die1_vect[x] = die1.get_roll();    
  114.     }
  115.  
  116.     //cout << "This is for Dice with " << die2.getsides() << " sides: " << endl;
  117.  
  118.     for(int x=0; x < ROLLS; x++)
  119.     {
  120.         die2.roll_dice();
  121.         sum_die2 = sum_die2 + die2.get_roll();
  122.         die2_vect[x] = die2.get_roll();    
  123.     }
  124.  
  125.     //cout << "This is for LoadedDice with default 6 sides: " << endl;
  126.     for(int x=0; x < ROLLS; x++)
  127.     {
  128.         die3.roll_dice();  
  129.         sum_die3 = sum_die3 + die3.get_roll();
  130.         die3_vect[x] = die3.get_roll();
  131.     }
  132.  
  133.     //cout << "This is for LoadedDice with " << die4.getsides() << " sides: " << endl;
  134.     for(int x=0; x < ROLLS; x++)
  135.     {
  136.         die4.roll_dice();  
  137.         sum_die4 = sum_die4 + die4.get_roll();
  138.         die4_vect[x] = die4.get_roll();
  139.     }
  140.  
  141.     //sorts the stored vectors of each 100 rolls from the respective die
  142.     sort(die1_vect.begin(), die1_vect.end());
  143.     sort(die2_vect.begin(), die2_vect.end());
  144.     sort(die3_vect.begin(), die3_vect.end());
  145.     sort(die4_vect.begin(), die4_vect.end());
  146.  
  147.     cout << "\n\n" << endl;
  148.     cout << "1. The sum of 6 sided die of 100 rolls is " << sum_die1 << endl;
  149.     cout << "2. The sum of the regular die with " << die2.getsides() << " sides and 100 rolls is " << sum_die2 << endl;
  150.     cout << "2. The sum of the loaded die with " << die3.getsides() << " sides and 100 rolls is " << sum_die3 << endl;
  151.     cout << "2. The sum of the loaded die with " << die4.getsides() << " sides and 100 rolls is " << sum_die4 << endl;
  152.  
  153.     double die1_mean = (sum_die1/ROLLS);
  154.     double die2_mean = (sum_die2/ROLLS);
  155.     double die3_mean = (sum_die3/ROLLS);
  156.     double die4_mean = (sum_die4/ROLLS);
  157.  
  158.     cout << "\nThe means of the dice are as follows: " << endl;
  159.     cout << "1. " << die1_mean << endl;
  160.     cout << "2. " << die2_mean << endl;
  161.     cout << "3. " << die3_mean << endl;
  162.     cout << "4. " << die4_mean << endl;
  163.  
  164.     int die1_med = die1_vect.at(49);
  165.     int die2_med = die2_vect.at(49);
  166.     int die3_med = die3_vect.at(49);
  167.     int die4_med = die4_vect.at(49);
  168.  
  169.     cout << "\nThe median of the dice are as follow: " << endl;
  170.     cout << "1. " << die1_med << endl;
  171.     cout << "2. " << die2_med << endl;
  172.     cout << "3. " << die3_med << endl;
  173.     cout << "4. " << die4_med << endl;
  174.  
  175.     vector<int> mode1(modeVect(die1_vect));
  176.     vector<int> mode2(modeVect(die2_vect));
  177.     vector<int> mode3(modeVect(die3_vect));
  178.     vector<int> mode4(modeVect(die4_vect));
  179.  
  180.     cout << "\nThe mode of the dice are as follows: " << endl;
  181.     cout << "1. ";
  182.  
  183.         for(int i =0; i < mode1.size(); i++)
  184.         {
  185.             cout << mode1[i] << " ";
  186.         }
  187.     int die1_mode = mode1.at(0);
  188.     cout << endl;
  189.     cout << "2. ";
  190.  
  191.         for(int i =0; i < mode2.size(); i++)
  192.         {
  193.             cout << mode2[i] << " ";
  194.         }
  195.     int die2_mode = mode2.at(0);
  196.     cout << endl;
  197.     cout << "3. ";
  198.  
  199.         for(int i =0; i < mode3.size(); i++)
  200.         {
  201.             cout << mode3[i] << " ";
  202.         }
  203.     int die3_mode = mode3.at(0);
  204.  
  205.     cout << endl;  
  206.     cout << "4. ";
  207.  
  208.         for(int i =0; i < mode4.size(); i++)
  209.         {
  210.             cout << mode4[i] << " ";
  211.         }
  212.     int die4_mode = mode4.at(0);
  213.  
  214.     double die1_stddiv;
  215.     double die2_stddiv;
  216.     double die3_stddiv;
  217.     double die4_stddiv;
  218.  
  219.     die1_stddiv = std_div(die1_vect, die1_mean);
  220.     die2_stddiv = std_div(die2_vect, die2_mean);
  221.     die3_stddiv = std_div(die3_vect, die3_mean);
  222.     die4_stddiv = std_div(die4_vect, die4_mean);
  223.  
  224.     cout << "\n\nThe standard deviation of each die are as follows: " << endl;
  225.     cout << "1. " << die1_stddiv << endl;
  226.     cout << "2. " << die2_stddiv << endl;
  227.     cout << "3. " << die3_stddiv << endl;
  228.     cout << "4. " << die4_stddiv << endl;
  229.  
  230.     cout << "\n\n" <<endl;
  231.  
  232.     //data text ouput
  233.     output.open("lab3.txt", ios::app);
  234.  
  235.     if (output.fail()) {
  236.         cout << "Error creating output file." << endl;
  237.     }
  238.     else {
  239.         output << "**** Regular Dice 6 Sides Data ****\n";
  240.         output << "Mean:               " << die1_mean << "\n";
  241.         output << "Median:             " << die1_med << "\n";
  242.         output << "Mode:               " << die1_mode << "\n";
  243.         output << "Standard Deviation: " << die1_stddiv << "\n";
  244.         output << "\n";
  245.         output << "**** Regular Dice 12 Sides Data ****\n";
  246.         output << "Mean:               " << die2_mean << "\n";
  247.         output << "Median:             " << die2_med << "\n";
  248.         output << "Mode:               " << die2_mode << "\n";
  249.         output << "Standard Deviation: " << die2_stddiv << "\n";
  250.         output << "\n";
  251.         output << "**** Loaded Dice 6 Sides Data ****\n";
  252.         output << "Mean:               " << die3_mean << "\n";
  253.         output << "Median:             " << die3_med << "\n";
  254.         output << "Mode:               " << die3_mode << "\n";
  255.         output << "Standard Deviation: " << die3_stddiv << "\n";
  256.         output << "\n";
  257.         output << "**** Loaded Dice 12 Sides Data ****\n";
  258.         output << "Mean:               " << die4_mean << "\n";
  259.         output << "Median:             " << die4_med << "\n";
  260.         output << "Mode:               " << die4_mode << "\n";
  261.         output << "Standard Deviation: " << die4_stddiv << "\n";
  262.         output << "\n";
  263.     }  
  264.     output.close();
  265.    
  266.     return 0;
  267. }
  268.  
  269. /*********************************************************
  270. * Entry: std::vector                                     *
  271. *                                                        * 
  272. * Exit: vector mode obtained                             *
  273. *                                                        *     
  274. * Purpose: To determine the mode of the sequence         * 
  275. *********************************************************/
  276. vector<int> modeVect (vector<int>& vect)
  277. {
  278.     int current;                //current number being tallied
  279.     int count = 0;              //iteration
  280.     int old_freq = 0;           //compare to older mode
  281.     vector<int> mode;
  282.  
  283.     do{
  284.         int frequency = 0;      //frequency a number appears
  285.         current = vect[count];
  286.  
  287.         for(int i = 0; i < vect.size(); i++)
  288.         {
  289.             if(vect[i] > current)
  290.                 break;
  291.  
  292.             if(vect[i] == current)
  293.             {
  294.                 frequency++;
  295.             }  
  296.         }
  297.  
  298.         //if the current mode has the same frequency of the current number being tested
  299.         if(frequency == old_freq)
  300.         {
  301.             for(int j = 0; j < mode.size(); j++)
  302.             {
  303.                 if(mode[j] == current)
  304.                     goto same;
  305.             }  
  306.             mode.push_back(current);
  307.         }
  308.        
  309.         //if the current number has a greater frequency than the previous largest
  310.         if(frequency > old_freq)
  311.         {
  312.             mode.clear();
  313.             mode.push_back(current);
  314.             old_freq = frequency;
  315.         }
  316.        
  317.         same:
  318.         count++;
  319.     //as long as the count is less than the size of the parameter vector   
  320.     }while (count < vect.size());      
  321.  
  322.     return mode;
  323.  
  324. }
  325.  
  326. double std_div(vector<int>& vec, double mean)
  327. {
  328.     vector<double> holder(100);
  329.     double holder_sum;
  330.     double holder_div;
  331.     double standard_div = 0;
  332.  
  333.     //squares the value minus the mean
  334.     for(int x = 0; x < vec.size(); x++)
  335.     {
  336.         holder[x] = pow((vec[x] - mean), 2);
  337.     }  
  338.  
  339.     //sum of new vector
  340.     for(int x=0; x < holder.size(); x++)
  341.     {
  342.         holder_sum += holder[x];
  343.     }
  344.  
  345.     holder_div = (holder_sum/holder.size());
  346.  
  347.     standard_div = sqrt(holder_div);
  348.  
  349.     return standard_div;
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement