Display max value in array plus corresponding subscript.

Hello. First year programming student, here. I have a hw assignment to store monthly rainfall into an array and then find max and min and display the month that has the max/min value along with the value.

I figured out how to get the max/min VALUE but not the corresponding subscript.

Like I need to tell the user "Month 1 had the highest rainfall at 2.2 inches." But I don't know how to get the month to display. I keep getting the error message saying "minmonth has not been initialized"

Here's what I have so far

#include <iostream>
#include <iomanip>
using namespace std;

//Function Prototypes
void findHighest(double arr[]);
void findLowest (double arr[]);


int main()
{

double monthlyrainfall[12]; //Array size 12 for 12 months.

cout <<"Please enter the total rainfall for 12 months, one month at a time.\n";

//Enter the rainfall for each month
//Begin for Loop.
for (int index = 0; index < 12; index++)
{
cout <<"Please enter the total rainfall for Month #" << (index + 1) <<": ";
cin >> monthlyrainfall[index];
}

//Find Highest/Lowest Call Function
findHighest(monthlyrainfall);
findLowest(monthlyrainfall);

return 0;
}

void findHighest(double monthlyrainfall[])
{
int maxmonth;
int index = 1;
double max = monthlyrainfall[0];

for(; index < 12; index++)
{
if (monthlyrainfall[index] > max)
{
max = monthlyrainfall[index];
maxmonth = index + 1;
}
}

cout <<"The month with the greatest amount of rainfall is Month #" << maxmonth << endl;
cout <<"The max rainfall was " << setw(3) << max <<" inches.\n";
}
void findLowest(double monthlyrainfall[])
{
int minmonth;
int index = 1;
double min = monthlyrainfall[0];
for(; index < 12; index++)
{
if (monthlyrainfall[index] < min)
{
min = monthlyrainfall[index];
minmonth = index + 1;
}
}

cout <<"The month with the lowest amount of rainfall is Month #" << minmonth << endl;
cout <<"The max rainfall was " << setw(3) << min <<" inches.\n";\
}

the
Please edit your post and add code tags. That will make it much easier to review your code. Simply highlight your code and click the "<>" button in the Format menu.

In your code you are initializing max and min to the first value in the monthlyrainfall array. And then you skip the first element when you do your loop.

Because you are initializing min and max, then minmonth and maxmonth need to be initialized to 1, which is the index of the initial element + 1 (just like the other assignements to minmonth or maxmonth.
Topic archived. No new replies allowed.