0
votes

I recently finished working on a program that calculates the total, average, highest and lowest rainfall during a year. The program requires the user to type the rainfall each month, and then it make the calculations. When I get the lowest and highest numbers, the program should display the month corresponding those numbers, not the number itself.

My program runs fine and I don't get any error at all. However, I notice that sometimes, depending on the output, I get one of these three cases:

  • The program displays the month with the highest number, but it doesn't display the month with the lowest number.
  • The program displays the month with the lowest number, but it doesn't display the month with the highest number.
  • The program displays everything without any problems

The code is the following:

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

int main()
{
    //array containing the months
    string months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    //variables
    double data[12];
    double sum = 0;
    double avg, max1, min1;
    string max2, min2;
    for (int i = 0; i < 12; i++)//asks the user for input for each month
    {
        cout << "Enter rainfall for " << months[i] << ": ";
        cin >> data[i];
        while (data[i] < 0) //input validation
        {
            cout << "invalid data (negative rainfall) -- retry";
            cin >> data[i];
        }
    }


    for (int j = 0; j < 12; j++) //the sum of all of the numbers
    {
        sum += data[j];
    }

    max1 = data[0];
    for (int k = 0; k < 12; k++) //calculates the biggest number and its month
    {
        if (data[k] > max1)
        {
            max1 = data[k];
            max2 = months[k];
        }

    }
    min1 = data[0];
    for (int l = 0; l < 12; l++) //calculates the least number and its month
    {
        if (data[l] < min1)
        {
            min1 = data[l];
            min2 = months[l];
        }

    }
    avg = sum / 12;
    //output
    cout << "Total rainfall: " << sum << endl;
    cout << "Average rainfall: " << avg << endl;
    cout << "Least rainfall in " << min2 << endl;
    cout << "Most rainfall in " << max2 << endl;

    return 0;
}

Example of Output: code example

I don't know why this is happening. I would like if someone could help me answer that question and how I can fix that error.

If you need the original problem text, I would leave it here as well. Thank you!


Rainfall Statistics

Write a program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles . The program should calculate and display (in this order):

the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest amounts.

Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November, December.

Input Validation: Do not accept negative numbers for monthly rainfall figures. When a negative value is entered, the program outputs "invalid data (negative rainfall) -- retry" and attempts to reread the value .

Prompts And Output Labels: Decimal values should be displayed using default precision, i.e. do not specify precision. Each item read should be prompted for by a string of the form "Enter rainfall for MONTH:" where MONTH is "January" or "February" or ... or "December". The output should be of the form:

    Total rainfall: 12.36 

    Total rainfall: 1.03

    Least rainfall in August

    Most rainfall in April

where the specific amount of rainfall or specific months identified depend on the actual input.

2
Looks like you do not set max2 if the max happens on the first month because if (data[k] > max1) Is never true. - drescherjm
if (data[k] > max1) What if data[k] is never greater than max1? - PaulMcKenzie

2 Answers

0
votes

You need to set min2 and max2 everywhere you set min1 and max1 respectively.

0
votes

The problem is that you never set the strings to the maximum or minimum months if the following conditions are never met:

  if (data[k] > max1)
  //...
  if (data[l] < min1)

You can either set max2 and min2 to the first item before you enter the loop, or simply find the index of the largest and smallest values, and use the index to tell you what month to choose in your month array.

int maxIndex = 0;
max1 = data[0];

for (int k = 0; k < 12; k++) 
{
    if (data[k] > max1)
        maxIndex = k;
}

int minIndex = 0;
min1 = data[0];

for (int l = 0; l < 12; l++) 
{
    if (data[l] < min1)
        minIndex = l;
}

max2 = months[maxIndex];
min2 = months[minIndex];