Board index » delphi » Can someone help me with this procedure?

Can someone help me with this procedure?

Quote
Michael Taylor wrote:

> I need to write this procedure to have the user input a bunch of numbers on
> one line, separated by a space, and ending by pressing <enter>. Print the
> average of those numbers. I'm attaching the code that I have written so
> far. My question is, how do I get to add the numbers that the user inputs
> with one variable. Do I have to write a counting loop? If so, can someone
> make a suggestion as to how to implement this?

Mike,
     Why not post code people can read?  Why bother encoding it?

     Do you understand how the standard read() procedure works?  What eoln()
does?  The concept of a loop?

     In problems like this, it is often useful to forget the specific programming
language for a minute and figure out what you want to do.  Once you know that,
then figure out how to implement it.

     It seems to me like you want to do something like the following --

  read numbers, counting how many they are and adding them up until the end
of the line (signified by the user pressing <enter>).  Compute the average from
the sum and number-of-numbers.

     If you translate the above into slightly more sophisticated "pseudocode"
(that is, write out how to do the test, how to count, how to add), you'll see that
your code is almost the Pascal code you need to solve the problem.

Bob Schor
Pascal Enthusiast

 

Re:Can someone help me with this procedure?


Bob,

Thanks for your help. BTW, I'm programming Turbo Pascal 7.0 for DOS. I'm
sorry that you couldn't read my code, so here it goes:

Program AverageNums;

USES CRT;

VAR Average, NumsToAvg, NumSum:Real;

Begin
    CLRSCR;
    Writeln;
    Writeln;
    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;
        Write('The average of the numbers is ', Average:8:2, '.');
   Readln;
   Readln
End.

Regards,
Michael

Bob Schor <bsc...@vms.cis.pitt.edu> wrote in article
<32D7AD22....@vms.cis.pitt.edu>...

Quote
> Michael Taylor wrote:

> > I need to write this procedure to have the user input a bunch of
numbers on
> > one line, separated by a space, and ending by pressing <enter>. Print
the
> > average of those numbers. I'm attaching the code that I have written so
> > far. My question is, how do I get to add the numbers that the user
inputs
> > with one variable. Do I have to write a counting loop? If so, can
someone
> > make a suggestion as to how to implement this?

> Mike,
>      Why not post code people can read?  Why bother encoding it?

>      Do you understand how the standard read() procedure works?  What
eoln()
> does?  The concept of a loop?

>      In problems like this, it is often useful to forget the specific
programming
> language for a minute and figure out what you want to do.  Once you know
that,
> then figure out how to implement it.

>      It seems to me like you want to do something like the following --

>   read numbers, counting how many they are and adding them up until the
end
> of the line (signified by the user pressing <enter>).  Compute the
average from
> the sum and number-of-numbers.

>      If you translate the above into slightly more sophisticated
"pseudocode"
> (that is, write out how to do the test, how to count, how to add), you'll
see that
> your code is almost the Pascal code you need to solve the problem.

> Bob Schor
> Pascal Enthusiast

Re:Can someone help me with this procedure?


Quote
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

Re:Can someone help me with this procedure?


Bob,

Thanks for the help. Much appreciated. Mike Copeland basically told me the
same thing. Both of you did a good job in helping me to understand what was
going on. Thanks guys.

Regards,
Michael

Bob Schor <bsc...@vms.cis.pitt.edu> wrote in article
<32D85503.2...@vms.cis.pitt.edu>...

Quote
> 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

Other Threads