Return to Jeff's Main Page

CS1 at Oswego

Hypertexknowlogy

Frequently Asked Questions

 
My Intro to Object-Oriented Programming  
 
 
 
Class Notes

Monday October 23 , 2000
 
The first half of class was a continuation from last Wednesday's lesson on ``incremental programming''. 

The remainder of this class session dealt with the following problem: 


Problem:   Read several (at least one) non-negative integers and computer their real average. 

/ /Establish an ``accumulator''
int sum = 0; 

/ /Establish a counter to count data.
int count = 0; 

/ /Sum the integers
int num = IO.read_int ();
while (num    > =    0)
{

sum = sum + num;
count = count + 1;  / /count + +
num = IO.read_int ();

/ /Compute and display average.
double ave = (double) sum    /    (double) count;
IO.println (``ave =  ''    +    ave);
} / /end 

In this program,  the input of 5,  10,  - 2 would result in an output of 7.5.   The input of - 2 broke the loop of the while statment and resulted in the input of 5 and 10 being averaged.