Java Reference
In-Depth Information
31 System.out.println("No numbers to average");
32 } else {
33 double average = sum / totalNumber;
34 System.out.println("average = " + average);
35 }
36 System.out.println("# of negatives = " + negatives);
37 }
38 }
The program's execution will look something like this:
This program examines a sequence
of numbers to find the average
and count how many are negative.
How many numbers do you have? 8
#1? 2.5
#2? 9.2
#3? -19.4
#4? 208.2
#5? 42.3
#6? 92.7
#7? - 17.4
#8? 8
average = 40.7625
# of negatives = 2
Roundoff Errors
As you explore cumulative algorithms, you'll discover a particular problem that you
should understand. For example, consider the following execution of the previous
ExamineNumbers2 program with different user input:
This program examines a sequence
of numbers to find the average
and count how many are negative.
How many numbers do you have? 4
#1? 2.1
#2? - 3.8
#3? 5.4
#4? 7.4
average = 2.7750000000000004
# of negatives = 1
 
Search WWH ::




Custom Search