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: 666462 • 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

#include <iostream>

#include <iomanip>

#include <fstream>   

using namespace std;

void getRainFall(float[], int);// display the rainfall from text file   

float getTotalOfRainfall (float[], int); //Get rainfall     

float getAverageOfRainfall(float[], int); //Average rainfall   

double getHighestIndexOfRainfall(float[], int);//High rainfall  

double getLowestOfRainfall(float[], int);//Low rainfall    

int main()//Main method

{

       const int SIZE=12;//Size of array

       float rainFall[SIZE];  

       double sum=0; //Double declaration     

       double avg=0; //Double declaration    

       double HighestIndexOfRainfall;    

       double lowest;    

       int highestMnth=0;//High rainfall  

       int lowestMnth=0; //Lower rainfall  

      cout << fixed << showpoint << setprecision(2);

       getRainFall(rainFall, SIZE);            

       sum=getTotalOfRainfallOfRainfall(rainFall, SIZE);              

       cout << "The total rainfall for the year is " << sum << " inches. ";

       avg=getAverageOfRainfall(rainFall, SIZE);       

    cout << "The average rainfall for the year is " << avg << " inches. ";

       HighestIndexOfRainfall=getHighestIndexOfRainfall(rainFall, SIZE);    

    cout << "The largest amount of rainfall was " << HighestIndexOfRainfall

              << " inches in month " << highestMnth << endl;

       lowest=getLowestOfRainfall(rainFall, SIZE);      

       cout << "The smallest amount of rainfall was " << lowest

              << " inches in month " << lowestMnth << endl;

       getchar();

    return 0;

}

void getRainFall(float rainFall[], int i)

{

    const int SIZE=12;

    ifstream inputFile;             //Variable used to read data from "Rain.txt"

    inputFile.open("Rain.txt"); //Opens the file "Rain.txt"

    for(i=0; i<SIZE; i++)

    inputFile >>rainFall[i];

}

double getTotalOfRainfall (float rainFall[], int size)

{

    double sum=0;

    const int SIZE=12;

    for(size=0; size<SIZE; size++)//loop started

    sum=sum+rainFall[size];//Total rainfall of the year

    return sum;//sum of the rainfall

}

double getAverageOfRainfall(float rainFall[], int size)//Average function

{

       const double SIZE=12;

       double sum=0;

       double avg=0;       //Average variable.

       for(size=0; size<SIZE; size++)

       sum=sum+rainFall[size];

       avg=sum/SIZE;

       return avg;

}

double getHighestIndexOfRainfall(float rainFall[], int size)

{

       const int SIZE=12;

       double HighestIndexOfRainfall=rainFall[0]; //Holds HighestIndexOfRainfall rainfall.

    int highestMnth;                 //Month that has the HighestIndexOfRainfall rainfall.

    for(size=0; size<rainFall[0]; size++)

    if(rainFall[size]>HighestIndexOfRainfall)

    {        highestMnth=HighestIndexOfRainfall;

        HighestIndexOfRainfall=rainFall[size];

    }

   return HighestIndexOfRainfall;

}  

double getLowestOfRainfall(float rainFall[], int size)

{

   const int SIZE=12;//

    double lowest=rainFall[0];//clutch the low rainfall     

    int lowestMnth;

    for(size=0; size<rainFall[0]; size++)

    if(rainFall[size]<lowest)

    {

        lowestMnth=lowest;

        lowest=rainFall[size];

    }

       return lowest;//Low rainfall

}  //End Program

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