QuestionQuestion

Write a program to explore state/county economic data

Write a 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:
```
./explore_econdata test_data.txt
```
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:
```
n_states
state_name state_07_unemployed state_15_unemployed state_med_income n_counties
county_1_name county_1_07_unemployed county_1_15_unemployed county_1_med_income
...
county_n_name county_n_07_unemployed county_n_15_unemployed county_n_med_income
state_name state_07_unemployed state_15_unemployed state_med_income n_counties
...
```
For example, here's what a very small data file might look like:
```
2
Oregon 5.2 5.7 51088 3
Benton 4.1 4.3 54089
Linn 6.3 6.9 44358
Multnomah 4.9 5.0 53519
California 5.4 6.2 61927 2
Alameda 4.7 4.7 76996
San_Francisco 4.2 3.6 83788
```
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:

```
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;
};
```

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.
* 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.

For the sorting functionality, consider using C++'s built-in `sort()` function.

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.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

#include "econdata.hpp"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>   
#include <vector>
#include <iomanip> // for width
using namespace std;

/**
* Allocates an array of a specified number of states.
* @param size number of states
* @return an array of states
*/
struct state* allocate_states(int size) {
    struct state* sts = new state[size];

    return sts;
}

/**
* Reads data for a specified number of states from an input file stream
* into a given (pre-allocated) array.
* @param sts array of state
* @param sizer number of states in the array
* @param in input stream
*/
void read_state_data(struct state* sts, int size, std::ifstream& in) {
    int n_counties;

    for (int i = 0; i < size; i++) {
       // read state information
       in >> sts[i].name >> sts[i].unemployed_2007
                >> sts[i].unemployed_2015 >> sts[i].med_income
                >> n_counties;
       // set up number of counties
       sts[i].n_counties = n_counties;
       // allocate memory to the array of counites
       sts[i].counties = allocate_counties(n_counties);
      
       // read all counties
       read_county_data(sts[i].counties, n_counties, in);
    }
}

/**
* Allocates an array of a specified number of counties.
* @param size number of counties
* @return an array of counties
*/
struct county* allocate_counties(int size) {
    struct county * cts = new county[size];

    return cts;
}...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

50% discount

Hours
Minutes
Seconds
$68.00 $34.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available C-Family Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats