When displaying the months with the highest and lowest rainfall amounts, it displays month nan.



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
#include <iostream>
using namespace std;

int main()
{
	const int NUM_MONTHS = 12;      // Constant for the array size
	double rainfall[NUM_MONTHS];   // An array of 12 doubles
	int count;		      // Loop counter variable
	
	// Input the total rainfall for each month.
	for (count = 0; count < NUM_MONTHS; count++)
	{
		cout << "Enter the total rainfall for month " 
			 << (count + 1) << ": ";
		cin >> rainfall[count];
		
		if (rainfall[count] < 0)
			{
				cout << "Please enter a value greater than "
				     << "or equal to 0";
				cin >> rainfall[count];
			}
	}
	
	// Find the total amount of rainfall for the year
	// and the average monthly rainfall, then display them.
	double total = 0;
	double average;
	for (count = 0; count < NUM_MONTHS; count++)
		total += rainfall[count];
	average = total / NUM_MONTHS;
	cout << "The total amount of rainfall for the whole year is "
		 << total << endl;
	cout << "The average monthly rainfall is "
		 << average << endl;
	
	// Find and display the month with the highest rainfall amount.
	int highest;
	
	highest = rainfall[0];
	for (count = 1; count < NUM_MONTHS; count++)
	{
		if (rainfall[count] > highest)
			highest = rainfall[count];
	}
	cout << "The month with the highest rainfall amount is month " 
             << rainfall[NUM_MONTHS] << " with a rainfall of " << highest
	     << endl;
	
	// Find and display the month with the lowest rainfall amount.	 
	int lowest;
	
	lowest = rainfall[0];
	for (count = 1; count < NUM_MONTHS; count++)
	{
		if (rainfall[count] < lowest)
			lowest = rainfall[count];
	}
	cout << "The month with the lowest rainfall amount is month "
             << rainfall[NUM_MONTHS] <<" with a rainfall of " << lowest
             << endl;
	
	system("pause");
	return 0;
}
for highest and lowest, the program outputs rainfall[NUM_MONTHS] which is one element past the end of the array. That's an invalid location. It happens to contain something which is "not a number", but you should not be accessing that location in any case.

I was going to say that int highest; should be a double, but I think you are intending to store the subscript rather than the value, in which case the code needs to be changed somewhat - though the overall logic seems to be heading in the right direction.
You were right with the int highest; being a double because I also want to display the values of the highest and lowest in addition to the months. Now instead of rainfall[NUM_MONTHS], I put rainfall[count]. However, now I am getting the the same values as the average instead of the subscript.
What is the value of count?

Consider this code:
1
2
3
4
for (count = 1; count < NUM_MONTHS; count++)
{ 

}

count is given the initial value 1. Each time before executing the body of the loop, the condition count < NUM_MONTHS is tested. As soon as that becomes false, the loop terminates.
In this case it is false when count == NUM_MONTHS

Unless there is an explicit need to access the loop counter after the loop has terminated (in this case there isn't), it is better to remove the declaration of count at line 8, and instead declare it locally in the loop itself:
1
2
3
4
for (int count = 1; count < NUM_MONTHS; count++)
{ 

}
as that will avoid such an error as you just made.
Topic archived. No new replies allowed.