Rainfall Code question???

Hey, I was writing this rainfall statistics code where we're supposed to print the rain in inches that fell in each month, with the values given to us. Everything works fine, but the program prints four months with the highest rainfall, when it is only supposed to print one. It prints "The highest rainfall fell in February, the highest rainfall fell in March, the highest rainfall fell in April, the highest rainfall fell in September." Why is this happening?

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  #include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const int NUM_MONTHS = 12;
const int STRING_SIZE = 10;

void wettestMonth(double [], string []);
void driestMonth(double[], string []);
void totalRain(double[]);
void averageRain(double[]);
void zeroArray(double []);

int main()
{
        string months[NUM_MONTHS] =
                { "January", "February", "March",
                  "April", "May", "June", "July",
                  "August", "September", "October",
                  "November", "December" };
    double rain[NUM_MONTHS] = {
                            0.40, 0.94, 3.21, 3.74, 1.73, 1.03, 1.27, 2.58, 6.98, 6.90, 2.80, 2.53};
    int count;

    cout << "Austin, Tx Rainfall 2009:" << endl;
    cout << endl;
    for (int month = 0; month < NUM_MONTHS; month++)
    {
        cout << setw(9) << left << months[month] << " has ";
        cout << rain[month] << " inches of rainfall" << endl;
    }

        totalRain(rain);
        averageRain(rain);
        driestMonth(rain, months);
        wettestMonth(rain, months);

    return 0;
}

void wettestMonth(double rain[], string months[])
{
    string month[12];

    double highest = rain[0];
	int R;
	for (int i = 0; i < 12; i++)
	{
		if(rain[i] > highest)
		{
			month[i] = months[i];
			highest = rain[i];
			R = i;
		}
}
    cout << "Highest Rainfall: " << highest << " inches." << endl;
    for(int i = 0; i < 12; ++i)
    {
        if(month[i] != "")
           cout << "Month with highest rainfall: " << month[i] << endl;
    }

}

void driestMonth(double rain[], string months[])
{
    string month[12];

    double lowest = rain[0];
	    month[0] = months[0];

    for(int i = 1; i < 12; ++i)
    {
        if(rain[i] < lowest) {

           month[i] = months[i];
           lowest = rain[i];
        }
        else if(rain[i] == lowest)
        {
            month[i] = months[i];
        }
    }

    cout << "Lowest Rainfall: " << lowest << " inches" << endl;
    for(int i = 0; i < 12; ++i)
	{
        if(month[i] != "")
           cout << "Month with lowest rainfall: " << month[i] << endl;
    }


}

void totalRain(double rain[])
{
   double total = 0;
   for (int index = 0; index < NUM_MONTHS; index++)
        total += rain[index];

    cout << "Total Rainfall: " << total << " inches." << endl;
}

void averageRain(double rain[])
{

	int count = 0;
	double avgRain = 0;
	double rainSum = 0;

	for (count =0; count <=11; count++)
	{
		rainSum = rainSum + rain[count];
	}
	avgRain = rainSum / 12;
	cout<< "Average Rainfall: " << avgRain << " inches." << endl;
}
Everything works fine, but the program prints four months with the highest rainfall, when it is only supposed to print one. It prints "The highest rainfall fell in February, the highest rainfall fell in March, the highest rainfall fell in April, the highest rainfall fell in September." Why is this happening?


You keep a record of which months are the current highest months as you're figuring out which is ultimately highest. The month array in wettestMonth tracks these. At the end of the function you print each month which was ever the highest at any point in the calculation.

Something like this, maybe:
1
2
3
4
5
6
7
8
9
10
11
12
13
void wettestMonth(double rain[], string months[])
{
    int hi_idx = 0;

    for (int i=1; i<12; ++i)
    {
        if (rain[i] > rain[hi_idx])
            hi_idx = i;
    }

    cout << "Highest Rainfall: " << rain[hi_idx] << " inches." << endl;
    cout << "Month with highest rainfall: " << month[hi_idx] << endl;
}
Last edited on
What you said makes a lot of sense, thanks!
When I try to run the program, the compilaer outputs the following errors:
"error: invalid types 'double*[double]' for array subscript|"
What does this mean?
I need to submit this tonight, so I desperately need help.
Thank you for your help!
I didn't test the code and originally typed hi_idx as double. Make it an integral type as it is above after the edit.

You can't index an array with a value of type double. (What element should array[1.3352] access?)
Now the code runs, but the program prints "month with highest rainfall: " and doesn't print the month. Any idea why this is happening?
closed account (48T7M4Gy)
month with highest rainfall is month[R]?
Now the code runs, but the program prints "month with highest rainfall: " and doesn't print the month. Any idea why this is happening?

Inspecting the code I supplied again, it still shouldn't compile. There is no variable month, it should be months.

So, rather than saying it isn't working, provide the actual code you're using.
sorry, this is my current 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const int NUM_MONTHS = 12;
const int STRING_SIZE = 10;

void wettestMonth(double [], string []);
void driestMonth(double[], string []);
void totalRain(double[]);
void averageRain(double[]);
void zeroArray(double []);

int main()
{
        string months[NUM_MONTHS] =
                { "January", "February", "March",
                  "April", "May", "June", "July",
                  "August", "September", "October",
                  "November", "December" };
    double rain[NUM_MONTHS] = {
                            0.40, 0.94, 3.21, 3.74, 1.73, 1.03, 1.27, 2.58, 6.98, 6.90, 2.80, 2.53};
    int count;

    cout << "Austin, Tx Rainfall 2009:" << endl;
    cout << endl;
    for (int month = 0; month < NUM_MONTHS; month++)
    {
        cout << setw(9) << left << months[month] << " has ";
        cout << rain[month] << " inches of rainfall" << endl;
    }

        totalRain(rain);
        averageRain(rain);
        driestMonth(rain, months);
        wettestMonth(rain, months);

    return 0;
}

void wettestMonth(double rain[], string months[])
{
    int hi_idx = 0;
    string month[12];
    for (int i=1; i<12; ++i)
    {
        if (rain[i] > rain[hi_idx])
            hi_idx = i;
    }

    cout << endl;
    cout << "Highest Rainfall: " << rain[hi_idx] << " inches" << endl;
    cout << "Month with highest rainfall: " << month[hi_idx] << endl;
}

void driestMonth(double rain[], string months[])
{
    string month[12];

    double lowest = rain[0];
	    month[0] = months[0];

    for(int i = 1; i < 12; ++i)
    {
        if(rain[i] < lowest) {

           month[i] = months[i];
           lowest = rain[i];
        }
        else if(rain[i] == lowest)
        {
            month[i] = months[i];
        }
    }
    cout << endl;
    cout << "Lowest Rainfall: " << lowest << " inches" << endl;
    for(int i = 0; i < 12; ++i)
	{
        if(month[i] != "")
           cout << "Month with lowest rainfall: " << month[i] << endl;
    }
}

void totalRain(double rain[])
{
   double total = 0;
   for (int index = 0; index < NUM_MONTHS; index++)
        total += rain[index];
    cout << endl;
    cout << "Total Rainfall: " << total << " inches." << endl;
}

void averageRain(double rain[])
{

	int count = 0;
	double avgRain = 0;
	double rainSum = 0;

	for (count =0; count <=11; count++)
	{
		rainSum = rainSum + rain[count];
	}
	avgRain = rainSum / 12;
	cout << endl;
	cout<< "Average Rainfall: " << avgRain << " inches." << endl;
}


Thank you again for your help!
Look at line 45. Look at line 54. Is there anything that leads you to believe that the array defined on line 45 has anything but empty strings in it by the time line 54 is reached? What array actually has the names of months in it?
I made a few changes, and this is my code for wettestMonth now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void wettestMonth(double rain[], string months[])
{
    int hi_idx = 0;
    string month[12];
    for (int i=1; i<12; ++i)
    {
        if (rain[i] > rain[hi_idx])
        {
            month[i] = months[i];
            hi_idx = rain[i];
        }

    }

    cout << endl;
    cout << "Highest Rainfall: " << rain[hi_idx] << " inches" << endl;
    cout << "Month with highest rainfall: " << month[hi_idx] << endl;
}


The code runs, but it prints the highest rainfall as 3.21 inches, and the wettest month as March. I honestly don't know what else to do, I've tried everything and nothing will work. Please help!!
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void wettestMonth(double rain[], string months[])
{
    
    double highest = rain[0];
    int highest_index = 0;
    
    for (int i = 0; i < NUM_MONTHS; i++)
    {
        if(rain[i] > highest)
        {
            highest = rain[i];
            highest_index = i;
        }
    }
    cout << "Highest rainfall: " << rain[highest_index] << " inches." << endl;
    cout << "Highest rainfall month is " << months[highest_index] << endl;
}
Thank you so much for your help! I really appreciate it!
Topic archived. No new replies allowed.