C++ HELP! Write a program, IN C++< that reads from a manually created file “Rain
ID: 3687731 • Letter: C
Question
C++ HELP!
Write a program, IN C++< 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.
INCLUDE COMMENTS TO DESCRIBE THE CODE!!
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
#include<iostream>
#include<stdlib>
#include<fstream>
using namespace std;
double * fillArray(double rainMonth[])
{
int i=0;
ifstream in("Rainfall.txt");
while(in)
{
in>>rainMonth[i];
i++;
}
return rainMonth;
}
void display(double rainMonth[])
{
double av,sum=0,i;
cout<<"Rainfall for each month:";
for(i=0;i<12,i++)
{
cout<<rainMonth[i]<<"; ";
sum=sum+rainMonth[i];
}
av=sum/12;
cout<<"Total rainfall:"<<sum<<endl;
cout<<"Average monthly rainfall:"<<av;
}
void min_max(double rainMonth[])
{
double min=rainMonth[0],max=rainMonth[0];
int i,ind,ind1;
for(i=0;i<12;i++)
{
if(rainMonth[i]<min)
{
min=rainMonth[i];
ind=i;
}
if(rainMonth[i]>min)
{
max=rainMonth[i];
ind1=i;
}
}
cout<<"Highest Rainfall recorded is"<<max<<"inches"<<endl;
cout<<"Lowest Rainfall recorded is"<<min<<"inches";
}
void main()
{
string nameMonth[]={"January","February","March","April","May","June","July","August","September","Octomber","November","December"};
double rainMonth[12];
double rainMonth=fillArray(rainMonth);
display(rainMonth);
min_max(rainMonth);
system("pause");
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.