M Inbox 117 prabnoc upload Assignment Ass D https Warmingdale oper x C Chegg stu
ID: 3812504 • Letter: M
Question
M Inbox 117 prabnoc upload Assignment Ass D https Warmingdale oper x C Chegg study louided 5e x Ce Secure I https open suny edu/webapps content id- 40995 18 course id- 2015 1&group; id-&mode; view The attached tie has a number ot records giving US state names, populations, an number or ederal electors he fields are delmited hyspaces The tirst held is a single integer giving the number of states that follow The assignment is to cakulate and display some statistics about the state population data, as shown below. Copyright Policy Guidelines Read the first integer and use it to allocate dynamic arrays to contain the state name and the state population. Read in the population dete. The electors fiel used s not Calculete he mean of the population of the states. Calculate he standard deviation otthe pol pulation of the states. e states whose populations are within 05 standa deviations or the mean popluation Calculate the percentage of ate that satisfy this Criteria Cleanup. Output US Population Statistics as of 2015 (51 states Total population. 321418a20 Population mean 6302329.8 Population standard deviation 7130151.4 States whose population is within .5 tandard devietions of the mean New Jersey Vrginia Nevada Uhe stales are within 5 slandard devialiuris ur lie miean povulaliuri Formulas Mean Variance (mean /n Standard Devlation variance us-pop-by-state-with-electors-2015 tot 3:14 PM E- O Search right there 1/6/20Explanation / Answer
please refer below code
#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
using namespace std;
typedef struct data
{
string city;
long long int population;
int elector;
}data;
long long int sum_population(data *arr,int size_arr)
{
long long int sum = 0;
for(int i = 0; i < size_arr; i++)
{
sum += arr[i].population;
}
return sum;
}
double calculate_mean(long long int total_pop,int size_arr)
{
return (total_pop * 1.0)/size_arr;
}
double std_deviation(data *arr,double mean,int size_arr)
{
double var=0.0;
for(int i = 0; i < size_arr; i++)
{
var += ((mean - arr[i].population) * (mean - arr[i].population));
}
return (sqrt(var/size_arr));
}
int city_half_sd(data *arr, int size_arr,double mean, double SD)
{
double temp;
int count_city = 0;
temp = 0.5 * SD ;
for(int i = 0; i < size_arr; i++)
{
if(arr[i].population <= temp)
{
cout<<arr[i].city<<endl;
count_city++;
}
}
return count_city;
}
int main()
{
string city;
long long int population;
int elector,size_arr;
data *arr;
data temp;
int i = 0,count_city;
std::ifstream myfile("us-pop-by-state-with-electors-2015.txt");
long long int total_pop;
double pop_mean,SD,per;
myfile >> size_arr;
arr = new data[size_arr];
while(myfile >> city >> population >> elector)
{
arr[i].city = city;
arr[i].elector = elector;
arr[i].population = population;
i++;
}
total_pop = sum_population(arr,size_arr);
pop_mean = calculate_mean(total_pop,size_arr);
SD = std_deviation(arr,pop_mean,size_arr);
cout<<setprecision(8)<<endl;
cout<<"US Population Statistics as of 2015 ("<<size_arr<<" states)"<<endl;
cout<<"Total Population "<<total_pop<<endl;
cout<<"Population mean "<<pop_mean<<endl;
cout<<"Population Standard Deviation "<<SD<<endl;
cout<<"State whose population is within 0.5 standard deviation of mean "<<endl;
count_city = city_half_sd(arr,size_arr,pop_mean,SD);
cout<<count_city<<size_arr<<endl;
per = ((count_city * 1.0)/size_arr) * 100.0;
cout<<per<<"% of the states are within 0.5 standard deviations of the mean population"<<endl;
return 0;
}
please refer below output
US Population Statistics as of 2015 (51 states)
Total Population 321418820
Population mean 6302329.8
Population Standard Deviation 7130151.4
State whose population is within 0.5 standard deviation of mean
Iowa
Utah
Mississippi
Arkansas
Kansas
Nevada
New_Mexico
Nebraska
West_Virginia
Idaho
Hawaii
New_Hampshire
Maine
Rhode_Island
Montana
Delaware
South_Dakota
North_Dakota
Alaska
Washington_DC
Vermont
Wyoming
2251
43.137255% of the states are within 0.5 standard deviations of the mean population
Process returned 0 (0x0) execution time : 0.031 s
Press any key to continue.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.