Rainfall Problem

Let’s imagine that you have a list that contains amounts of rainfall for each day, collected by a meteorologist. Her rain gathering equipment occasionally makes a mistake and reports a negative amount for that day. We have to ignore those. We need to write a program to (a) calculate the total rainfall by adding up all the positive integers (and only the positive integers), (b) count the number of positive integers (we will count with “1.0” so that our average can have a decimal point), and (c) print out the average rainfall at the end. Only print the average if there was some rainfall, otherwise print “No rain”.

Construct a program that correctly solves the rainfall problem

Type the program here and try it. Does it work like you thought it would?

Here is a solution. Notice that it checks that the value is positive before adding it to the sum. It also checks that the count is positive before calculating the average since you can’t divide by zero.

You have attempted of activities on this page