The data consists of energy consumption for various types of fuel for each state
ID: 3730621 • Letter: T
Question
The data consists of energy consumption for various types of fuel for each state and Washington DC (DC) expressed in trillions of BTU’s. We will read in the data per state and compute a total energy consumption for five categories as follows:
1. coal
2. natural gas
3. petroleum: Fuel Oil + Jet Fuel + LPG + Motor Gasoline + Residual Fuel Oil
4. nuclear
5. renewables: Hydroelectric Power + Wood and Waste + Fuel Ethanol + Geothermal + Solar + Wind
Part 1: Data Analysis - a Command-Line State Energy Consumption Query: Create a command line menu driven application to display a state’s total energy consumption for coal, natural gas, petroleum and renewables. The first part of the assignment will be to create a program named ’A03.cpp’ that will read in a data file called Data2015.csv using C++ file stream operators. The data is stored in a commaseparated values (csv) file. To correctly read the data into an array, the commas, spaces, newlines (whitespace) and header row (first row) must be ignored. File I/O can be performed using ifstream and iostream located in the #include standard library.
Data Processing
The data will be read into the program using a function called: ReadData(. . . ) with the following formal parameters: an ifstream &, a data structure (e.g. an array-of-structs, etc.) and the number of states (51 in our dataset that includes Washington D.C.). The ReadData(. . . ) function will compute the state energy consumption totals for each group (coal, natural gas, petroleum, nuclear, renewables) as described above and store the result in an array or other data structure used in the application.
Database Interface
After the data is loaded and processed, a command-driven interface will allow a user to enter a state name. The interface will query the preprocessed data and display the state’s energy consumption as a percentage of that state’s total energy consumption. For example, California consumes 31 trillion BTU’s of coal annually with a total energy consumption of 6506 trillion BTU’s. The state’s total was computed by adding up the five energy groups. The percentage of a given energy is then computed by: 31./6506. 100 = 0.35%. Note that the number of digits printed must be controlled with the stream formatting options.
An example of the output and user interaction using the database: Enter a statename or ’q’ to exit:
California Coal: 0.48%
Natural Gas: 36.61%
Petroleum: 46.95%
Nuclear: 2.97%
Renewables: 12.99%
Enter a statename or ’q’ to exit:
New York Coal: 1.16%
Natural Gas: 39.43%
Petroleum: 34.79%
Nuclear: 13.16%
Renewables: 11.45%
Enter a statename or ’q’ to exit:
Wyoming Coal: 59.90%
Natural Gas: 18.07%
Petroleum: 16.13%
Nuclear: 0.00%
Renewables: 5.90%
Enter a statename or ’q’ to exit:
Colorado Coal: 24.52%
Natural Gas: 35.52%
Petroleum: 31.22%
Nuclear: 0.00%
Renewables: 8.75%
Enter a statename or ’q’ to exit:
Montana Coal: 35.98%
Natural Gas: 15.05%
Petroleum: 24.81%
Nuclear: 0.00%
Renewables: 24.16%
Enter a statename or ’q’ to exit: q Goodbye
Explanation / Answer
#include<iostream>
#include <fstream>
#include<string>
#include<vector>
#include <cstring>
#include<algorithm>
using namespace std;
struct Entry
{
string state;
double Coal;
double NaturalGas;
double Petroleum;
double Nuclear;
double Renewable;
double Total;
};
void readData(ifstream &file, vector<Entry>& DataList, int NumEntry);
int main()
{
std::ifstream file("data.csv");
vector<Entry> DataList;
readData(file,DataList,51);
string input;
while(true)
{
cout << "Enter a statename or ’q’ to exit: ";
cin >> input;
if(input == "q") break;
for(int i=0;i<DataList.size();i++)
{
Entry entry = DataList[i];
if(entry.state == input)
{
cout << "Coal : " << entry.Coal *100.0/entry.Total << "% ";
cout << "NaturalGas : " << entry.NaturalGas *100.0/entry.Total << "% ";
cout << "Petroleum : " << entry.Petroleum *100.0/entry.Total << "% ";
cout << "Nuclear : " << entry.Nuclear *100.0/entry.Total << "% ";
cout << "Renewable : " << entry.Renewable *100.0/entry.Total << "% " ;
break;
}
}
}
cout << "GoodBye";
return 0;
}
void readData(ifstream &file, vector<Entry>& DataList, int NumEntry)
{
std::string line = "";
int lineNum =0;
while (getline(file, line))
{
lineNum++;
if(lineNum == 1) continue;
if(lineNum-1 > NumEntry ) break;
std::vector<std::string> vec;
for (char *p = strtok((char*)line.c_str(), ", "); p != NULL; p = strtok(NULL, ", "))
{
vec.push_back( string(p) );
}
Entry entry;
entry.state = vec[0];
entry.Coal = atof(vec[1].c_str());
entry.NaturalGas = atof(vec[2].c_str());
entry.Petroleum = atof(vec[3].c_str());
entry.Nuclear = atof(vec[4].c_str());
entry.Renewable = atof(vec[5].c_str());
entry.Total = entry.Coal + entry.NaturalGas +entry.Petroleum +entry.Nuclear + entry.Renewable ;
DataList.push_back(entry);
}
file.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.