The Weather Service Bureau department has data representing monthly rainfall for
ID: 3861082 • Letter: T
Question
The Weather Service Bureau department has data representing monthly rainfall for a year and we would like to create a table categorizing each month as rainy (rainfall 20% higher than average) dry (rainfall 25% lower than average) or average. The data file for monthly rain fall is called rainfall.txt. You need to down the file rainfall.txt and store it in the same folder of your lab5.cpp.
Output
The year's average monthly rainfall was 139 mm.
September has the highest rainfall (190 mm).
January has the lowes rainfall (95 mm)
Month Rainfall(mm) Classification
------- --------------- --------------
1 95 Dry
2 100 Dry
3 120 Average
4 130 Average
5 135 Average
6 145 Average
7 155 Average
8 185 Rainy
9 190 Rainy
10 160 Average
11 130 Average
12 120 Average
Program Requirements:
Implement the following functions in the program:
void inputRainfall(int rainFal l[], int size)
The function reads the monthly rainfall from the file rainFall.txt and stores them in the array rainFall
int calculateAverageRainFall(int rainFall [], int size)
Return the average monthly rainfall
void classifyAndDisplayRainfall(int rainFall[], int months);
Classify and display each month as average, rainy, or dry.
Hints on lab assignment 5
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
const int NUM_MONTHS=12;
const double RAIN_RATE=0.20;//20% more rain than average
const double DRY_RATE = 0.25;
void inputRainFall(int [], int);
int calculateAverageRainFall(int [], int );
void classifyAndDisplayRainfall(int [],int);
int main()
{int rainFall[NUM_MONTHS];
//Read rainfall from the the file and the fill them in the array
//Calculate the average rainfall by calling the function calculateAverageRainFall and display the average rainfall
//Classify months as Dry, Average, or Rainy and display the result by calling the function classifyAndDisplayRainFall
}
Define the function inputRainFall
void inputRainFall(int rainFall[], int size)
{//See page on 277 on how to read numeric data from the file.
//The rainfall read from the file rainfall.txt and store them in the array rainFall.
}
int calculateAverageRainFall(int rainFall [], int size)
{
Note: Use the function ceil (double x) to round a number up. It returns the smallest integer greater than or equal to x.
return ceil(float(sum / 12));
}
void classifyAndDisplayRainfall(int rainFall[], int months)
{
//Use a one-dimensional array month to store string month
string month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//Call the function calculateAverageRainFall to get rainfall average
double dryCut = averageRain - DRY_RATE * averageRain;
double rainCut = averageRain + RAIN_RATE * averageRain;
//Classify months as Dry, Average, or Rainy and display the result
//Use setw to align columns
}//end
Explanation / Answer
#include<fstream>
#include<iostream>
#include<iomanip>
#include<string>
#include <cmath>
using namespace std; //adds prefixes
void inputRainfall(int rainFall[], int size);
int calculateAverageRainFall(int rainFall[]);
void classifyAndDisplayRainfall(int rainFall[], int months);
int main() {
const int SIZE = 12;
int rainFall[SIZE];
inputRainfall(rainFall, SIZE);
classifyAndDisplayRainfall(rainFall, SIZE);
system("pause");
return 0;
}
//The function reads the monthly rainfall from the file rainFall.txt and stores them int the array rainFall
void inputRainfall(int rainFall[], int size) {
//Open the file
ifstream inputFile;
inputFile.open("rainfall.txt");
//Initialize month counter
int month = 0; //first month
//Read the monthly rainfall in the file
while (month < 12) {
inputFile >> rainFall[month];
month++;
}
}
//Return the average monthly rainfall (rounded to the nearest millimeter)
int calculateAverageRainFall(int rainFall[]) {
int sum = 0;
float average = 0;
for (int i = 0; i < 12; i++) {
sum = sum + rainFall[i];
}
average = (double)sum / 12;
int avg = round(average);
return avg;
}
//Display the average monthly rainfall, the month with the highest rainfall, and the month with the lowest rainfall
void classifyAndDisplayRainfall(int rainFall[], int months) {
int average = calculateAverageRainFall(rainFall);
string month = "";
string month2 = "";
int count = 0;
int count2 = 0;
int below = average - (average*.25);
int above = average + (average*.20);
int max = rainFall[0];
int min = rainFall[0];
for (int i = 0; i < months; i++) {
if (max < rainFall[i]) {
max = rainFall[i];
count = i;
}
}
for (int i = 0; i < months; i++) {
if (min > rainFall[i]) {
min = rainFall[i];
count2 = i;
}
}
if (count == 0) {
month = "January";
}
else if (count == 1) {
month = "February";
}
else if (count == 2) {
month = "March";
}
else if (count == 3) {
month = "April";
}
else if (count == 4) {
month = "May";
}
else if (count == 5) {
month = "June";
}
else if (count == 6) {
month = "July";
}
else if (count == 7) {
month = "August";
}
else if (count == 8) {
month = "September";
}
else if (count == 9) {
month = "October";
}
else if (count == 10) {
month = "November";
}
else if (count == 11) {
month = "December";
}
if (count2 == 0) {
month2 = "January";
}
else if (count2 == 1) {
month2 = "February";
}
else if (count2 == 2) {
month2 = "March";
}
else if (count2 == 3) {
month2 = "April";
}
else if (count2 == 4) {
month2 = "May";
}
else if (count2 == 5) {
month2 = "June";
}
else if (count2 == 6) {
month2 = "July";
}
else if (count2 == 7) {
month2 = "August";
}
else if (count2 == 8) {
month2 = "September";
}
else if (count2 == 9) {
month2 = "October";
}
else if (count2 == 10) {
month2 = "November";
}
else if (count2 == 11) {
month2 = "December";
}
cout << "The year's average monthly rainfall was " << average << " mm"<< endl;
cout << month<< " has the highest rainfall " << max << " mm" << endl;
cout << month2 << " has the lowes rainfall " << min << " mm" << endl;
cout << "" << endl;
int counter = 1;
cout << "Month Rainfall(mm) Classification" << endl;
cout << "----- ----------- --------------" << endl;
for (int i = 0; i < months; i++) {
if (rainFall[i] >= above) {
cout << setprecision(3)<<fixed<< setw(3) << counter << setw(16) << rainFall[i] << setw(17) << "Rainy" << endl;
}
else if (rainFall[i] <= below) {
cout << setw(3) << counter << setw(16) << rainFall[i] << setw(17) << "Dry" << endl;
}
else {
cout << setw(3) << counter << setw(16) << rainFall[i] << setw(17) << "Average" << endl;
}
counter++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.