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
|
std::string path = "C:\\Users\\Lord Brebo\\Desktop\\us-pop-by-state-with-electors-2015.txt";
std::ifstream file;
std::string *stateName;
int *pop, electors, siz=1, count=0, totalPop=0, i=0;
double percentWithinSD = 0.0, meanPop = 0.0, SDPop = 0.0, pos=0.0, neg=0.0;
//two arrays, one for stateName and one for poplulation
stateName = new std::string[siz];
pop = new int[siz];
file.open(path);
if (!file)
{
cout << "unable to open file: " << path << endl;
return -1;
}
file >> siz;
while (!file.eof())
{
file >> stateName[i] >> pop[i] >> electors;
++i;
++count;
}
file.close();
//calculate mean
while (i=0, pop[i] < siz, i++)
{
totalPop += pop[i];
}
meanPop = totalPop / siz;
//calculate Variance
cout << "US Population Statistics as of 2015 (" << siz << " states)" << endl;
cout << " Total population: " << totalPop << endl;
cout << " Population mean: " << meanPop << endl;
cout << " Population standard deviation: " << SDPop << endl;
cout << " States whose population is within .5 standard deviations of the mean:" << endl;
//list states within .5 Standard deviation
while (i = 0, i < siz, i++)
{
if (neg >= pop[i] <= pos)
{
cout << stateName[i] << endl;
}
}
//calculate % of states that fit ^^^^ that
cout << percentWithinSD << "% of the states are within .5 standard deviations of the mean population" << endl;
return 0;
|