Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a program that manipulates population information about American state

ID: 3859167 • Letter: I

Question

Implement a program that manipulates population information about American states (name, capital, population). Your program must be able to perform the following operations: Store the state information from an external file into an array (of structs). Display all state information. Sort the state information array, by state name. Sort the state information array, by population. Display states whose population is less than 5 million. Display states whose population is greater than 10 million. Estimated state populations as of 2000: http: //www2.cs.uidaho.edu/"bruceb/cs120/Assignments/states2000.dat A complete program to perform the manipulations described above. Output to demonstrate that your program behaves properly. An programming log that describes the time required to implement your program.

Explanation / Answer

The completed program is given below. The input data had to be cleaned and formatted so that it could be fed into the program. So removed all unneccesary information from the linked file given in question. The input file for program has the format where each line represents data for a single state. The line has format (no space before and after comma)

name,capital,population

name and capital can have multiple words separated by space. Input file is given below in the answer.

============

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cctype>
#include <iomanip>

using namespace std;

struct state
{
string name;
string capital;
long population;
};

void display(struct state states[], int count);
void sort_by_name(struct state states[], int count);
void sort_by_population(struct state states[], int count);
void display_population_less5(struct state states[], int count); //display states with population < 5million
void display_population_more10(struct state states[], int count); //display states with population > 10million

int load_file(ifstream &infile, struct state states[]);//loads data from file
string tolower(string str); //to convert a string to lower case, used for comparison

/*program reads an input file containing state information for American states i.e state name, captial and population.
The format of the file should be as follows-
each line contains data for 1 state. The values in the line are comma separated (using comma as delimiter since
state names / capitals can have multiple words and hence space can not be used as delimiter). So each line in the file
is of the format
<name>,<capitial>,<population>
*/
int main()
{
string filename;
ifstream infile;
  
  
cout << "Enter input filename: ";
cin >> filename;
  
//try to open input file
infile.open(filename.c_str());
if(!infile.is_open())
{
cout << "Could not open input file " << filename << endl;
exit(1);
}

struct state states[50];
int count = load_file(infile, states);
  
cout << "Loaded " << count << " states from file ... " << endl;
display(states, count);
  
  
cout << "Sorting states by name ... " << endl;
sort_by_name(states, count);
display(states, count);
  
cout << "Sorting states by population ... " << endl;
sort_by_population(states, count);
display(states, count);

  
display_population_less5(states, count);
display_population_more10(states, count);
  
}


void display(struct state states[], int count)
{
cout << " The list of states is " << endl;
cout << "State Name Capital Population" << endl;
for(int i = 0; i < count; i++)
cout << left << setw(25) << states[i].name << setw(25) << states[i].capital << right << setw(10) << states[i].population << endl;
  
cout << endl;
}

void sort_by_name(struct state states[], int count)
{
int minIdx;
string minstr;
for(int i = 0; i < count; i++)
{
minIdx = i;
minstr = tolower(states[i].name);
  
for(int j = i+1; j < count; j++)
{
string name = tolower(states[j].name);
if(name < minstr)
{
minIdx = j;
minstr = name;
}
}
  
if(minIdx != i)
{
struct state temp = states[i];
states[i] = states[minIdx];
states[minIdx] = temp;
}
}
}
void sort_by_population(struct state states[], int count)
{
int minIdx;

for(int i = 0; i < count; i++)
{
minIdx = i;
for(int j = i+1; j < count; j++)
{
if(states[j].population < states[minIdx].population)
minIdx = j;
}
  
if(minIdx != i)
{
struct state temp = states[i];
states[i] = states[minIdx];
states[minIdx] = temp;
}
}

}
void display_population_less5(struct state states[], int count) //display states with population < 5million
{
cout << " The list of states with population less than 5 million is " << endl;
cout << "State Name Capital Population" << endl;
for(int i = 0; i < count; i++)
{
if(states[i].population < 5000000)
cout << left << setw(25) << states[i].name << setw(25) << states[i].capital << right << setw(10) << states[i].population << endl;
  
  
}
cout << endl ;
}
void display_population_more10(struct state states[], int count) //display states with population > 10million
{
cout << " The list of states with population greater than 10 million is " << endl;
cout << "State Name Capital Population" << endl;
for(int i = 0; i < count; i++)
{
if(states[i].population > 10000000)
cout << left << setw(25) << states[i].name << setw(25) << states[i].capital << right << setw(10) << states[i].population << endl;
}
cout << endl ;
}

