Advertisement
Guest User

Digits

a guest
Oct 28th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>   // both this and the next header are needed for
  4. #include <cstdlib> // rand() to randomly generate numbers
  5. #include <cmath> // sqrt
  6.  
  7. using namespace std;
  8.  
  9. // calculate + print mean
  10. void calcMean(const vector<double> &data)
  11. {
  12.     double sum = 0.0;
  13.     for (size_t i = 0; i < data.size(); ++i)
  14.     {
  15.         sum += data[i];
  16.     }
  17.     cout << "Mean: " << sum / data.size() << endl;
  18. }
  19.  
  20. // calc + print lowest
  21. void findLo(const vector<double> &negative)
  22. {
  23.     double lowest = 0;
  24.     for (size_t i = 0; i < negative.size(); ++i)
  25.     {
  26.         if (lowest > negative[i])
  27.         { lowest = negative[i]; }
  28.     }
  29.     cout << "Lowest value: " << lowest << endl;
  30. }
  31.  
  32. // calc + print lowest
  33. void findHi(const vector<double> &positive)
  34. {
  35.     double highest = 0;
  36.     for (size_t i = 0; i < positive.size(); ++i)
  37.     {
  38.         if (highest < positive[i])
  39.         { highest = positive[i]; }
  40.     }
  41.     cout << "Highest value: " << highest << endl;
  42. }
  43.  
  44. // print the list of both negative and positive numbers
  45. void showVal(const vector<double> &positive,
  46.              const vector<double> &negative)
  47. {
  48.     cout << "List of positive numbers: ";
  49.     for (size_t i = 0; i < positive.size(); ++i)
  50.     {
  51.         cout << positive[i];
  52.         if (i < (positive.size() - 1) ) // no ending comma
  53.         { cout << ", "; }
  54.     }
  55.  
  56.     cout << "\nList of negative numbers: ";
  57.     for (size_t i = 0; i < negative.size(); ++i)
  58.     {
  59.         cout << negative[i];
  60.         if (i < (negative.size() - 1) )
  61.         { cout << ", "; }
  62.     }
  63. }
  64.  
  65. double split(const vector<double> &data)
  66. {
  67.     vector <double> positive;
  68.     vector <double> negative;
  69.     int j = 0; // iterate inner
  70.  
  71.     // add to pos/neg vector depending on value of data[i]
  72.     for (size_t i = 0; i < data.size(); ++i)
  73.     {
  74.  
  75.         if (data[i] >= 0)
  76.         {
  77.             positive.push_back(data[i]);
  78.             j++;
  79.  
  80.         } else if (data[i] < 0)
  81.         {
  82.             negative.push_back(data[i]);
  83.             j++;
  84.         }
  85.     }
  86.  
  87.     // debugging; output pos/neg vectors
  88. /*    for (size_t vC = 0; vC < positive.size(); ++vC)
  89.     {
  90.         cout << positive[vC] << endl;
  91.     }
  92.  
  93.     for (size_t vC = 0; vC < negative.size(); ++vC)
  94.     {
  95.         cout << negative[vC] << endl;
  96.     }*/
  97.  
  98.     // "return" the 2 vectors to the needed functions
  99.     findLo(negative);
  100.     findHi(positive);
  101.     showVal(positive, negative);
  102.  
  103.     return 0;
  104. }
  105.  
  106. // calculate std deviation
  107. double calculateSD(const vector<double> &data)
  108. {
  109.     // calculate sum of data
  110.     double sum = 0.0;
  111.     for (size_t i = 0; i < data.size(); ++i)
  112.         sum += data[i];
  113.  
  114.     // calculate average
  115.     double mean = sum / data.size();
  116.  
  117.     // calculate and return the standard deviation
  118.     double standardDeviation = 0.0;
  119.     for (size_t i = 0; i < data.size(); ++i)
  120.         standardDeviation += (data[i] - mean) * (data[i] - mean);
  121.     return sqrt(standardDeviation / data.size());
  122. }
  123.  
  124.  
  125.  
  126. int main()
  127. {
  128.     // vector with minimum 30 values
  129.     vector <double> data (30);
  130.  
  131.     // seed rand() with time
  132.     srand(static_cast<unsigned int>(time(nullptr)));
  133.    
  134.     // fill vector with neg/pos num
  135.     for (size_t i = 0; i < data.size(); ++i)
  136.     {
  137.         data[i] = rand() - (RAND_MAX / 2);
  138.         //debnugginbg // cout << data[i] << endl;
  139.     }
  140.     // cout << "rand end"; yes this also dbunug
  141.  
  142.     cout << "Standard Deviation: " << calculateSD(data) << endl;
  143.     calcMean(data);
  144.     split(data);
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement