C++ Write a program that reads the total rainfall for each of 12 months into an
ID: 666405 • Letter: C
Question
C++
Write a program that reads the total rainfall for each of 12 months into an array of float from a file named rain.txt. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the highest amount of rainfall, and the lowest amount of rainfall. The file rain.txt contains the rainfall for each month in the year.
Program Requirements:
Implement the following functions in the program:
void readRainfall(float rainFall[])
The function reads the monthly rainfall from the file rain.txt and stores them in the array rainFall
void displayRainfall(float rainFall[], int months);
The months paramter is the number of months in a year. The function displays the output below based on the data in the file rain.txt.
Month Rainfall
1 12.00
2 5.00
3 6.00
4 2.00
5 7.00
6 8.00
7 10.00
8 13.00
9 6.00
10 5.00
11 0.00
12 1.00
float getTotalOfRainfall(float rainFall[], int months);
This function returns the total amount of rainfall. The months paramter is the number of months in a year.
float getAverageOfRainfall(float rainfall[], int months);
This function returns the average amount of rainfall. The months paramter is the number of months in a year.
int getHighestIndexOfRainfall(float rainFall[], int months);
This function returns the index of the array element that has the highest rainfall.
int getLowestIndexOfRainfall(float rainFall[], int months);
This function returns the index of the array element that has the lowest rainfall.
Output
Month Rainfall
1 12.00
2 5.00
3 6.00
4 2.00
5 7.00
6 8.00
7 10.00
8 13.00
9 6.00
10 5.00
11 0.00
12 1.00
The total rainfall is 75.00 inches.
The average amount of rainfall is 6.25 inches.
The largest amount of rainfall is 13.00 inches.
The smallest amount of rainfall is 0.00 inches.
Explanation / Answer
//Find the month with the highest rainfall
#include <iostream>
#include <iomanip>
#include <fstream> //Required for inputFile variable.
using namespace std
void getRainFall(float[], int); //Function that displays the rainfall from .txt file.
float getTotal(float[], int); //Function that gets the total rainfall.
float getAverage(float[], int); //Function that gets the average rainfall.
float getHighest(float[], int); //Function that gets the highest rainfall
float getLowest(float[], int); //Function that gets the lowest rainfall.
//Begin main function.
int main()
{
const int SIZE=12; //Array size.
float rainFall[SIZE]; //Rainfall variable with size declarator.
double sum=0; //Sum variable.
double avg=0; //Average variable.
double highest; //Highest rainfall variable.
double lowest; //Lowest rainfall variable.
int highstMnth=0; //Highest month variable, used for highest rainfall.
int lowstMnth=0; //Lowest month variable, used for lowest rainfall.
//Set up numeric output formatting.
cout << fixed << showpoint << setprecision(2);
getRainFall(rainFall, SIZE); //Calls getRainFall function.
sum=getTotal(rainFall, SIZE); //Calls getTotal function.
cout << "The total rainfall for the year is " << sum << " inches. ";
avg=getAverage(rainFall, SIZE); //Calls getAverage function.
cout << "The average rainfall for the year is " << avg << " inches. ";
highest=getHighest(rainFall, SIZE); //Calls getHighest function.
cout << "The largest amount of rainfall was " << highest
<< " inches in month " << highstMnth << endl;
lowest=getLowest(rainFall, SIZE); //Calls getLowest function.
cout << "The smallest amount of rainfall was " << lowest
<< " inches in month " << lowstMnth << endl;
return 0;
} //End main function.
//Begin getRainFall function.
void getRainFall(float rainFall[], int i)
{
const int SIZE=12;
ifstream inputFile; //Variable used to read data from "Data.txt"
inputFile.open("Rain.txt"); //Opens the file "Data.txt"
//Loop counter.
for(i=0; i<SIZE; i++)
//Input rainfall data from Rain.txt
inputFile >>rainFall[i];
} //End getRainFall function.
//Begin getTotal function.
float getTotal(float rainFall[], int size)
{
double sum=0;
const int SIZE=12;
for(size=0; size<SIZE; size++)
//Get the total rainfall for the year.
sum=sum+rainFall[size];
//Return the sum of total rainfall.
return sum;
} //End getTotal function.
//Begin getAverage function.
float getAverage(float rainFall[], int size)
{
const int SIZE=12;
double sum=0;
double avg=0; //Average variable.
for(size=0; size<SIZE; size++)
//First, calculate the total rainfall for the year
//before calculating average to avoid returning 0.00
//for average.
sum=sum+rainFall[size];
//Calculate the average rainfall.
avg=sum/SIZE;
//Return the average rainfall.
return avg;
} //End getAverage function.
//Begin getHighest function.
float getHighest(float rainFall[], int size)
{
const int SIZE=12;
double highest=rainFall[0]; //Holds highest rainfall.
int highstMnth; //Month that has the highest rainfall.
for(size=0; size<rainFall[0]; size++)
//Find the highest rainfall.
if(rainFall[size]>highest)
{
highstMnth=highest;
highest=rainFall[size];
}
//Return the highest rainfall.
return highest;
} //End getHighest function.
//Begin getLowest function.
float getLowest(float rainFall[], int size)
{
const int SIZE=12;
double lowest=rainFall[0]; //Holds the lowest rainfall.
int lowstMnth;
for(size=0; size<rainFall[0]; size++)
//Find the lowest rainfall.
if(rainFall[size]<lowest)
{
lowstMnth=lowest;
lowest=rainFall[size];
}
//Return the lowest rainfall.
return lowest;
} //End getLowest function and end of program.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.