Question

In: Computer Science

Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...

Program Requirements:

Write a C++ program according to the following requirements:

1.   Open the data file Electricity.txt and read each column into an array (8 arrays total).

2.   Also create 2 arrays for the following:

  • Total Fossil Fuel Energy (sum of all fossil fuels)
  • Total Renewable Energy (sum of all renewable sources)

Electricity.txt:

Net generation United States all sectors monthly
https://www.eia.gov/electricity/data/browser/
Source: U.S. Energy Information Administration
All values in thousands of megawatthours
Year   all fuels   coal       natural gas   nuclear       hydroelectric   wind       solar
2018   347576.0   95496.8       122393.9   67257.0       24377.0       22720.8       7780.4
2017   336189.2   100486.3   108034.6   67079.1       25027.8       21191.9       6439.7
2016   339722.9   103262.4   114858.9   67141.2       22317.7       18916.0       4572.2
2015   339800.1   112699.8   111123.5   66431.5       20756.7       15893.2       3252.7
2014   341133.8   131809.2   93884.1       66430.5       21613.9       15137.9       2410.3
2013   338830.3   131759.6   93736.3       65751.4       22380.4       13986.6  
2012   337313.8   126170.2   102157.8   64110.9       23020.0       11735.1  
2011   341678.4   144452.5   84474.1       65850.4       26612.9       10014.7  
2010   343755.0   153940.9   82308.1       67247.4       21683.6       7887.7  
2009   329194.2   146325.4   76748.2       66571.2       22787.1       6157.2  
2008   343282.3   165483.4   73581.7       67184.0       21235.9       4613.6  
2007   346395.4   168038.0   74715.8       67202.1       20625.8       2870.8  
2006   338725.2   165875.9   68036.7       65601.6       24103.9       2215.8  
2005   337951.9   167739.4   63413.4       65165.5       22526.8       1484.2  
2004   330879.6   164858.4   59175.0       65710.7       22368.1       1178.6  
2003   323598.8   164478.1   54159.0       63644.4       22983.9       932.3  
2002   321537.7   161094.2   57583.8       65005.3       22027.4       862.9  
2001   311387.0   158663.0   53260.8       64068.9       18080.1       561.4

Solutions

Expert Solution

C++ CODE:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
    // Open file to read
    ifstream infile("Electricity.txt");
    // Validate if file has been opened or not
    if(!infile){
        cout << "Error: Cannot open file" << endl;
        return 1;
    }
    const int MAX = 18;
    // Create arrays to read each column from the file
    int year[MAX];
    float allFuels[MAX], coal[MAX], naturalGas[MAX], nuclear[MAX], hydroelectric[MAX], wind[MAX], solar[MAX], totalFossilFuel[MAX], totalRenewableFuel[MAX];
    // To read first 5 lines in the text file
    string line;
    // Read first 5 lines and discard them
    for(int i = 0; i < 5; i++)
        getline(infile, line);
    // Read each row's columns
    for(int i = 0; i < MAX; i++){
        infile >> year[i];
        infile >> allFuels[i];
        infile >> coal[i];// This is a fossil fuel
        infile >> naturalGas[i];// This is a fossil fuel
        infile >> nuclear[i];
        infile >> hydroelectric[i];
        infile >> wind[i];// This is a renewable fuel
        infile >> solar[i];// This is a renewable fuel
        totalFossilFuel[i] = coal[i] + naturalGas[i];
        totalRenewableFuel[i] = wind[i] + solar[i];
    }
    // Print array of total fossil fuel energy and renewable energy
    cout << left << setw(6) << "Year" << setw(30) << "Total Fossil Fuel Energy" << setw(25) << "Total Renewable Energy" << endl;
    for(int i = 0; i < MAX; i++){
        cout << left << setw(6) << year[i] << setw(30) << setprecision(1) << fixed << totalFossilFuel[i] << setw(25) << totalRenewableFuel[i] << endl;
    }
    return 0;
}

Electricity.txt:


OUTPUT:

FOR ANY HELP JUST DROP A COMMENT


Related Solutions

C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
In c++  write a program. Give the necessary statements to open a file and to confirm that...
In c++  write a program. Give the necessary statements to open a file and to confirm that the file has been successfully opened for writing.
Write a C program that will read different data types from the following file and store...
Write a C program that will read different data types from the following file and store it in the array of structures. Given file: (This file have more than 1000 lines of similar data): time latitude longitude depth mag magType nst gap dmin 2020-10-19T23:28:33.400Z 61.342 -147.3997 12.3 1.6 ml 12 84 0.00021 2020-10-19T23:26:49.460Z 38.838501 -122.82684 1.54 0.57 md 11 81 0.006757 2020-10-19T23:17:28.720Z 35.0501667 -117.6545 0.29 1.51 ml 17 77 0.1205 2020-10-19T22:47:44.770Z 38.187 -117.7385 10.8 1.5 ml 15 100.22 0.049 2020-10-19T22:42:26.224Z...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100...
Write a program that creates an output file named rand_nums.txt. Open the file and write 100 random integers between -50 and +50 (inclusive) to the file. Be sure to handle any file IO exceptions. Remember to close the file. Write a program that opens rand_nums.txt for input. Create two output files pos.txt and neg.txt. Read through the input file, one line at a time, converting each line into an integer (no exception handling, yet). If the number is positive, write...
Write a program that does the following in C++ 1 ) Write the following store data...
Write a program that does the following in C++ 1 ) Write the following store data to a file (should be in main) DC Tourism Expenses 100.20 Revenue 200.50 Maryland Tourism Expenses 150.33 Revenue 210.33 Virginia Tourism Expenses 140.00 Revenue 230.00 2 ) Print the following heading: (should be in heading function) Store name | Profit [Note: use setw to make sure all your columns line up properly] 3 ) Read the store data for one store (should be in...
Present a screenshot of the following Program to open up the created file in C++ language....
Present a screenshot of the following Program to open up the created file in C++ language. The list of random numbers will be below the instructions I have provided for you a file named Random.txt for use in this program. This file contains a long list of random numbers. You are to write a program that opens the file, reads all the numbers from the file and calculates the following: A. The number of numbers in the file B. The...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Language: c++ using visual basic Write a program to open a text file that you created,...
Language: c++ using visual basic Write a program to open a text file that you created, read the file into arrays, sort the data by price (low to high), by box number (low to high), search for a price of a specific box number and create a reorder report. The reorder report should alert purchasing to order boxes whose inventory falls below 100. Sort the reorder report from high to low. Inventory data to input. Box number Number boxes in...
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT