Finding the Average Rainfall

Hello fellow programmers, I need your help with this problem trying to find the average rainfall. I got most of it, but I'm not understanding the part where I have to find the average rainfall per month for the entire period. Is it like the average for each year, or the same month like January for all three 3 years, or something like that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Write a program that uses nested loops to collect data and calculate the average
rainfall over a period of years. The program should first ask for the number of years. The
outer loop will iterate once for each year. The inner loop will iterate twelve times,
once for each month. Each iteration of the inner loop will ask the user for the inches
of rainfall for that month.
After all iterations, the program should display the number of months, the total
inches of rainfall, and the average rainfall per month for the entire period.
Input Validation: Do not accept a number less than 1 for the number of years. Do not
accept negative numbers for the monthly rainfall.
*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
using namespace std;

int main()
{

	int inchesRainfall,
		numMonths,
		total,
		average,
		years,
		months = 12;

	cout << "Average Rainfall" << endl;
	cout << "\nEnter the number of years: ";
	cin >> years;

	while(years <= 1)
	{
		cout << endl << "Enter a number greater than 1 for the number of years. ";
		cin >> years;
	}

	numMonths = 12 * years;

	cout << "\nEnter the amount of rainfall (in inches) for each month in the year.\n" << endl;
	for(int count = 1; count <= years; count++)
	{
		total = 0;
		for(int counter = 1; counter <= months; counter++)
		{
			cout << "Year" << count << ", Month " << counter << ": ";
			cin >> inchesRainfall;

			total += inchesRainfall;
		}
		average = total / years;

		cout << "\nThe total amount of rainfall for month " << count << " is " << average << " inches." << endl;

		cout << endl;
	}

	cout << endl << "\nThere were " << numMonths << " months in total." << endl;
	cout << "The total amount of rainfall for all " << years << " years is " << total << " inches." << endl;

	return 0;
} 
"per month" hints to "average for Jan over these years was X".

It is actually easier too, because there are exactly 12 months per year, but there could be 3 or 42 years.
try this...Its working;..............

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
using namespace std;

int main()
{

int inchesRainfall,
numMonths,
years,
months = 12;
float average,
total;

cout << "Average Rainfall" << endl;
cout << "\nEnter the number of years: ";
cin >> years;

while(years <= 1)
{
cout << endl << "Enter a number greater than 1 for the number of years. ";
cin >> years;
}

numMonths = 12 * years;

cout << "\nEnter the amount of rainfall (in inches) for each month in the year.\n" << endl;
for(int count = 1; count <= years; count++)
{
total = 0;
for(int counter = 1; counter <= months; counter++)
{
cout << "Year" << count << ", Month " << counter << ": ";
cin >> inchesRainfall;

total += inchesRainfall;
}
average = total / years;

cout << "\nThe total amount of rainfall for month " << count << " is " << average << " inches." << endl;

cout << endl;
}

cout << endl << "\nThere were " << numMonths << " months in total." << endl;
cout << "The total amount of rainfall for all " << years << " years is " << total << " inches." << endl;
average=(total /(12 * years));
cout<<"The average of the rainfall for all is "<<average<<endl;

return 0;
}
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <cstring>
using namespace std;

int main()
{

int inchesRainfall,
numMonths,

years,
months = 12;
float average,
total;

cout << "Average Rainfall" << endl;
cout << "\nEnter the number of years: ";
cin >> years;

while(years <= 1)
{
cout << endl << "Enter a number greater than 1 for the number of years. ";
cin >> years;
}

numMonths = 12 * years;

cout << "\nEnter the amount of rainfall (in inches) for each month in the year.\n" << endl;
for(int count = 1; count <= years; count++)
{
total = 0;
for(int counter = 1; counter <= months; counter++)
{
cout << "Year" << count << ", Month " << counter << ": ";
cin >> inchesRainfall;

total += inchesRainfall;
}
average = total / years;

cout << "\nThe total amount of rainfall for month " << count << " is " << average << " inches." << endl;

cout << endl;
}

cout << endl << "\nThere were " << numMonths << " months in total." << endl;
cout << "The total amount of rainfall for all " << years << " years is " << total << " inches." << endl;
average=(total /(12 * years));
cout<<"The average of the rainfall for all is "<<average<<endl;

return 0;
}
Topic archived. No new replies allowed.