<p>Write a program that reads in the average monthly rainfall for a city for eac
ID: 3631705 • Letter: #
Question
<p>Write a program that reads in the average monthly rainfall for a city for each month of the year, and then reads in the actual monthly rainfall for each of the previous 12 months. The program then prints out a nicely formatted table showing the rainfall for each of the previous 12 months as well as how much above or below average the rainfall was for each month The user will first enter the average monthly rainfall for the months January, February, and so forth, in order. To obtain the actual rainfall for the previous 12 months, the program first asks what the current month is and then asks for the rainfall figures for the previous 12 months. For example, if the user answers that the current month is October, then the program will ask for rainfall for September, August, July, and so forth, in reverse order. The output should correctly label the months. </p><div><br />
<blockquote class="webkit-indent-blockquote">
<div>There are a variety of ways to deal with the month names. One straightforward method is to create an array with 12 month names, and then index into this array to obtain the correct name. You can create this array by giving it an initialization list with the names of the 12 months.</div>
</blockquote>
<p>A nicely formatted output table might look as follows:</p>
</div>
<pre><span class="Apple-style-span"><span>Month Rainfall Deviation From Avg<br />September 3.81 0.89<br />August 1.21 -1.32<br />July 10.53 7.21<br />June 4.58 -0.21<br />...</span></span></pre>
<p>Notice that month names are left justified and that numeric amounts are right-justified. Your output does not have to look exactly like this, but it should resemble it.</p>
<p><span>(This problem has been taken from here [</span><span>http://www.cramster.com/solution/solution/864012</span><span>] and reworded, i cannot seem to get my code to create JUST the table, and obtain actual rainfall in reverse order)</span></p>
Explanation / Answer
please rate - thanks
# include <iostream>
#include<iomanip>
using namespace std;
int main ()
{int i,start;
double amt[12],actual[12];
string months[12]={"January","February","March","April","May","June","July",
"August","September","October","November","December"};
cout<<"Enter average rainfall for ";
for(i=0;i<12;i++)
{cout<<months[i]<<": ";
cin>>amt[i];
}
cout<<"What month is it (1:(Jan.) - 12(Dec.)): ";
cin>>start;
start-=2;
if(start<0)
start=11;
cout<<"Enter actual rainfall for ";
for(i=0;i<12;i++)
{cout<<months[start]<<": ";
cin>>actual[start];
start--;
if(start<0)
start=11;
}
cout<<" The years rainfall : ";
cout<<"Month average deviation from average ";
for(i=0;i<12;i++)
cout<<setw(15)<<left<<months[i]<<" "<<right<<setprecision(2)
<<fixed<<amt[i]<<" "<<setprecision(2)<<fixed<<right<<actual[i]-amt[i]<<" ";
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.