int load_file(ifstream &infile, struct state states[])//loads data from file
{
int count;
string line;
while(true)
{
getline(infile, line);
if(line.length() <= 1) //reached end of file
break;
  
//extract the state name
int idx1 = line.find(",");
states[count].name = line.substr(0, idx1);
  
//extract the capital
int idx2 = line.find_last_of(",");
states[count].capital = line.substr(idx1 + 1, idx2 - idx1 - 1);
  
//extract and convert population
states[count].population = atol(line.substr(idx2 + 1).c_str());
count++;
}
  
return count;
}
string tolower(string str) //to convert a string to lower case, used for comparison
{
string res = "";
for(int i = 0; i < str.length(); i++)
{
char ch = str[i];
if(ch >= 'A' && ch <= 'Z') //convert all upper case to lower
ch = tolower(ch);
res += ch;
}
  
return res;
}

input file : population.txt (NO BLANK LINE ANYWHERE IN FILE)

Idaho,Boise,1297274
Washington,Olympia,5908684
Vermont,Montpelier,609890
Hawaii,Honolulu,1216642
Arizona,Phonenix,5140683
Montana,Helena,905316
Virginia,Richmond,7100702
North Dakota,Bismarck,643756
Colorado,Denver,4311882
Mississippi,Jackson,2852927
Delaware,Dover,785068
Georgia,Atlanta,8206975
South Carolina,Columbia,4025061
Illinois,Springfield,12439042
Nebraska,Lincoln,1715369
Arkansas,Little Rock,2679733
New Hampshire,Concord,1238415
Ohio,Columbus,11374540
Kansas,Topeka,2693824
Louisiana,Baton Rouge,4480271
Michigan,Lansing,9955829
Florida,Talahasse,16028890
Connecticut,Hartford,3409535
Iowa,Des Moines,2931923
West Virginia,Charleston,1813077
Missouri,Jefferson,5606260
Wyoming,Cheyenne,495304
California,Sacramento,33930798
Alabama,Montgomery,4461130
New Jersey,Trenton,8424354
South Dakota,Pierre,756874
Maryland,Annapolis,5307886
Indiana,Indianapolis,6090782
Oklahoma,Oklahoma City,3458819
North Carolina,Raleigh,8067673
New York,Albany,19004973
Rhode Island,Providence,1049662
Massachusetts,Boston,6355568
Texas,Austin,20903994
Kentucky,Frankfort,4049431
Alaska,Juneau,628933
Utah,Salt Lake City,2236714
Nevada,Carson City,2002032
Maine,Augusta,1277731
Tennessee,Nashville,5700037
New Mexico,Santa Fe,1823821
Oregon,Salem,3428543
Minnesota,St. Paul,4925670
Wisconsin,Madison,5371210
Pennsylvania,Harrisburg,12300670

OUTPUT

Enter input filename: population.txt
Loaded 50 states from file ...

The list of states is
State Name        Capital        Population
Idaho Boise 1297274
Washington Olympia 5908684
Vermont Montpelier 609890
Hawaii Honolulu 1216642
Arizona Phonenix 5140683
Montana Helena 905316
Virginia Richmond 7100702
North Dakota Bismarck 643756
Colorado Denver 4311882
Mississippi Jackson 2852927
Delaware Dover 785068
Georgia Atlanta 8206975
South Carolina Columbia 4025061
Illinois Springfield 12439042
Nebraska Lincoln 1715369
Arkansas Little Rock 2679733
New Hampshire Concord 1238415
Ohio Columbus 11374540
Kansas Topeka 2693824
Louisiana Baton Rouge 4480271
Michigan Lansing 9955829
Florida Talahasse 16028890
Connecticut Hartford 3409535
Iowa Des Moines 2931923
West Virginia Charleston 1813077
Missouri Jefferson 5606260
Wyoming Cheyenne 495304
California Sacramento 33930798
Alabama Montgomery 4461130
New Jersey Trenton 8424354
South Dakota Pierre 756874
Maryland Annapolis 5307886
Indiana Indianapolis 6090782
Oklahoma Oklahoma City 3458819
North Carolina Raleigh 8067673
New York Albany 19004973
Rhode Island Providence 1049662
Massachusetts Boston 6355568
Texas Austin 20903994
Kentucky Frankfort 4049431
Alaska Juneau 628933
Utah Salt Lake City 2236714
Nevada Carson City 2002032
Maine Augusta 1277731
Tennessee Nashville 5700037
New Mexico Santa Fe 1823821
Oregon Salem 3428543
Minnesota St. Paul 4925670
Wisconsin Madison 5371210
Pennsylvania Harrisburg 12300670

Sorting states by name ...

