Help me with Array please!

When I run my program, everything runs fine until I get to the findHighest(rainfallArray, numMonths, highestMonth); in line 57. For some reason, after cout total rainfall line, a window pops up saying "[name of function] has stopped working"
then i get a return value of 255 in my output screen and the findHighest output never gets displayed. Can somebody tell me the problem in my code?
Thanks!

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


double averageFunction(double rainfallArray[],int numMonths, double& sum);
double findHighest(double rainfallArray[], int numMonth, string& monthName);



int main()
{
    const int numMonths = 12;
    double rainfallArray[numMonths-1];
    double total;
    string highestMonth;

    cout << "Enter the rainfall for each month in order." << endl;
    for (int i = 0; i < numMonths; i++)
    {
        if (i==0)
            cout << "January: ";
        else if (i == 1)
            cout << "February: ";
        else if (i==2)
            cout << "March: ";
        else if (i==3)
            cout << "April: ";
        else if (i==4)
            cout << "May: ";
        else if (i==5)
            cout << "June: ";
        else if (i==6)
            cout << "July: ";
        else if (i==7)
            cout << "August: ";
        else if (i==8)
            cout << "September: ";
        else if (i==9)
            cout << "October: ";
        else if (i==10)
            cout << "November: ";
        else if (i==11)
            cout << "December: ";
        do
        {
        cin >> rainfallArray[i];
        if (rainfallArray[i] < 0)
            {cout << "You can not enter negative values." << endl;
            cout << "Please enter an appropriate value." << endl;}
        }while (rainfallArray[i] < 0);
    }


cout << "The average monthly rainfall is " << averageFunction(rainfallArray, numMonths, total) << " inches." << endl;
cout << "The total rainfall for the year is " << total << " inches." << endl;
findHighest(rainfallArray, numMonths, highestMonth);
cout << "The month with the highest rainfall is " << highestMonth << " with " <<
findHighest( rainfallArray,  numMonths, highestMonth) << " inches of rainfall." << endl;

return 0;
}

double averageFunction(double rainfallArray[], int numMonths, double& sum)
{
   sum = 0;
    for (int k=0; k< numMonths; k++)
    {
        sum+=rainfallArray[k];
    }
    return (sum/12);
}

double findHighest(double rainfallArray[], int numMonths, string& monthName)
{

    double highest = 0;
    int month;
    for (int i=0; i<numMonths; i++)
    {
         if (rainfallArray[i]>highest)
            {highest = rainfallArray[i];
                month = i;
            }
    }
        if (month ==0)
            monthName =  "January";
        else if (month == 1)
            monthName =  "February";
        else if (month ==2)
            monthName = "March";
        else if (month ==3)
            monthName =  "April";
        else if (month ==4)
            monthName =  "May";
        else if (month==5)
            monthName =  "June";
        else if (month==6)
            monthName = "July";
        else if (month==7)
            monthName =  "August";
        else if (month==8)
            monthName =  "September";
        else if (month==9)
            monthName =  "October";
        else if (month==10)
            monthName = "November";
        else if (month ==11)
            monthName =  "December";

    return highest;

}
.
1
2
3
4
5
6
7
8
9
10
11
const int numMonths = 12;
//rainfallArray contains 11 elements with valid indices 0-10
double rainfallArray[numMonths-1];

double findHighest(double rainfallArray[], int numMonths, string& monthName)
{
   double highest = 0;
    int month;
    //Loop will iterate 12 times and in the end will try to access rainfallArray[11]
    //Which is invalid index and leads to out-of-bound access
    for (int i=0; i<numMonths; i++)
oh yes yes. thank you. that was a silly mistake. i thought for a second there should be 11 elements because i am declaring numbers into element 0 to 11, but i forgot that the declaration array should include the number of elements.... not the last index.
Topic archived. No new replies allowed.