everything works except I need it to give me the max and min month (i.e. 1,2,3..12) instead of the number entered into the array.

#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 12;
    double months[SIZE];
    int count;
    double sum = 0;
    double totalRainfall;
    double averageMonthlyRainfall;
    double min, max;


    for (count=0; count<SIZE; count ++)
    {
        cout << "What is the average rainfall for month"
             << (count+1) << ": ";
        cin >> months[count];
    }

    for (count=0; count<SIZE; count++)
    {
        sum += months[count];
    }

    totalRainfall= sum;
    cout << "total rainfall all year was " << totalRainfall << endl;
    averageMonthlyRainfall=sum/12;
    cout << "average rainfall per month was " << averageMonthlyRainfall << endl;

    max = months[0];
    for (count=0; count<SIZE; count++)
    {
        if (months[count] > max)
            max=months[count];
    }
    cout << "the max rain fall was " << max << endl;

    min = months[0];
    for ( count=0; count<SIZE; count++)
    {
        if (months[count] < min)
            min=months[count];
    }
    cout << "the min rain fall was " << min << endl;
  return 0;
}

Recommended Answers

All 3 Replies

What?

If I understand you correctly, you have to capture "count" when you set the min and max values.

yes, I need the subscript the max and min came from

Declare integers maxMonth and minMonth.

Initialize them to 0, since that's the index that you're initializing the max/min to.

Each time you find a new max/min value, in addition to setting the max and min double equal to months[count], set the respective maxMonth and minMonth equal to count.


in your code:

#include <iostream>
using namespace std;

int main()
{
const int SIZE = 12;
double months;
int count;
double sum = 0;
double totalRainfall;
double averageMonthlyRainfall;
double min, max;
int minMonth = 0;
int maxMonth = 0;


for (count=0; count<SIZE; count ++)
{
cout << "What is the average rainfall for month"
<< (count+1) << ": ";
cin >> months[count];
}

for (count=0; count<SIZE; count++)
{
sum += months[count];
}

totalRainfall= sum;
cout << "total rainfall all year was " << totalRainfall << endl;
averageMonthlyRainfall=sum/12;
cout << "average rainfall per month was " << averageMonthlyRainfall << endl;

max = months[0];
for (count=0; count<SIZE; count++)
{
if (months[count] > max)
max=months[count];
maxMonth = count;
}
cout << "the max rain fall was " << max << " in month "<<minMonth<< endl;

min = months[0];
for ( count=0; count<SIZE; count++)
{
if (months[count] < min)
min=months[count];
minMonth = count;
}
cout << "the min rain fall was " << min <<" in month "<<minMonth<< endl;

return 0;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.