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
|
#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
int main ()
{ double Deaths[SIZE]; // Changed to double to match input
double Doctors[SIZE];
double Hospitals[SIZE];
double Income[SIZE]; // Changed to double to match input
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;
// Check if open failed
if (! health.is_open())
{ cout << "Failed to open input file" << endl;
return 1;
}
// Eat the column headers
getline(health, firstline);
// Read the input
// Note that we don't use eof()
while (health >> Deaths[cnt] >> Doctors[cnt] >> Hospitals[cnt] >> Income[cnt] >> Population[cnt])
{ cnt++; // Increment numbeer of records read
}
MeanDeaths = CalculateMean (Deaths, cnt);
cout << "Mean deaths = " << MeanDeaths << endl;
MeanDoctors = CalculateMean (Doctors, cnt);
cout << "Mean doctors = " << MeanDoctors << endl;
MeanHospitals = CalculateMean (Hospitals, cnt);
cout << "Mean hospitals = " << MeanHospitals << endl;
MeanIncome = CalculateMean (Income, cnt);
cout << "Mean income = " << MeanIncome << endl;
MeanPopulation = CalculateMean (Population, cnt);
cout << "Mean population = " << MeanPopulation << endl;
MaxDeaths = CalculateMax (Deaths, cnt);
cout << "Max Deaths = " << MaxDeaths << endl;
MaxDoctors = CalculateMax (Doctors, cnt);
cout << "Max doctors = " << MaxDoctors << endl;
MaxHospitals = CalculateMax (Hospitals, cnt);
cout << "Max hospitals = " << MaxHospitals << endl;
MaxIncome = CalculateMax (Income, cnt);
cout << "Max income = " << MaxIncome << endl;
MaxPopulation = CalculateMax (Population, cnt);
cout << "Max population = " << MaxPopulation << endl;
SumDeaths = CalculateSum(Deaths, cnt);
cout << "Sum deaths = " << SumDeaths << endl;
SumDoctors = CalculateSum(Doctors, cnt);
cout << "Sum doctors = " << SumDoctors << endl;
SumHospitals = CalculateSum(Hospitals, cnt);
cout << "Sum hospitals = " << SumHospitals << endl;
SumIncome = CalculateSum(Income, cnt);
cout << "Sum income = " << SumIncome << endl;
SumPopulation = CalculateSum(Population, cnt);
cout << "Sum population = " << SumPopulation << endl;
// Perform other calculations and outputs here
system ("pause");
return 0;
}
double CalculateMean(const double x[], int cnt)
{
double sum(0);
for (int i=0; i<SIZE; i++)
{
sum += x[i];
}
return sum/cnt;}
double CalculateMax (const double x[], int cnt)
{
// Determine local objects
double Max;
// Determine maximum value in the array
Max = x[0];
for (int i=1; i<SIZE; i++)
{
if (x[i] > Max)
Max = x[i];
}
// Return maximum value. /
return Max;
}
double CalculateSum(const double x[], int cnt)
{
double sum = 0.0;
for (int i=0; i<SIZE; i++)
{
sum += x[i];
}
return sum;
}
|