Question

C++ COVID-19 has hit US hard. All states and US territories are reporting total cases, new...

C++

COVID-19 has hit US hard. All states and US territories are reporting total cases, new cases, total deaths, new deaths, and active cases. Create a program that will

  • read in all states and US territories from a text file into an array using a function
  • read each state’s data (a 55 by 4 array) from an example text file (see below) into a two-dimensional array using a function.
  • calculate grand total of total cases, new cases, total deaths, and new deaths for all states using a function (or several functions).
  • display each state’s statistical data and output to an output text file (I am not interested in your files, I am just interested in the programming part).

Example text file includes:

130689  7671    4758    599     112565
41090   3585    1003    86      39995
15718   0       617     0       15032
15240   203     351     4       13989
14867   1857    512     35      14305
13324   974     236     15      12988
12980   1470    162     12      12742
12500   0       231     0       12259
11256   0       274     0       10932
7984    0       338     0       7022
7314    572     229     10      7054
7276    231     140     7       6461
5675    0       189     0       5436
4950    0       140     0       4770
4944    533     139     12      4791
4450    407     142     23      4308
4045    436     91      24      3770
3633    0       44      0       3294
2901    238     42      4       2773
2878    241     54      3       2822
2456    187     65      1       2371
2440    173     77      9       2361
2367    0       52      3       2303
2049    0       44      0       2005
1953    117     46      0       1871
1927    86      47      2       1860
1738    100     51      8       1687
1605    0       8       0       1586
1327    75      51      5       893
1101    0       10      0       1091
1097    99      24      2       815
1068    0       27      0       1041
986     51      30      1       486
955     0       45      0       604
946     78      25      3       853
922     0       25      0       887
854     17      16      0       741
845     98      25      3       820
673     0       14      0       588
669     0       9       0       513
624     0       12      0       558
543     31      23      1       520
499     29      10      0       331
409     46      8       0       401
371     0       4       0       282
345     21      4       1       341
299     1       6       0       261
288     48      4       0       193
225     18      3       0       148
210     10      0       0       158
185     0       6       0       164
112     0       4       0       85
8       0       1       0       7
513     38      21      1       488
42      0       1       0       7

Homework Answers

Answer #1

Code:

Code as text:

#include <iostream>
#include <fstream>

using namespace std;
#define ROW 55
#define COL 4

// function to read data from a text file into an array
void read_data(int data[][COL]) {
// ask for file name and open
ifstream file;
string filename;
cout << "Enter filename: ";
cin >> filename;
file.open(filename);

// print error if file not found
if (!file) {
cout << "Error. File not found.\n";
exit(1);
}

// read data of each state form file into the 2d array
int total_case, new_case, total_death, new_death, active_cases;
int i = 0;
while (file >> total_case >> new_case >> total_death >> new_death >> active_cases) {
data[i][0] = total_case;
data[i][1] = new_case;
data[i][2] = total_death;
data[i][3] = new_death;
i++;
}
// close the file
file.close();
}

// function to calculate the grand total of data
void calculate_total(int data[][COL]) {
int grand_total_cases = 0, grand_new_cases = 0, grand_total_deaths = 0, grand_new_deaths = 0;

// loop for each state and calculate total
for (int i = 0; i < ROW; i++) {
grand_total_cases += data[i][0];
grand_new_cases += data[i][1];
grand_total_deaths += data[i][2];
grand_new_deaths += data[i][3];
}
// open output file
ofstream file("output.txt");

// print grand total and also write to file
cout << "Grand total of total cases: " << grand_total_cases << endl;
file << "Grand total of total cases: " << grand_total_cases << endl;
cout << "Grand total of new cases: " << grand_new_cases << endl;
file << "Grand total of new cases: " << grand_new_cases << endl;
cout << "Grand total of total deaths: " << grand_total_deaths << endl;
file << "Grand total of total deaths: " << grand_total_deaths << endl;
cout << "Grand total of new deathnew deaths: " << grand_new_deaths << endl;
file << "Grand total of new deathnew deaths: " << grand_new_deaths << endl;
// close the file
file.close();
cout << "Data written to file: output.txt" << endl;
}
  
