Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Write a program that reads the total rainfall for each of 12 months into an

ID: 666476 • 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.

Hints

#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
void readRainFall(float []);
void displayRainFall(float [], int);
float getTotalOfRainFall(float [], int);
float getAverageOfRainFall(float [], int);
int getIndexOfHighestRainFall(float [],int);
int getIndexOfHighestRainFall(float [],int);
int getIndexOfLowestRainFall(float [],int);

int main()
{
float rainFall[12];
//Call the function readRainFall
_______________________________
//Call the function displayRainFall
_______________________________
//Call the function getTotalOfRainFall
_______________________________
//Display the total of rainfall
_______________________________
//Call the function getAverageOfRainFall
_______________________________
//Display the average rainfall
_______________________________
//Call the function getIndexOfHigestRainFall
_______________________________
//Display the largest amount of rainfall
_______________________________
//Call the function getIndexOfLowestRainFall
_______________________________
//Display the lowest amount of rainfall
_______________________________

return 0;
}
void readRainFall(float rainFall[])
{//Open the file
ifstream inputFile;
//Open the file
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++;
}
}//end function readRainFall

//Define the funtion displayRainFall
void displayRainFall(float rainFall[], int months)
{
_______________________________

}

//Define the function getTotalOfRainFall
float getTotalOfRainFall(float rainFall[], int months)
{
_______________________________

}

//Define the function getAverageOfRainFall
float getAverageOfRainFall(float rainFall[], int months)
{
_______________________________

}

//Define the function getIndexOfHighestRainFall
int getIndexOfHighestRainFall(float rainFall[], int months)
{
int highestIndex = 0;
float highestRainFall = rainFall[0];
for(int i =1; i < months; i++)
{ if(___________________________)
{ highestRainFall = rainFall[i];
highestIndex = ________________;
}
}
return highestIndex;


}

//Define the function getIndexOfLowestRainFall
int getIndexOfLowestRainFall(float rainFall[], int months)
{
_______________________________

}

Explanation / Answer

#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
void readRainFall(float []);
void displayRainFall(float [], int);
float getTotalOfRainFall(float [], int);
float getAverageOfRainFall(float [], int);
int getIndexOfHighestRainFall(float [],int);
int getIndexOfLowestRainFall(float [],int);
int main()
{
float res,rainFall[12];
readRainFall(rainFall);
displayRainFall(rainFall,12);
res=getTotalOfRainFall(rainFall,12);
cout<<"The total rainfall is "<<res<<" inches. ";
res=getAverageOfRainFall(rainFall,12);
cout<<"The average rain fall is "<<res<<" inches ";
cout<<"laregest amount of rain fall is "<<rainFall[getIndexOfHighestRainFall(rainFall,12)]<<endl;
cout<<"smallest amount of rainFall is "<<rainFall[getIndexOfLowestRainFall(rainFall,12)]<<endl;
return 0;
}
void readRainFall(float rainFall[])
{//Open the file
ifstream inputFile;
//Open the file
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++;
}
}//end function readRainFall
//Define the funtion displayRainFall
void displayRainFall(float rainFall[], int months)
{
cout<<"Month Rainfall ";
for(int i=0;i<months;i++)
cout<<i+1<<" "<<rainFall[i]<<" ";
}
//Define the function getTotalOfRainFall
float getTotalOfRainFall(float rainFall[], int months)
{
float res=0;
for(int i=0;i<months;i++)
res+=rainFall[i];
return res;

}
//Define the function getAverageOfRainFall
float getAverageOfRainFall(float rainFall[], int months)
{
float res=0,sum;
sum=getTotalOfRainFall(rainFall,months);
res=sum/months;
return res;
}
//Define the function getIndexOfHighestRainFall
int getIndexOfHighestRainFall(float rainFall[], int months)
{
int highestIndex = 0;
float highestRainFall = rainFall[0];
for(int i =1; i < months; i++)
{ if(highestRainFall<rainFall[i])
{ highestRainFall = rainFall[i];
highestIndex = i;
}
}
return highestIndex;

}
//Define the function getIndexOfLowestRainFall
int getIndexOfLowestRainFall(float rainFall[], int months)
{
int lowestIndex = 0;
float lowRainFall = rainFall[0];
for(int i =1; i < months; i++)
{ if(lowRainFall<rainFall[i])
{ lowRainFall = rainFall[i];
lowestIndex = i;
}
}
return lowestIndex;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote