Here is the C++ code . Please fill in the TODO comment with a valid C++ code. th
ID: 3882401 • Letter: H
Question
Here is the C++ code . Please fill in the TODO comment with a valid C++ code. this is the expected output for the code .
Total rainfall: 36.2 inches Month Average Rainfall (in) Jan...................0.030 Feb...................0.012 Mar...................0.095 Apr...................0.083 May...................0.146 Jun...................0.147 Jul...................0.236 Aug...................0.082 Sep...................0.130 Oct...................0.123 Nov...................0.077 Dec...................0.020 Month with greatest rainfall: Jul Month with least rainfall: Feb
#include <cmath> #include <cstdlib> #include <fstream> #include <iomanip> #include <iostream> #include <limits> const char* INPUT_FILE_NAME{"data.txt"}; const size_t NUM_MONTHS{12}; const size_t MAX_NUM_DAYS{31}; /* * The following acts as a sentinel value to fill in columns of data * to maintain a rectangular two-dimensional array. For example, for * months that have only 30 days, the rainfall for the 31st day (i.e., * column) is marked with a -1.0 in the input data file. Similarly for * February, in which there are only 29 days (the data file contains * data from 2016 which was a leap year), there are -1.0 values placed * in the 30th and 31st days (i.e., columns). */ const double INVALID_DATA{-1.0}; enum month_t{JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC, UNKNOWN}; /** * Get data from the supplied data file. The supplied data file contains * precipitation data for Springfield in the year 2016. * @param data a two-dimensional array that gets initialized with data */ void getData(double data[][MAX_NUM_DAYS]); /** * A helper function that returns the enum following the given enum which * is used to facilitate for-loops iterating over month_t instances. * @param month a particular month enum * @return The month that follows the given month is returned, unless the * given month is DEC, in which UNKNOWN is returned. */ month_t getNext(month_t month); /** * Calculate the total rainfall stored in the given array. * @param data a two-dimensional array containing rainfall information * @return The total amount of rainfall for the year stored in the given * array is returned. */ double totalRainfall(double data[][MAX_NUM_DAYS]); /** * Calculate the average rainfall for a given month. * @param data a two-dimensional array containing rainfall information * @param month the month for which we wish to calculate average rainfall * @return The average rainfall for the given month is returned. */ double averageForMonth(double data[][MAX_NUM_DAYS], size_t month); /** * Determine the month with the most rainfall. * @param data a two-dimensional array containing rainfall information * @return The month that has the most rainfall is returned. */ month_t monthWithMostRainfall(double data[][MAX_NUM_DAYS]); /** * Determine the month with the least rainfall. * @param data a two-dimensional array containing rainfall information * @return The month that has the leasr rainfall is returned. */ month_t monthWithLeastRainfall(double data[][MAX_NUM_DAYS]); /** * A utility function that displays various statistics surrounding the * data maintained in the given two-dimensional array. * @param data a two-dimensional array containing rainfall information */ void displayStatistics(double data[][MAX_NUM_DAYS]); /** * @brief Entry point for this application. * * @return 0 is returned upon successful execution. */ int main() { double stats[NUM_MONTHS][MAX_NUM_DAYS]; getData(stats); displayStatistics(stats); return EXIT_SUCCESS; } // end program void getData(double data[][MAX_NUM_DAYS]) { std::ifstream input{INPUT_FILE_NAME}; if (input.is_open()) { for (month_t month{JAN}; month <= DEC; month = getNext(month)) { for (size_t day{0}; day < MAX_NUM_DAYS; ++day) { input >> data[month][day]; } } input.close(); } else { std::cout << "Unable to open input file" << std::endl; } } month_t getNext(month_t month) { switch(month) { case JAN: return FEB; case FEB: return MAR; case MAR: return APR; case APR: return MAY; case MAY: return JUN; case JUN: return JUL; case JUL: return AUG; case AUG: return SEP; case SEP: return OCT; case OCT: return NOV; case NOV: return DEC; case DEC: return UNKNOWN; default: return UNKNOWN; } } double totalRainfall(double data[][MAX_NUM_DAYS]) { // TODO: Implement me. Once implemented, commit your changes. return 0; } double averageForMonth(double data[][MAX_NUM_DAYS], size_t month) { // TODO: Implement me. Once implemented, commit your changes. return 0; } month_t monthWithMostRainfall(double data[][MAX_NUM_DAYS]) { // TODO: Implement me. Once implemented, commit your changes. return JAN; } month_t monthWithLeastRainfall(double data[][MAX_NUM_DAYS]) { // TODO: Implement me. Once implemented, commit your changes. return JAN; } void displayStatistics(double data[][MAX_NUM_DAYS]) { // TODO: Implement me. Once implemented, commit your changes.
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
const int MONTHS = 12;
string name[MONTHS]= {"January","February","March","April","May","June","July","August","September","October","November","December"};
int count= 0;
double rain[MONTHS];
double avg;
double year=0;
double highest;
string highMonth;
double lowest;
string lowMonth;
for(count = 0; count < MONTHS; count++)
{
cout <<"How many inches of rain does "<< name[count];
cout<< " have? ";
cin >> rain[count];
while (rain[count] < 0)
{
cout << "Please enter a number greater than 0."<< endl;
cin >> rain[count];
}
}
for(int count=0; count<MONTHS; count++)
year += rain[count];
avg = year / MONTHS;
for( int count = 0; count < MONTHS; count++)
{
cout << name[count];
cout<< " has ";
cout<< rain[count] << " inches of rainfall. ";
}
highest = rain[0];
for (count = 1 ;count < MONTHS; count++)
{
if (rain[count] > highest)
{
highMonth = name[count];
highest = rain[count];
}
}
lowest = rain[0];
for (count = 1 ;count < MONTHS; count++)
{
if (rain[count] < lowest)
{
lowMonth = name[count];
lowest = rain[count];
}
}
cout << endl;
cout << setprecision(2) << fixed;
cout <<"Total Rainfall throughout the year is " <<year << " inches" << endl;
cout <<"The average monthly rainfall is " << avg << " inches"<< endl;
cout <<"The month with the highest amount of rainfall is " << highMonth << " with " << highest<< " inches."<< endl;
cout <<"The month with the lowest amount of rainfall is " << lowMonth << " with " << lowest<< " inches."<< endl;
return 0;
}