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

Can someone help me with this procedure?

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

   This is obviously a homework assignment, and I can't read whatever
you posted to show your work.  Also, you don't state what your
development environment is, so it's difficult to suggest a detailed
process.  
   Assuming the worst (a standard Pascal environment, without string
support and string functions), you'll have to do the following:
  1. Read in a "line" of ASCII characters. which _should_ be
combinations of numeric digits and spaces - anything else will make
processing much more difficult.
  2. Parse the numeric digit tokens, one at a time, until the data on
the "line" is exhausted.  This means to scan the data,
character-by-character, collecting characters which are numeric digits
and stopping each time a blank is encountered.
  3. Convert each token (providing one has been formed) to integer.
This can be an iterative process of adding each digit to a sum which has
been previously multiplied by 10: e.g. 235 is
(((((0*10)+2)*10)+3)*10)+5.
  4. Use the converted token in your sum/average processing.
   The above is done in a loop, for whatever data has been read in.  I
explain a very crude process above, but without knowing your
compiler/environment, I have nothing else to offer.  Of course, since
it's a homework assignment, no honorable person will actually post
_code_ for you...
 

Re:Can someone help me with this procedure?


Mike,

Thanks for your help. BTW, I'm programming Turbo Pascal 7.0 for DOS. I'm
sorry that you couldn't read it, 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

Mike Copeland <mrc...@primenet.com> wrote in article
<32D718EB....@primenet.com>...

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

>    This is obviously a homework assignment, and I can't read whatever
> you posted to show your work.  Also, you don't state what your
> development environment is, so it's difficult to suggest a detailed
> process.  
>    Assuming the worst (a standard Pascal environment, without string
> support and string functions), you'll have to do the following:
>   1. Read in a "line" of ASCII characters. which _should_ be
> combinations of numeric digits and spaces - anything else will make
> processing much more difficult.
>   2. Parse the numeric digit tokens, one at a time, until the data on
> the "line" is exhausted.  This means to scan the data,
> character-by-character, collecting characters which are numeric digits
> and stopping each time a blank is encountered.
>   3. Convert each token (providing one has been formed) to integer.
> This can be an iterative process of adding each digit to a sum which has
> been previously multiplied by 10: e.g. 235 is
> (((((0*10)+2)*10)+3)*10)+5.
>   4. Use the converted token in your sum/average processing.
>    The above is done in a loop, for whatever data has been read in.  I
> explain a very crude process above, but without knowing your
> compiler/environment, I have nothing else to offer.  Of course, since
> it's a homework assignment, no honorable person will actually post
> _code_ for you...

Re:Can someone help me with this procedure?


In comp.lang.pascal.borland Michael Taylor <bobbi...@erols.com> wrote:
: Mike,

: Thanks for your help. BTW, I'm programming Turbo Pascal 7.0 for DOS. I'm
: sorry that you couldn't read it, 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

The only thing you ought to do is initialize NumSum to 0 to proevent
garbage from being added. It shoudln't, but... :)

--
Happy birthday HAL - 1/12/97

Other Threads