1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
double mean(const double[], const int);
double max(const double[], const int);
double sum(const double[], const int);
double sumProducts(const double[], const int, const double[]);
double sumSquares(const double[], const int);
double slope(const double[], const int, const double[]);
double intercept(const double[], const int, const double[]);
int main()
{
const int SIZE = 100;
int COUNT = 0;
double Deaths[SIZE], Doctors[SIZE], Hospitals[SIZE], Income[SIZE], Population[SIZE];
ifstream health("health.txt");
string firstline;
if (!health.is_open())
{
cout << "Failed to open input file" << endl;
return 1;
}
else
{
getline(health, firstline);
while (health >> Deaths[COUNT] >> Doctors[COUNT] >> Hospitals[COUNT] >> Income[COUNT] >> Population[COUNT])
COUNT++;
}
cout << "Mean, Max, Sum of DEATH: " << setprecision(2) << fixed
<< mean(Deaths, COUNT) << '\t' << max(Deaths, COUNT) << '\t'
<< sum(Deaths, COUNT) << endl;
cout << "Mean, Max, Sum of DOC: "
<< setw(8) << mean(Doctors, COUNT) << '\t' << max(Doctors, COUNT) << '\t'
<< sum(Doctors, COUNT) << endl;
cout << "Mean, Max, Sum of HOSP: "
<< setw(7) << mean(Hospitals, COUNT) << '\t' << max(Hospitals, COUNT) << '\t'
<< sum(Hospitals, COUNT) << endl;
cout << "Mean, Max, Sum of INC: "
<< setw(6) << mean(Income, COUNT) << '\t' << max(Income, COUNT) << '\t'
<< sum(Income, COUNT) << endl;
cout << "Mean, Max, Sum of POPUL: "
<< mean(Population, COUNT) << '\t' << max(Population, COUNT) << '\t'
<< sum(Population, COUNT) << endl;
cout << "" << endl;
cout << "Income vs Deaths:" << setw(7) << "y = " << slope(Income, COUNT, Deaths) << "x + ";
cout << intercept(Income, COUNT, Deaths) << endl;
cout << "Doctors vs Deaths:" << setw(6) << "y = " << slope(Doctors, COUNT, Deaths) << "x + ";
cout << intercept(Doctors, COUNT, Deaths) << endl;
return 0;
}
double mean(const double x[], const int n)
{
double sum = 0;
for (int i = 0; i < n; i++)
{
sum += x[i];
}
return sum / n;
}
double max(const double x[], const int n)
{
// Determine local objects
double max;
// Determine maximum value in the array
max = x[0];
for (int i = 0; i < n; i++)
{
if (x[i] > max)
max = x[i];
}
return max;
}
double sum(const double x[], const int n)
{
double total= 0.0;
for (int i = 0; i < n; i++)
total+= x[i];
return total;
}
double sumProducts(const double x[], const int n, const double y[])
{
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += x[i] * y[i];
return sum;
}
double sumSquares(const double x[], const int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
sum += x[i] * x[i];
return sum;
}
double slope(const double x[], const int n, const double y[])
{
return (n * sumProducts(x, n, y) - sum(x, n) * sum(y, n)) / (n * sumSquares(x, n) - sum(x, n) * sum(x, n));
}
double intercept(const double x[], const int n, const double y[])
{
double a = 0;
a += (sum(x, n) * sumProducts(x, n, y) - sumSquares(x, n) * sum(y, n)) / (sum(x, n) * sum(x, n) - n * sumSquares(x, n));
return a;
}
|