Answer to Question #170195 in C++ for Phillip Le

Question #170195

A hospital administrator wished to develop a regression model for predicting the degree of long-term recovery after discharge from the hospital for severely injured patients. The predictor variable to be utilized is number of days of hospitalization (X), and the response variable is a prognostic index for long- term recovery (Y), with large values of the index reflecting a good prognosis. Data for 15 patients were studied and are presented in a file Related earlier studies reported in the literature found the relationship between the predictor variable and the response variable to be exponential. Hence, it was decided to investigate the appropriateness of the two-parameter nonlinear exponential regression mode.

data.txt:

Days  Prognostic_index

2   54

5   50

7   45

10   37

14   35

19   25

26   20

31   16

34   18

38   13

45   8

52   11

53   8

60   4

65   6

Write a C++ program that computes calculations: mean, mode, median, variance, standard deviation, and range for a set of numbers given in the data file (Prognostic_index). The output should be to two decimal places.


1
Expert's answer
2021-03-09T10:57:38-0500

Hello,

Please kindly accept the following decision.


#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
using namespace std;

int main() {
   const vector <double> numbers = {54, 50, 45, 37, 35, 25, 20, 16, 18, 13, 8, 11, 8, 4, 6 };

   // counting all numbers, check for consistency, sort and print them
   const int numbersCount = numbers.size();
   if (!numbersCount) {
      cout << "No input data";
      return -1;
   }
   cout << "Input    : ";
   for (auto elem : numbers) cout << elem << " ";
   cout << endl;

   vector <double> numbersSortAsc = numbers;
   sort(begin(numbersSortAsc), end(numbersSortAsc));
   cout << "Sorted   : ";
   for (auto elem : numbersSortAsc) cout << elem << " ";
   cout << endl << endl;

   // setting two decimal places output
   cout << fixed << setprecision(2);

   // mean is the average of all numbers
   const double numbersSum = accumulate(begin(numbers), end(numbers), 0.0);
   const double mean = numbersSum / numbersCount;
   cout << "Mean     : " << mean << endl;

   // mode is the number which appears most often among all numbers
   map <double, unsigned> numbersMap;
   for (const auto& elem : numbers) ++numbersMap[elem];      
   using pair_type = decltype(numbersMap)::value_type;
   auto mode = max_element(begin(numbersMap), end(numbersMap),
                     [](const pair_type& p1, const pair_type& p2) {
                     return p1.second < p2.second; });
   cout << "Mode     : " << mode->first << endl;

   // median is the middle of a sorted list of numbers if amount of numbers is odd
   // with even amount of numbers median is average of the middle pair of numbers
   double median = 0;
   const bool odd = numbersCount % 2;
   const int middleIndex = numbersCount / 2;   
   if (odd) 
      median = numbersSortAsc[middleIndex];
   else
      median = (numbersSortAsc[middleIndex - 1] + numbersSortAsc[middleIndex]) / 2;
   cout << "Median   : " << median << endl;

   // variance is an average of the squared differences from the mean
   vector <double> numbersSqDiff = numbers;
   transform(begin(numbersSqDiff), end(numbersSqDiff), begin(numbersSqDiff),
           [&](auto elem) { return (elem - mean) * (elem - mean); });
   const double numbersSqDiffSum = accumulate(begin(numbersSqDiff), end(numbersSqDiff), 0.0);
   const double variance = numbersSqDiffSum / numbersCount;
   cout << "Variance : " << variance << endl;

   // standard deviation is a measure of how widely numbers are spread out
   // it is being calculated as the square root of the variance
   const double deviation = sqrt(variance);
   cout << "Deviation : " << deviation << endl;

   // range is the difference between the lowest and highest values
   const auto highlowNumber = minmax_element(begin(numbers), end(numbers));
   const double range = *highlowNumber.second - *highlowNumber.first;
   cout << "Range    : " << range << endl;

   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS