rainfall program using functions

Hi, I am a beginner to programming and i am taking a programming class. The assignment i am having issues with is the creation of a rainfall program. The program will give the total rainfall for the year, the average monthly rainfall, the highest amount of rainfall and what month it fell in, an the lowest amount of rainfall and the month it fell in. The program must have the following functions:

double getTotal(double[], int);
double getAverage(double[], int);
double getLowest(double[], int, int&);
double getHighest(double[], int. int&);

The issues i am having is pulling the month for the lowest rainfall amount. I am able to get the lowest amount but i am not able to get the month that it fell. Any help would be great. Thanks in advance.


#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

double getTotal(double[], int);
double getAverage(double[], int);
double getLowest(double[], int, int&);
double getHighest(double[], int, int&);

int main()
{
const int SIZE = 12;
string months[SIZE] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"};
double rainfall[SIZE],
total,
average,
lowest;
string lowmonth;
int count;
for (count = 0; count < SIZE; count++)
{
cout << "What is the rainfall amount for the month of " << months[count] << "?" << endl;
cin >> rainfall[count];
while (rainfall[count] < 0)
{
cout << "You entered an invalid number please enter a value greater than 0." << endl;
cin >> rainfall[count];
}
}
total = getTotal(rainfall, SIZE);
cout << "The total rainfall for the year is: " << total << endl;
average = getAverage(rainfall, SIZE);
cout << "The average monthly rainfall is: " << average << endl;
lowest = getLowest(rainfall, SIZE, count);
cout << "lowest rainfall amount is: " << lowest << ", which fell in the month of: " << lowmonth << endl;
return 0;
}

double getTotal(double rainfall[], int SIZE)
{
double total = 0;

for (int count = 0; count < SIZE; count++)
total += rainfall[count];

return total;
}
double getAverage(double rainfall[], int SIZE)
{
double average = 0;
double total;
total = getTotal(rainfall, SIZE);

average = total / SIZE;

return average;
}
double getLowest(double rainfall[], int SIZE, int&count)
{
double lowest;
string lowmonth;
string months;

lowest = rainfall[0];
for (count = 1; count < SIZE; count++)
{
if (rainfall[count] < lowest)
lowmonth = months[count];
lowest = rainfall[count];
}

return lowest;
}
Just because two variables got the same name doesn't mean that they are the same variable. You've got a `lowmonth' in main() and a `lowmonth' in getLowest(), they are not related in any way.


Also, you've got a bug in your function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double getLowest (double rainfall[], int SIZE, int& count)
{
	double lowest;
	string lowmonth;
	string months;

	lowest = rainfall[0];
	for (count = 1; count < SIZE; count++)
	{
		if (rainfall[count] < lowest)
			lowmonth = months[count];
		lowest = rainfall[count]; //note the indentation
	}

	return lowest;
}


You may do getLowest() to return the index of the min element, then you can do
1
2
lowest = getLowest(rainfall, SIZE, count);
cout << "lowest rainfall amount is: " << rainfall[lowest] << ", which fell in the month of: " << months[lowest] << endl;
Thanks for the reply. I have made a couple changes to the program and made the changes you suggested. I am getting it to identify a month, but the lowest rainfall amount is not accurate now and the month of the lowest is not accurate as well. Below is the code I have now.

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
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

double getTotal(double[], int);
double getAverage(double[], int);
double getLowest(double[], int, int&);
double getHighest(double[], int, int&);

int main()
{
	const int SIZE = 12;
	string months[SIZE] = { "January", "February", "March", "April", "May", 
							"June", "July", "August", "September", "October", 
							"November", "December"};
	double rainfall[SIZE],
		total,
		average;
	int lowest;

	int count;
	for (count = 0; count < SIZE; count++)
	{
		cout << "What is the rainfall amount for the month of " << months[count] << "?" << endl;
		cin >> rainfall[count];
		while (rainfall[count] < 0)
		{
			cout << "You entered an invalid number please enter a value greater than 0." << endl;
			cin >> rainfall[count];
		}
	}
	total = getTotal(rainfall, SIZE);
	cout << "The total rainfall for the year is: " << total << endl;
	average = getAverage(rainfall, SIZE);
	cout << "The average monthly rainfall is: " << average << endl;
	lowest = getLowest(rainfall, SIZE, count);
	cout << "lowest rainfall amount is: " << rainfall[lowest] << ", which fell in the month of: " << months[lowest] << endl;
	return 0;
}	

double getTotal(double rainfall[], int SIZE)
{
	double total = 0;

	for (int count = 0; count < SIZE; count++)
		total += rainfall[count];
	
	return total;
}
double getAverage(double rainfall[], int SIZE)
{
	double average = 0;
	double total;
	total = getTotal(rainfall, SIZE);

	average = total / SIZE;
	
	return average;
}
double getLowest(double rainfall[], int SIZE, int &count)
{
	double lowest;

	lowest = rainfall[0];

	for (count = 1; count < SIZE; count++)
	{
		if (rainfall[count] < lowest)
			lowest = rainfall[count];
	}
	
	return lowest;
}
No, lowest should be an index, not the value.
1
2
3
4
int lowest = 0;
for(int K=0; K<SIZE; ++K)
   if(array[K] < array[lowest])
      lowest = K;


you may also use http://www.cplusplus.com/reference/algorithm/min_element/
Last edited on
Thanks very much, It worked.
Topic archived. No new replies allowed.