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,...
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...
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...
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...
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...
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...
Historically, the MBA program at Whatsamattu U. has about 40% of their students choose a Leadership...
Historically, the MBA program at Whatsamattu U. has about 40% of their students choose a Leadership major, 30% choose a Finance major, 20% choose a Marketing major, and 10% choose no major. Does the most recent class of 200 MBA students fit that same pattern or has there been a shift in the choice of majors. Using the sample of 200 students (in the data file), conduct a Chi Square Goodness of Fit test to determine if the current distribution...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
For some reason I followed the steps in my project and I am getting the incorrect...
For some reason I followed the steps in my project and I am getting the incorrect output and when I am submitting it, it gives me compilation error. Printing empty array -- next line should be blank Testing append: Shouldn't crash! Should print 100 through 110 below, with 110 on a new line: 100 101 102 103 104 105 106 107 108 109 110 Checking capacity of new array: OK Append test #2: Should print 100 through 120 below, on...
While job opportunities for men and women are considerably more balanced than they were 40 years...
While job opportunities for men and women are considerably more balanced than they were 40 years ago, the career aspirations may still differ. Is there a difference in majors chosen by men and women? Using the sample of 200 MBA students (in the data file), conduct a Chi Square Test of Independence to determine if one's choice of major is independent of their gender. Use a .05 significance level. ID Gender Major Employ Age MBA_GPA BS GPA Hrs_Studying Works FT...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT