Min of an array (temperature)

Hi all,

I have a program that lets the user enter the total rainfall for each of
the 12 months, and then display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.

I'm running into problem with outputting the month with the least rainfall. I don't know why that even though I initialized my lowest, it's still giving me garbage value. Can you please look at it and tell me what I did wrong? Thanks so much.

#include <iostream>

using namespace std;

int main()
{
double rainfall[12];
double sum = 0;
double average = 0;
double highest = rainfall[0];
double lowest = rainfall[0];
int most, least;

for (int i=0; i<12; i++)
{
cout << "Enter the amount of rainfall in the " << i+1 << " month: ";
cin >> rainfall[i];

if (rainfall[i] < 0)
{
cout << "Please don't enter a negative number.";
i--;
cout << endl;
}


else
{
sum += rainfall[i];

if (rainfall[i] > highest)
{
highest = rainfall[i];
most = i+1;
}

if (rainfall[i] < lowest)
{
lowest = rainfall[i];
least = i+1;
}

}
}

average = sum / 12;

cout << "The total rainfall this year is " << sum << endl;
cout << "The average rainfall per month is " << average << endl;
cout << most << " is the month with the most rainfall and " << least
<< " is the month with the least rainfall." << endl;
And I'm really sorry with the formatting. This is my first time posting. I don't know how to make it look pretty.
Topic archived. No new replies allowed.