Objective: Implement a program that manipulates population information about Ame
ID: 3859135 • Letter: O
Question
Objective: Implement a program that manipulates population information about American states (name, capital, population). Program Description: 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. Deliverables. 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
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
struct entry{
string name;
string capital;
long population;
};
void sortByPopulation(vector<entry> &data){
entry temp;
for (int i = 0; i<data.size(); i++){
for (int j=0; j<data.size(); j++){
if (data[i].population < data[j].population){
temp.name = data[i].name;
temp.capital = data[i].capital;
temp.population = data[i].population;
data[i].name = data[j].name;
data[i].capital = data[j].capital;
data[i].population = data[j].population;
data[j].name = temp.name;
data[j].capital = temp.capital;
data[j].population = temp.population;
}
}
}
}
void sortByState(vector<entry> &data){
entry temp;
for (int i = 0; i<data.size(); i++){
for (int j=0; j<data.size(); j++){
if (data[i].name < data[j].name){
temp.name = data[i].name;
temp.capital = data[i].capital;
temp.population = data[i].population;
data[i].name = data[j].name;
data[i].capital = data[j].capital;
data[i].population = data[j].population;
data[j].name = temp.name;
data[j].capital = temp.capital;
data[j].population = temp.population;
}
}
}
}
int main(){
ifstream fin;
vector<entry> data;
string name;
string capital;
long population;
entry item;
fin.open("statedata.txt"); //Assuming data is in Input.txt and every line conatins name, capital and population separated with spaces
while (fin >> name){
item.name = name;
if (name == "North"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "South"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "New"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "West"){
fin >> name;
item.name = item.name + " " + name;
}
if (name == "Rhode"){
fin >> name;
item.name = item.name + " " + name;
}
fin >> capital;
item.capital = capital;
if (capital == "Little"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Des"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Baton"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Salt"){
fin >> capital;
item.capital = item.capital + " " + capital;
item.capital = capital;
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Carson"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Santa"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "St."){
fin >> capital;
item.capital = item.capital + " " + capital;
}
if (capital == "Oklahoma"){
fin >> capital;
item.capital = item.capital + " " + capital;
}
fin >> population;
item.population = population;
data.push_back(item);
}
cout << "All State Information ";
for (int i = 0; i<data.size(); i++){
cout << data[i].name << " " << data[i].capital << " " << data[i].population << endl;
}
cout << "-------------------------------------------------------------------" << endl;
sortByPopulation(data);
cout << "Sorted by Population ";
for (int i = 0; i<data.size(); i++){
cout << data[i].name << " " << data[i].capital << " " << data[i].population << endl;
}
cout << "-------------------------------------------------------------------" << endl;
sortByState(data);
cout << "Sorted by State ";
for (int i = 0; i<data.size(); i++){
cout << data[i].name << " " << data[i].capital << " " << data[i].population << endl;
}
cout << "-------------------------------------------------------------------" << endl;
cout << "States with population less that 5 million ";
for (int i = 0; i<data.size(); i++){
if (data[i].population < 500000)
cout << data[i].name << " " << data[i].capital << " " << data[i].population << endl;
}
cout << "-------------------------------------------------------------------" << endl;
cout << "States with population more than 10 million ";
for (int i = 0; i<data.size(); i++){
if (data[i].population > 1000000)
cout << data[i].name << " " << data[i].capital << " " << data[i].population << endl;
}
return 0;
}
The input file(statedata.txt) has been created from states2000.dat as follows:
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 Screenshots are big (Not all the data is visible in one screen) so not attached
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.