In: Computer Science
Problem: Your friend is an intern at the local Department of Health and needs to prepare a report about the recent activity of the influenza virus. She has recorded the number of cases of each type of flu (A, B, and C) over the last several weeks. Write a C++ program that will calculate the total cases for each week, determine the level of activity for the week (low, moderate, or widespread) and print a report to a file, including a small bar chart for the weekly cases.
Input: The user should be prompted to input the total number of weeks, and then for each week the number of cases of type A, B, and C flu reported.
Processing: For each week, sum up the total number of flu cases. Also sum up the total number of cases of flu reported over the entire period.
Output: The program should output a report to a file named “flu_report.txt”. The first row of the report should be a list of column headers that label the data to be output. This should be followed by a row for each week that lists the week number and the number of total cases of flu. This should be followed by the activity rating: “Low” if the total cases are less than than 500. Otherwise “Moderate” if the cases are less than 2000, and “Widespread” if the total cases are 2000 or more. This should be followed by an asterisk for each 250 cases of the total weekly cases. After the table is complete, the program should output the total number of flu cases for the entire period. See a sample below:
Week Total Cases Activity Chart
1 333 Low |*
2 1079 Moderate |****
3 2497 Widespread |*********
4 1830 Moderate |*******
------------------------------------------------------------------
The total number of cases reported: 5739
I have provided the proper commented code with proper screenshots so that you may indent it properly.
I hope that you find the answer helpful.
CODE:
-------------------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int main(){
// Defining Variables
int no_of_weeks;
int total_cases = 0;
//Declaring Vector of Pair of Integer and string
std::vector<pair<int,string>> data;
// Taking Input for the Number of Weeks
cout<<"Enter No. of Weeks\n";
cin >> no_of_weeks;
// Running the Loop for no_of_weeks times
for(int i = 0; i < no_of_weeks ; i++){
int A,B,C;
// Taking Input for different types
of flus
cout<<"Enter No. of Cases of
Flu A, B, C for week" << i + 1 << " seperated by space
: \n";
cin >> A >> B
>>C;
// Adding all the cases in a
week
int cases_in_a_week = A + B +
C;
// Updating total cases
total_cases +=
cases_in_a_week;
// Declaring the level
variable
string level;
// Updating the level of the week
corresponding to each case
if(cases_in_a_week < 500) level
= "Low";
else if(cases_in_a_week >= 500
&& cases_in_a_week < 2000) level = "Moderate";
else level = "Widespread";
// Storing the Week's information
by using a vector of pairs
// in which pair's first is the
number of cases which is of type int
// while the second is the level of
the flu which is of the type string
data.push_back(make_pair(cases_in_a_week,level));
}
// Linking the stdoutput to the flu_report.txt
file
// this also creates the file with the same name if it
doesn't exists
freopen("flu_report.txt", "w", stdout);
// Printing the respective output data with Bar Chart
of stars for each level
for(int i = 0;i < no_of_weeks ; i++){
//printing the week no. and number
of cases
cout<<i+1<<"
"<<data[i].first<<" "<<data[i].second<<"
|";
//calculating the number of
stars
int stars =
data[i].first/250;
//printing the stars of the bar
chart
for(int j = 0; j < stars ; j++)
cout<<"*";
cout<<endl;
}
//printing the total number of cases
cout<<total_cases;
}
-------------------------------------------------------------------------------------------------------------
Screenshots:
-------------------------------------------------------------------------------------------------------------
Code:
Compilation and Running:
You can Compile the code using command "g++ filename.cpp" without quotes and run the same using "./a.out"
Output:
I hope you find the answer useful.
Happy Coding !