The list of states is
State Name        Capital        Population
Alabama Montgomery 4461130
Alaska Juneau 628933
Arizona Phonenix 5140683
Arkansas Little Rock 2679733
California Sacramento 33930798
Colorado Denver 4311882
Connecticut Hartford 3409535
Delaware Dover 785068
Florida Talahasse 16028890
Georgia Atlanta 8206975
Hawaii Honolulu 1216642
Idaho Boise 1297274
Illinois Springfield 12439042
Indiana Indianapolis 6090782
Iowa Des Moines 2931923
Kansas Topeka 2693824
Kentucky Frankfort 4049431
Louisiana Baton Rouge 4480271
Maine Augusta 1277731
Maryland Annapolis 5307886
Massachusetts Boston 6355568
Michigan Lansing 9955829
Minnesota St. Paul 4925670
Mississippi Jackson 2852927
Missouri Jefferson 5606260
Montana Helena 905316
Nebraska Lincoln 1715369
Nevada Carson City 2002032
New Hampshire Concord 1238415
New Jersey Trenton 8424354
New Mexico Santa Fe 1823821
New York Albany 19004973
North Carolina Raleigh 8067673
North Dakota Bismarck 643756
Ohio Columbus 11374540
Oklahoma Oklahoma City 3458819
Oregon Salem 3428543
Pennsylvania Harrisburg 12300670
Rhode Island Providence 1049662
South Carolina Columbia 4025061
South Dakota Pierre 756874
Tennessee Nashville 5700037
Texas Austin 20903994
Utah Salt Lake City 2236714
Vermont Montpelier 609890
Virginia Richmond 7100702
Washington Olympia 5908684
West Virginia Charleston 1813077
Wisconsin Madison 5371210
Wyoming Cheyenne 495304

Sorting states by population ...

The list of states is
State Name        Capital        Population
Wyoming Cheyenne 495304
Vermont Montpelier 609890
Alaska Juneau 628933
North Dakota Bismarck 643756
South Dakota Pierre 756874
Delaware Dover 785068
Montana Helena 905316
Rhode Island Providence 1049662
Hawaii Honolulu 1216642
New Hampshire Concord 1238415
Maine Augusta 1277731
Idaho Boise 1297274
Nebraska Lincoln 1715369
West Virginia Charleston 1813077
New Mexico Santa Fe 1823821
Nevada Carson City 2002032
Utah Salt Lake City 2236714
Arkansas Little Rock 2679733
Kansas Topeka 2693824
Mississippi Jackson 2852927
Iowa Des Moines 2931923
Connecticut Hartford 3409535
Oregon Salem 3428543
Oklahoma Oklahoma City 3458819
South Carolina Columbia 4025061
Kentucky Frankfort 4049431
Colorado Denver 4311882
Alabama Montgomery 4461130
Louisiana Baton Rouge 4480271
Minnesota St. Paul 4925670
Arizona Phonenix 5140683
Maryland Annapolis 5307886
Wisconsin Madison 5371210
Missouri Jefferson 5606260
Tennessee Nashville 5700037
Washington Olympia 5908684
Indiana Indianapolis 6090782
Massachusetts Boston 6355568
Virginia Richmond 7100702
North Carolina Raleigh 8067673
Georgia Atlanta 8206975
New Jersey Trenton 8424354
Michigan Lansing 9955829
Ohio Columbus 11374540
Pennsylvania Harrisburg 12300670
Illinois Springfield 12439042
Florida Talahasse 16028890
New York Albany 19004973
Texas Austin 20903994
California Sacramento 33930798


The list of states with population less than 5 million is
State Name        Capital        Population
Wyoming Cheyenne 495304
Vermont Montpelier 609890
Alaska Juneau 628933
North Dakota Bismarck 643756
South Dakota Pierre 756874
Delaware Dover 785068
Montana Helena 905316
Rhode Island Providence 1049662
Hawaii Honolulu 1216642
New Hampshire Concord 1238415
Maine Augusta 1277731
Idaho Boise 1297274
Nebraska Lincoln 1715369
West Virginia Charleston 1813077
New Mexico Santa Fe 1823821
Nevada Carson City 2002032
Utah Salt Lake City 2236714
Arkansas Little Rock 2679733
Kansas Topeka 2693824
Mississippi Jackson 2852927
Iowa Des Moines 2931923
Connecticut Hartford 3409535
Oregon Salem 3428543
Oklahoma Oklahoma City 3458819
South Carolina Columbia 4025061
Kentucky Frankfort 4049431
Colorado Denver 4311882
Alabama Montgomery 4461130
Louisiana Baton Rouge 4480271
Minnesota St. Paul 4925670


The list of states with population greater than 10 million is
State Name        Capital        Population
Ohio Columbus 11374540
Pennsylvania Harrisburg 12300670
Illinois Springfield 12439042
Florida Talahasse 16028890
New York Albany 19004973
Texas Austin 20903994
California Sacramento 33930798

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote