Rainfall Statistics

The problem is:
Write a program that lets the user enter 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.
Input Validation: Do not accept negative numbers for monthly rainfall figures.

my code works, but I'm curious if theres a way of shortening it? I feel like I listed too many variables

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;




int main ()
{
const int MONTHS = 12;
string name[MONTHS]= {"January","February","March","April","May","June","July","August","September","October","November","December"};
int count= 0;
double rain[MONTHS];
double avg;
double year=0;
double highest;
string highMonth;
double lowest;
string lowMonth;




for(count = 0; count < MONTHS; count++) // ask user to enter amount of rainfall for each month
{
cout <<"How many inches of rain does "<< name[count];
cout<< " have? \n";
cin >> rain[count];
while (rain[count] < 0)
{
cout << "Please enter a number greater than 0."<< endl;
cin >> rain[count];
}
}
for(int count=0; count<MONTHS; count++) // totals up all the rainfall
year += rain[count];

avg = year / MONTHS;

for( int count = 0; count < MONTHS; count++) //spits out each month with its amount of rain
{
cout << name[count];
cout<< " has ";
cout<< rain[count] << " inches of rainfall.\n";
}


highest = rain[0]; // finds month with the highest amount of rain

for (count = 1 ;count < MONTHS; count++)
{
if (rain[count] > highest)
{
highMonth = name[count];
highest = rain[count];
}
}


lowest = rain[0]; // finds month with the least amount of rain

for (count = 1 ;count < MONTHS; count++)
{
if (rain[count] < lowest)
{
lowMonth = name[count];
lowest = rain[count];
}
}


cout << endl;

cout << setprecision(2) << fixed;

cout <<"Total Rainfall throughout the year is " <<year << " inches" << endl;

cout <<"The average monthly rainfall is " << avg << " inches"<< endl;

cout <<"The month with the highest amount of rainfall is " << highMonth << " with " << highest<< " inches."<< endl;

cout <<"The month with the lowest amount of rainfall is " << lowMonth << " with " << lowest<< " inches."<< endl;

return 0;
}










There are some things that can be shortened down yes. For example, you dont need 2 individual for-loops to find the lowest and highest. Here is the full code -

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
const int MONTHS = 12;
	int count = 0;
	string name[MONTHS] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
	double rain[MONTHS], avg, year = 0, highest, lowest;
	string highMonth, lowMonth;

	for (count = 0; count < MONTHS; count++) // ask user to enter amount of rainfall for each month
	{
		cout << "How many inches of rain does " << name[count] << " have? \n";
		cin >> rain[count];
		while (rain[count] < 0)
		{
			cout << "Please enter a number greater than 0." << endl;
			cin >> rain[count];
		}
	}

	highest = rain[0]; // finds month with the highest amount of rain
	lowest = rain[0]; // finds month with the least amount of rain

	for (count = 1; count < MONTHS; count++)
	{
		if (rain[count] > highest)
		{
			highMonth = name[count];
			highest = rain[count];
		}
		else if (rain[count] < lowest)
		{
			lowMonth = name[count];
			lowest = rain[count];
		}
	}

	avg = year / MONTHS;

	for (count = 0; count < MONTHS; count++)
	{
		year += rain[count];
		cout << name[count] << "has " << rain[count] << " inches of rainfall.\n";
	}

	cout << endl << setprecision(2) << fixed;
	cout << "Total Rainfall throughout the year is " << year << " inches" << endl;
	cout << "The average monthly rainfall is " << avg << " inches" << endl;
	cout << "The month with the highest amount of rainfall is " << highMonth << " with " << highest << " inches." << endl;
	cout << "The month with the lowest amount of rainfall is " << lowMonth << " with " << lowest << " inches." << endl;
Hi,

Please always use code tags - use the <> button on the format menu.

The number of variables you have is fine, but I would name them a little differently, for example AverageRainFall, MonthNames, MonthlyRainFall, TotalYearlyRain, HighestMonthlyRain, LowestMonthlyRain. It might seems like overkill for this, but meaningful names are an aid to understanding, can help prevent errors, improve readability etc.

Arrays start at 0, so the for loops should too, otherwise you will miss the first one and have a segmentation fault as it runs past the end of the array.

You can use "\n" instead of std::endl

I like the way you split your std::cout statements over several lines :+) There is no need to put them all into 1 statement.

Hope all goes well :+)

Edit:

Your code would benefit from splitting it into functions - there is an unwritten rule that functions should be less than 40 Lines Of Code (LOC) . This also aids understanding and readability.

So you could have functions for getting the input, Average, lowest, highest, total, showing the output.
Last edited on
thank you guys so much !!
Topic archived. No new replies allowed.