Write a C ++ program to explore a small set of state and county economic data. S
ID: 3783086 • Letter: W
Question
Write a C ++ program to explore a small set of state and county economic data. Specifically, your program should take a single command line argument which contains the path to a file containing state and county economic data. For example, if your executable program is called explore_econdata, you should be able to run it like this:
Your program must read all of the data from the file that is specified and then allow the user to interactively explore that data.
Data file format and storage
The file will contain the following data for some number of states:
Statewide 2007 unemployment rate
Statewide 2015 unemployment rate
Statewide median household income
For some number of counties in the state:
County 2007 unemployment rate
County 2015 unemployment rate
County median household income
It will have the following format:
For example, here's what a very small data file might look like:
A more extensive set of data is provided for you in test_data.txt. You can use this file to test your program.
To hold this data, your program should use these two structures:
Your program should use the first value in the file (n_states) to allocate a dynamic array of type struct state big enough to hold data about all of the states. Then, it should read the info about the first state and dynamically allocate the state's array of counties based on its value of n_counties. After the county array is allocated, you can read the county data for the state and store if in the county array. Repeat this process for each of the states.
Program features
After your program reads and stores the state and county data from the specified file, it should allow the user to interactively explore the data. Specifically, you should allow the user to do these things:
Print the state with the highest median household income.
Print the state with the lowest median household income.
Print the state with the highest unemployment in 2015.
Print the state with the lowest unemployment in 2015.
Print the states in sorted order by change in unemployment from 2007 to 2015 (i.e. start with the largest decrease in unemployment and end with the largest increase in unemployment).
Print the states in sorted order based on median household income.
Print all states with
Select a state and then:
Print the county in that state with the highest median household income.
Print the county in that state with the lowest median household income.
Print the county in that state with the highest unemployment in 2015.
Print the county in that state with the lowest unemployment in 2015.
Print the counties in that state in sorted order by change in unemployment from 2007 to 2015 (i.e. start with the largest decrease in unemployment and end with the largest increase in unemployment).
Print the counties in that state in sorted order based on median household income.
After performing one of these tasks, your program should allow the user to select another task to perform, and it should continue asking until the user quits the program.
In addition, your program should exit with an error message if the user does not specify a valid filename.
Program design
It is up to you how to design an interface to allow the user to perform the tasks above. A system of menus where the user can enter values (e.g. integer values) to select operations from those menus is one option.
Your program should contain at least the following functions (keeping these prototypes):
struct state* allocate_states(int) - Allocates an array of a specified number of states.
void read_state_data(struct state*, int, std::ifstream&) - Reads data for a specified number of states from an input file stream into a given (pre-allocated) array.
struct county* allocate_counties(int) - Allocates an array of a specified number of counties.
void read_county_data(struct county*, int, std::ifstream&) - Reads data for a specified number of counties from an input file stream into a given (pre-allocated) array.
void free_state_data(struct state*, int) - Releases all data (including county data) allocated to given array. You must make sure to call this function when needed to make sure any allocated data is freed before it is lost or before the program exits.
In addition, you should write any other functions needed to implement the features described above. Your program should be well modularized. That is, your program should be factored into reasonably-sized (preferably small) functions that each perform some specific logical task.
You should separate your source code into interface and implementation by writing it using three source code files:
econdata.hpp - This should contain definitions for your structures and prototypes for your functions.
econdata.cpp - This should contain implementations of the functions prototyped in econdata.hpp.
explore_econdata.cpp - This should contain your main() function.
You should also write a Makefile that specifies the compilation of your program.
Code style
You must include a header comment for each source code file that contains a description of the file (including how to run the program, command line arguments, etc. if the file contains your main() function), your name, and the date. Your code should be well commented, including header comments for allfunctions describing what the function does, its parameters, and any pre- and post-conditions for the function. You should appropriately use whitespace, newlines, and indentation.
Make sure you review the style guidelines for the course, and start trying to follow them:
http://web.engr.oregonstate.edu/~hessro/files/cs162/162_style_guideline.pdf
Submitting your program
To submit your program, you need to make sure the following files are their:
econdata.hpp
econdata.cpp
explore_econdata.cpp
Makefile
text_data.txt
3 Oregon 5.2 5.7 51088 36 Baker 5.8 6.9 38966 Benton 4.1 4.3 54089 Clackamas 4.6 5.2 65555 Clatsop 4.7 5.6 45132 Columbia 5.7 7.2 57517 Coos 6.6 7.6 39956 Crook 6.2 8.5 38155 Curry 6.5 8.5 40524 Deschutes 5.0 6.0 52387 Douglas 7.7 7.7 41504 Gilliam 4.5 6.5 51978 Grant 8.0 8.9 41304 Harney 7.3 7.3 36340 Hood_River 4.5 4.7 54063 Jackson 5.6 6.9 44835 Jefferson 6.7 7.4 42753 Josephine 7.0 7.9 37230 Klamath 6.9 8.0 39234 Lake 7.3 7.8 40328 Lane 5.2 5.9 44976 Lincoln 5.5 6.8 40226 Linn 6.3 6.9 44358 Malheur 5.6 6.5 35094 Marion 5.4 6.1 48554 Morrow 5.4 5.8 51839 Multnomah 4.9 5.0 53519 Polk 4.9 5.7 52626 Sherman 4.8 6.3 53277 Tillamook 4.9 5.8 41662 Umatilla 5.8 6.5 44780 Union 5.5 6.3 44841 Wallowa 6.1 7.9 39743 Wasco 4.9 5.7 44010 Washington 4.3 4.8 66315 Wheeler 5.6 5.2 33387 Yamhill 5.0 5.3 52903 Pennsylvania 4.4 5.1 53224 67 Adams 3.3 4.1 60179 Allegheny 4.1 4.8 52385 Armstrong 5.0 6.4 44326 Beaver 4.5 5.9 51041 Bedford 5.6 5.9 46890 Berks 4.2 4.8 55936 Blair 4.2 5.0 43343 Bradford 4.7 5.7 47242 Bucks 3.8 4.5 77823 Butler 4.0 4.6 60851 Cambria 5.3 6.4 42703 Cameron 6.0 7.0 40257 Carbon 5.4 5.8 49252 Centre 3.5 3.8 51573 Chester 3.1 3.7 85613 Clarion 4.8 5.7 43202 Clearfield 5.5 6.4 40230 Clinton 5.2 6.7 44224 Columbia 5.0 5.5 46938 Crawford 4.9 5.0 43969 Cumberland 3.3 3.9 62692 Dauphin 3.9 4.5 52967 Delaware 4.1 4.7 62867 Elk 4.7 4.8 46965 Erie 4.8 5.3 45476 Fayette 5.7 7.2 36902 Forest 6.6 7.7 37794 Franklin 3.4 4.8 50434 Fulton 5.6 6.0 48084 Greene 5.0 5.8 49897 Huntingdon 5.2 6.8 44531 Indiana 4.7 6.1 42611 Jefferson 4.6 5.9 43810 Juniata 4.2 4.8 48944 Lackawanna 4.8 5.7 45098 Lancaster 3.4 4.0 57369 Lawrence 5.2 6.1 44903 Lebanon 3.5 4.4 53861 Lehigh 4.4 5.2 58283 Luzerne 5.0 6.3 44222 Lycoming 4.9 5.9 45211 McKean 5.0 5.9 44723 Mercer 5.5 5.3 43559 Mifflin 5.2 5.6 40957 Monroe 4.9 6.3 59492 Montgomery 3.5 4.0 79576 Montour 3.9 4.2 54547 Northampton 4.4 5.1 61510 Northumberland 5.1 5.9 40429 Perry 4.1 4.3 54920 Philadelphia 6.1 6.9 39037 Pike 5.4 6.3 58375 Potter 7.2 6.8 40323 Schuylkill 5.0 6.1 43169 Snyder 4.6 4.5 49117 Somerset 5.5 6.6 45706 Sullivan 4.6 6.0 44191 Susquehanna 4.3 5.4 49552 Tioga 5.5 6.3 47360 Union 5.3 4.3 47996 Venango 4.9 5.8 43810 Warren 4.6 4.8 44651 Washington 4.6 5.4 55796 Wayne 4.2 5.7 48663 Westmoreland 4.6 5.2 53536 Wyoming 5.3 5.9 55882 York 3.8 4.6 58471 California 5.4 6.2 61927 58 Alameda 4.7 4.7 76996 Alpine 7.9 7.9 53003 Amador 5.8 6.6 54610 Butte 6.7 7.2 42302 Calaveras 6.4 6.5 53321 Colusa 11.8 15.3 48006 Contra_Costa 4.7 5.0 80338 Del_Norte 7.5 8.6 41419 El_Dorado 5.2 5.7 70235 Fresno 8.6 10.2 43338 Glenn 8.8 8.7 43755 Humboldt 5.9 5.6 40581 Imperial 18.1 24.0 38737 Inyo 4.9 5.8 49267 Kern 8.2 10.2 47451 Kings 8.7 10.5 44490 Lake 8.4 7.7 36333 Lassen 8.2 7.3 49995 Los_Angeles 5.1 6.7 55686 Madera 7.5 10.5 43171 Marin 3.7 3.5 94549 Mariposa 6.0 7.2 47781 Mendocino 5.5 5.9 42840 Merced 10.1 11.4 43818 Modoc 8.1 8.8 39172 Mono 4.8 6.1 59181 Monterey 7.1 8.1 57428 Napa 4.0 4.6 71063 Nevada 4.8 5.5 57118 Orange 3.9 4.5 76061 Placer 4.8 5.0 75689 Plumas 8.5 10.4 47964 Riverside 6.0 6.7 56877 Sacramento 5.4 6.0 55803 San_Benito 7.2 7.6 68166 San_Bernardino 5.6 6.5 51951 San_Diego 4.6 5.2 66034 San_Francisco 4.2 3.6 83788 San_Joaquin 8.1 8.9 51527 San_Luis_Obispo 4.3 4.6 61775 San_Mateo 3.8 3.4 100806 Santa_Barbara 4.4 5.3 62116 Santa_Clara 4.7 4.2 97219 Santa_Cruz 5.9 7.5 64257 Shasta 7.5 7.8 43661 Sierra 8.0 9.0 47293 Siskiyou 8.5 9.4 38641 Solano 5.3 6.1 67106 Sonoma 4.3 4.5 66949 Stanislaus 8.7 9.5 50917 Sutter 9.7 10.8 51827 Tehama 7.2 8.0 40782 Trinity 10.4 7.8 34961 Tulare 9.2 11.7 42360 Tuolumne 6.2 7.1 49476 Ventura 4.9 5.7 74967 Yolo 5.7 6.4 54509 Yuba 9.3 9.2 41403Explanation / Answer
I haven't typed the complete code for the program but rather have tried to explain how you may go about writing it.
First define the structures for the state and county data:-
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
using namespace std;
struct county {
std::string name;
float unemployed_2007;
float unemployed_2015;
int med_income;
}
struct state {
std::string name;
float unemployed_2007;
float unemployed_2015;
int med_income;
struct county* counties;
int n_counties;
}
The main function will open the file entered by the user at commandline and call the various functions as required.
int main (int argc,char* argv[]) {
fstream ifs; //to read the file
int numStates,numCounties; //number of states & counties
char* ch;
struct state *st;
struct county *cn;
ifs.open (argv[1], ios::in);//open the file for read-only
if(!ifs.fail()){
numStates=(int)ifs.read((char*)&ch,sizeof (numStates));
st=allocate_states(numStates);
/*here include the code to show the user menu and all other function calls as required*/
}
else
cout<<"Unable to open file.";
ifs.close();
free_state_data(st, numStates)
return 0;
}
struct state* allocate_states(int n_states){
state* st=new state[n_states];
for (int i=0;i<n_states;i++){
ifs>>st[i]->name;
ifs>>st[i]->unemployed_2007;
ifs>>st[i]->med_income;
ifs>>st[i]->n_counties;
county* cn=new county[];
cn=allocate_counties(st[i]->n_counties);
return st;
}
struct county* allocate_counties(int n){
//use same logic as allocate states
}
void free_state_data(struct state* st, int n){
for(int i=0;i<n;i++){
delete st->counties;
delete st;
}
}
You may use any of the various sorting methods to find the highest and lowest figures mentioned in the question.
Hope this helps!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.