help with greatest and least question using loops

Hello everyone. I would really appreciate your help. I'm still a beginner in c++ and have came up on this issue. What is happening is I'm supposed to display the greatest and least amounts for this program. Here is the problem and here is my code I have. My code works. The problem I have is it does not display the correct "least" amount for the problem. Im on step 3 by the way. The correct amount is displayed in my "cout" statement for the "largest inches per year", but I cant get the "smallest inches per year" to display correctly. I have included my code below the question. Scroll down please. Thanks everyone.
Here the question


Step #1

For this programming challenge, you will write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program first asks the user to enter a number of years. You may assume the user enters a positive number. The program then asks the user to enter, for each year, the the inches of rainfall for each of the four quarters of that year (each quarter is 3 months). You may assume the user does not enter a negative number for the inches in rainfall. The program then outputs the average rainfall per quarter for each year.
Sample input/output

Enter number of years: 2
Inches for quarter 1: 2
Inches for quarter 2: 5
Inches for quarter 3: 3
Inches for quarter 4: 4
Average inches per quarter for year 1: 3.5
Inches for quarter 1: 7
Inches for quarter 2: 4
Inches for quarter 3: 5
Inches for quarter 4: 6
Average inches per quarter for year 2: 5.5

Step #2

Same as Step #1, except that the program also will output the average rainfall per quarter for all years.

Sample input/output

Enter number of years: 2
Inches for quarter 1: 3
Inches for quarter 2: 9
Inches for quarter 3: 7
Inches for quarter 4: 6
Average inches per quarter for year 1: 6.25
Inches for quarter 1: 2
Inches for quarter 2: 5
Inches for quarter 3: 3
Inches for quarter 4: 4
Average inches per quarter for year 2: 3.5
Average inches per quarter for all years 4.875

Step #3

Same as Step #2, except that the program also will output the largest and smallest number of inches of rainfall per year. (To test this you should enter at least 3 years).

Sample input/output

Enter number of years: 3
Inches for quarter 1: 3
Inches for quarter 2: 4
Inches for quarter 3: 5
Inches for quarter 4: 6
Average inches per quarter for year 1: 4.5
Inches for quarter 1: 1
Inches for quarter 2: 2
Inches for quarter 3: 3
Inches for quarter 4: 4
Average inches per quarter for year 2: 2.5
Inches for quarter 1: 6
Inches for quarter 2: 7
Inches for quarter 3: 8
Inches for quarter 4: 9
Average inches per quarter for year 3: 7.5
Average inches per quarter for all years 4.83333
Largest inches per year: 30
Smallest inches per year: 10

Step #4

Same as Step #3, except that the program also will output the year which had the largest rainfall and the year which had the smallest rainfall. (To test this you should enter at least 3 years).

Sample input/output

Enter number of years: 3
Inches for quarter 1: 3
Inches for quarter 2: 4
Inches for quarter 3: 5
Inches for quarter 4: 6
Average inches per quarter for year 1: 4.5
Inches for quarter 1: 1
Inches for quarter 2: 2
Inches for quarter 3: 3
Inches for quarter 4: 4
Average inches per quarter for year 2: 2.5
Inches for quarter 1: 6
Inches for quarter 2: 7
Inches for quarter 3: 8
Inches for quarter 4: 9
Average inches per quarter for year 3: 7.5
Average inches per quarter for all years 4.83333
Largest inches per year: 30
Smallest inches per year: 10
Year with largest rain: 3
Year with smallest rain: 2


#include <iostream>
using namespace std;

int main()
{
int years, inches, totalInches, large, small, totalRain; ;
double average;
double total = 0;

cout << "Enter number of years: ";
cin >> years;

for (int x = 1; x <= years; x++)
{
totalInches = 0;

for ( int z = 1; z <= 4; z++)
{
cout << "Inches for quarter " << z << ": ";
cin >> inches;
totalInches += inches;




}
large = small = totalInches;
if (totalInches >= large)
large = totalInches;
if (totalInches < small)
small = totalInches;

average = totalInches / (float) 4;
cout << "Average inches per quarter for year " << x << ": " << average << endl;
total +=average;



}


cout << "Average inches per quarter for all years " << total/(double)years << endl;
cout << "Largest inches per year: " << large << endl;
cout << "Smallest inches per year: " << small << endl;

return 0;
}
Last edited on
Your code just sets large and small to be the same as totalInches, every time. That's clearly wrong.


Last edited on
Topic archived. No new replies allowed.