The attached file has a record for each state containing the

The attached file has a record for each state containing the state name (with no blanks - New York is New_York) and the state\'s population. Write a program to read the entire file, calculate the total population of all states and display it. Display the state name and population of the state with the minimum population, and the state with the maximum population. Display the result as:

Total population: _________
The state with the minimum population of ______ is _______
The state with the maximum population of ______ is _______

Based on this text file: https://www.scribd.com/document/331171873/Us-Population-by-State-2015

Solution

Here is the C++ code for you:

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
string fileName;
cout<<\"Enter the name of the file to read: \";
cin>>fileName;
fin.open(fileName);

string stateName, minPopStateName, maxPopStateName;
int population, minPopulation, maxPopulation;
fin>>stateName>>population;
int totalPopulation = population;
minPopStateName = maxPopStateName = stateName;
minPopulation = maxPopulation = population;
while(!fin.eof())
{
fin>>stateName>>population;
if(population < minPopulation)
{
minPopulation = population;
minPopStateName = stateName;
}
if(population > maxPopulation)
{
maxPopulation = population;
maxPopStateName = stateName;
}
totalPopulation += population;
}
cout<<\"Total population: \"<<totalPopulation;
cout<<\"The state with the minimum population of \"<<minPopulation<<\" is \"<<minPopStateName<<endl;
cout<<\"The state with the maximum population of \"<<maxPopulation<<\" is \"<<maxPopStateName<<endl;
}

The attached file has a record for each state containing the state name (with no blanks - New York is New_York) and the state\'s population. Write a program to

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site