1

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;
}

Edited by mike_2000_17: Fixed formatting

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by donaldw
0

What?

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

1

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;
}

Edited by donaldw: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.