Rainfall Statistics (20 points) Write a C++ program that reads from a manually c
ID: 3688878 • Letter: R
Question
Rainfall Statistics (20 points)
Write a C++ program that reads from a manually created file “Rainfall.txt” (You can manually create this file first and fill it with any twelve double values written one below the other; so that reading them through code becomes easy) the total rainfall for each of 12 months into an array of doubles. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.
Create an array of strings nameMonth in main() that holds the twelve month names: “January”, “February”, … ,”December”.
Create a function fillArray() that takes another parallel twelve element array rainMonth as its argument and reads a file named “Rainfall.txt” to fill this array with double values. This function is called from main().
Create a function display() that takes the array rainMonth from main() and prints
o Rainfall for each month(display values with a ‘;’ in-between)
o Total rainfall for the year
o Average monthly rainfall (two digits after decimal point)
Create a function min_max() that takes the arrays nameMonth and rainMonth as arguments from main() and displays the months with the Highest and Lowest amounts.
Do NOT use STL vector in your program.
Example output:
> Rainfall for each month: 1.01; 1.50; 3.09; 2.23; 2.10; 1.01; 1.10; 0.50; 0.02; 0.03; 0.09; 0.90
> Total rainfall: 13.58
> Average monthly rainfall: 1.13
> Highest Rainfall recorded is 3.09 inches in March.
> Lowest Rainfall recorded is 0.02 inches in September
Explanation / Answer
main.cpp
#include<iostream>
#include <fstream>
using namespace std;
//function prototype
void fillArray(double [], int);
void display(double[], int);
void min_max(double[], string[], int);
int main(){
int array_size = 12;
// create an array to store month names
string nameMonth[] ={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
//create an array to store rainfall data
double rainMonth[array_size];
fillArray(rainMonth, array_size); // call fillArray Function
display(rainMonth, array_size); // call display Function
min_max(rainMonth, nameMonth, array_size); // call min_max Function
return 0;
};
void fillArray(double rainMonth[], int array_size ){
ifstream inputFile;
double num;
//open file
inputFile.open("rainfall.txt");
if(!inputFile){
cout << "Error opening file! "; // Test open file erros
}
else{
// Read the file
int count = 0;
while (inputFile >> num) {
rainMonth[count] = num; //store the data into rainMonth
count++;
}
}
}
void display(double rainMonth[], int array_size){
double sum = 0;
double average = 0;
// print out railfall for each month
cout<<"Rainfall for each month: ";
for (int i = 0; i< array_size; i++) {
cout<<rainMonth[i]<<"; ";
//total rainfall for a year
sum += rainMonth[i];
//average rainfall for a year
average = sum / array_size;
}
cout<<endl;
cout<<"Total rainfall for the year: "<<sum<<endl;
cout<<"Average monthly rainfall: "<<average<<endl;
}
void min_max(double rainMonth[], string nameMonth[], int array_size){
double highRf = 0.0, lowRf = 0.0;
string highmth, lowmth;
//find highest value in array
for (int i = 0; i<array_size; i++) {
highRf = rainMonth[0];
if(rainMonth[i] > highRf)
{
highRf = rainMonth[i];
highmth = nameMonth[i];
}
//find lowest value in array
lowRf = rainMonth[0];
if (rainMonth[i] < lowRf) {
lowRf = rainMonth[i];
lowmth = nameMonth[i];
}
}
//display results
cout << "Highest Rainfall recorded is " << highRf <<" inches in month of " <<highmth<<endl;
cout << "Lowest Rainfall recorded is " << lowRf <<" inches in month of " <<lowmth;
}
rainfall.txt
20.1
23.4
33.2
44.5
11.2
10.3
15.4
16.2
18.4
11.3
22.4
32.8
sample output
Rainfall for each month: 20.1; 23.4; 33.2; 44.5; 11.2; 10.3; 15.4; 16.2; 18.4; 11.3; 22.4; 32.8;
Total rainfall for the year: 259.2
Average monthly rainfall: 21.6
Highest Rainfall recorded is 32.8 inches in month of Dec
Lowest Rainfall recorded is 20.1 inches in month of Octsh-4.3$
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.