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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
const int SIZE = 100; // Maximum size of array
double CalculateMean(const double x[], int cnt); // Mean of each category
double CalculateMax(const double x[], int cnt); // Maximum of each category
double CalculateSum(const double x[], int cnt); // Sum of each category
double SumOfProducts1(double Income[], double Deaths[], int cnt);
double SumOfProducts2(double Doctors[], double Deaths[], int cnt);
double SumOfSquares1(double Income[], int cnt);
double SumOfSquares2(double Doctors[], int cnt);
int main ()
{ double Deaths[SIZE];
double Doctors[SIZE];
double Hospitals[SIZE];
double Income[SIZE];
double Population[SIZE];
ifstream health("health.txt"); // Open health.txt
string firstline;
int cnt = 0;
double MeanDeaths, MeanDoctors, MeanHospitals, MeanIncome, MeanPopulation;
double MaxDeaths, MaxDoctors, MaxHospitals, MaxIncome,MaxPopulation;
double SumDeaths, SumDoctors, SumHospitals, SumIncome, SumPopulation;
double SumOfProd1, SumOfSq1, slope1 = 0.0, yInt1 = 0.0;
double SumOfProd2, SumOfSq2, slope2 = 0.0, yInt2 = 0.0;
// Check if open failed
if (! health.is_open())
{ cout << "Failed to open input file" << endl;
return 1;
}
getline(health, firstline);
// Read input
while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
{
cnt++;
}
MeanDeaths = CalculateMean (Deaths, cnt);
MeanDoctors = CalculateMean (Doctors, cnt);
MeanHospitals = CalculateMean (Hospitals, cnt);
MeanIncome = CalculateMean (Income, cnt);
MeanPopulation = CalculateMean (Population, cnt);
SumDeaths = CalculateSum(Deaths, cnt);
SumDoctors = CalculateSum(Doctors, cnt);
SumHospitals = CalculateSum(Hospitals, cnt);
SumIncome = CalculateSum(Income, cnt);
SumPopulation = CalculateSum(Population, cnt);
MaxDeaths = CalculateMax (Deaths, cnt);
MaxDoctors = CalculateMax (Doctors, cnt);
MaxHospitals = CalculateMax (Hospitals, cnt);
MaxIncome = CalculateMax (Income, cnt);
MaxPopulation = CalculateMax (Population, cnt);
cout << "Mean, Max, Sum of Death = " << setprecision(2) << fixed << MeanDeaths << setw(8) << MaxDeaths << setw(8) << SumDeaths << endl;
cout << "Mean, Max, Sum of Doctors = " << MeanDoctors << setw(8) << MaxDoctors << setw(10) << SumDoctors << endl;
cout << "Mean, Max, Sum of Hospitals = " << MeanHospitals << setw(10) << MaxHospitals << setw(10) << SumHospitals << endl;
cout << "Mean, Max, Sum of Income = " << MeanIncome << setw(7) << MaxIncome << setw(8) << SumIncome << endl;
cout << "Mean, Max, Sum of Population = " << MeanPopulation << setw(8) << MaxPopulation << setw(9) << SumPopulation << endl;
SumOfProd1 = SumOfProducts1(Income, Deaths, cnt);
SumOfSq1 = SumOfSquares1(Income, cnt);
SumOfProd2 = SumOfProducts2(Doctors, Deaths, cnt);
SumOfSq2 = SumOfSquares2(Doctors, cnt);
slope1 = (SumIncome*SumDeaths - cnt*SumOfProd1) / (SumOfSq1 - cnt*SumOfSq1);
yInt1 = (SumIncome*SumOfProd1 - SumOfSq1*SumDeaths) / (SumOfSq1 - cnt*SumOfSq1);
cout << "Income vs Death" << setw(6) << "y= " << slope1 << "x + "; cout << yInt1 << endl;
slope2 = (SumDoctors*SumDeaths - cnt*SumOfProd2) / (SumOfSq1 - cnt*SumOfSq1);
yInt2 = (SumDoctors*SumOfProd2 - SumOfSq2*SumDeaths) / (SumOfSq2 - cnt*SumOfSq2);
cout << "Doctors vs Death" << setw(6) << "y= " << slope2 << "x + "; cout << yInt2 << endl;
return 0;
}
double CalculateMean(const double x[], int cnt)
{
double sum = 0.0;
for (int i=0; i<cnt; i++)
{
sum += x[i];
}
return sum/cnt;
}
double CalculateMax (const double x[], int cnt)
{
double Max;
Max = x[0];
for (int i=0; i<cnt; i++)
{
if (x[i] > Max)
Max = x[i];
}
return Max;
}
double CalculateSum(const double x[], int cnt)
{
double sumOfFile = 0.0;
for (int i=0; i<cnt; i++)
{
sumOfFile += x[i];
}
return sumOfFile;
}
double SumOfProducts1(double Income[], double Deaths[], int cnt)
{
double SumOP1 = 0.0;
for (int i=0; i<cnt-1; i++)
{
SumOP1 += Income[i] * Deaths[i];
}
return SumOP1;
}
double SumOfSquares1(double Income[], int cnt)
{
double SumOS1 = 0.0;
for (int i=0; i<cnt-1; i++)
{
SumOS1 += Income[i] * Income[i];
}
return SumOS1;
}
double SumOfProducts2(double Doctors[], double Deaths[], int cnt)
{
double SumOP2 = 0.0;
for (int i=0; i<cnt-1; i++)
{
SumOP2 += Doctors[i] * Deaths[i];
}
return SumOP2;
}
double SumOfSquares2(double Doctors[], int cnt)
{
double SumOS2 = 0.0;
for (int i=0; i<cnt-1; i++)
{
SumOS2 += Doctors[i] * Doctors[i];
}
return SumOS2;
}
| |