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
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
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:
Get Answers For Free
Most questions answered within 1 hours.