int main() {
// declare 2d array to store data
int data[ROW][COL];
// call function to read data from file
read_data(data);
// call function to calculate the total
calculate_total(data);
return 0;
}

Sample run:

data.txt file:

Run program:

Output file:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a...
And need to be writing in C++ language Programm need to start with   #include<fstream> Prepare a text file data_in.txt with the following information (highlight the piece of text below with numbers and copy it to a text file): 54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61, 8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22, 69, 77, 86, 35, 19, 32, 49, 15,...
A new industrial oven has just been installed at Piatt Bakery. To develop experience regarding the...
A new industrial oven has just been installed at Piatt Bakery. To develop experience regarding the oven temperature, an inspector reads the temperature at four different places inside the oven each half hour starting at 8:00 a.m. The last reading was at 10:30 a.m., for a total of six samples. The first reading, taken at 8:00 a.m., was 351 degrees Fahrenheit. (Only the last two digits are given in the following table to make the computations easier.) Reading 1 Reading...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into...
Question 2: Write a C program that read 100 integers from the attached file (integers.txt) into an array and copy the integers from the array into a Binary Search Tree (BST). The program prints out the following: The number of comparisons made to search for a given integer in the BST And The number of comparisons made to search for the same integer in the array Question 3 Run the program developed in Question 2 ten times. The given values...
State Total Murders1 Total Firearms Handguns Rifles Shotguns Firearms (Type Unknown) Knives or Cutting Instruments Other...
State Total Murders1 Total Firearms Handguns Rifles Shotguns Firearms (Type Unknown) Knives or Cutting Instruments Other Weapons Hands, Fists, Feet, and so on2 Alabama 3 3 1 0 1 1 0 0 0 Alaska 57 39 12 2 1 24 7 8 3 Arizona 278 171 128 4 3 36 42 55 10 Arkansas 164 110 51 10 4 45 18 30 6 California 1,861 1,275 855 34 33 353 263 233 90 Colorado 176 115 65 12 6 32...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Attention:- Your code should follow all the below instruction. Specification The file has entries like the...
Attention:- Your code should follow all the below instruction. Specification The file has entries like the following Barnstable,Barnstable,1 Bourne,Barnstable,5 Brewster,Barnstable,9 ... This code should create a dictionary where the county is the key and then total number of cases for the country is the value. The code should print the name of the county with the highest number of cases along with the total cases. The code must only use below 3 functions open_file_read cases_dictionary_create highest_cases This code must read...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on...
C Programming. Do not use a function in the solution. Assignment 6 This assignment focuses on using arrays. Instructions Create a program called arrays.c that does the following: 1. Prompt the user for a string (<= 100 characters). (using the attached file: AS06Data.txt) AS06Data.txt file contains the text " Instructions: When your executing program displays "Enter string 1:" starting with T, select the line of text below, copy with ctrl-c, then type ctrl-v THIS IS a test of string 0123456789...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing...
USING C++ The purpose of this assignment is the use of 2-dimensional arrays, reading and writing text files, writing functions, and program planning and development. You will read a data file and store all of the input data in a two dimensional array. You will perform calculations on the data and store the results in the 2 dimensional array. You will sort the array and print the results in a report. Instructions You will read in the same input file...
I need the actual code for this... I found a similar response but it was not...
I need the actual code for this... I found a similar response but it was not thorough enough. Problem Prerequisites: None Suppose that a scientist is doing some important research work that requires her to use rabbits in her experiments. She starts out with one adult male rabbit and one adult female rabbit. At the end of each month, a pair of adult rabbits produces one pair of offspring, a male and a female. These new offspring will take one...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT