> Michael Taylor wrote:

> > Write('Please enter several numbers, seperated by spaces, to ');

> > Write('average ==> ');

> > WHILE NOT EOLN DO

> > BEGIN

> > Read(NumsToAvg);

> > NumSum := NumsToAvg + NumsToAvg;

> > END;

> > Average := NumsToAvg / NumSum;

> > Writeln;

> Let's "play computer" and take a very simple case, namely you have

> only one number that you input. What happens?

> You enter the WHILE loop, read a number (say you entered "123") into

> the variable "NumsToAvg", then form the sum NumSum := NumsToAvg +

NumsToAvg,

> so NumSum becomes "246". As this is the only number you enter, the WHILE

> loop should exit since the next time through you should hit EOLN.

> OK, so at this point, NumSum is "246", and NumsToAvg is "123". You

> compute Average := 123 / 246 = 0.5, and if that's what your program is

> giving you, then you know it is working correctly.

> But is that what you want it to do? Note that it will return 0.5

for

> almost every (single) value you input (I can think of one entry that will

> cause a different answer -- do you see it?).

> You've solved the "difficult" part of this exercise, namely figuring

> out how to handle a series of numbers separated by spaces on a single

line.

> Now you have to think again about what sort of computation you'd want to

> carry out to compute the average. One technique to think about is that

> variables have to be "initialized" (all of your variables above you do

> initialize -- you initialize "NumsToAvg" by reading it in, and NumSum by

> defining it in terms of other initialized variables; where this becomes

> an issue is when you have something like "count := count + 1", where

"count"

> is defined in terms of itself, so somewhere it has to be set to some

> known quantity, often "count := 0").

> So now think about how you want to compute an average. Do it by

hand,

> and watch what you do. Try a concrete example, for instance, 1 2 3 4,

and/or

> 5 6 7, and make sure your code works.

> Bob Schor

> Pascal Enthusiast