[comp.lang.pascal] Algorithm for average time of day?

azarian@hpcc01.HP.COM (Randy Azarian) (09/06/89)

Does anyone have an algorithm available that will calculate
an average time of day?

For example:
   
     4:30
     5:30
     -> average would be 5:00


Thanks,

lihan@walt.cc.utexas.edu (Bruce Bostwick) (09/06/89)

In article <1780003@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>Does anyone have an algorithm available that will calculate
>an average time of day?
>
>For example:
>   
>     4:30
>     5:30
>     -> average would be 5:00

Try converting the times to decimal hours, e.g. 4:30 becomes 4.5, 5:30 becomes
5.5; then averaging those two numbers and converting back to h:mm format.

If noon becomes a problem, you can remove some worries by using 24-hour
decimal time -- 4:30 PM would be 16.5, etc., etc.

r

=======================================================================
Internet:lihan@walt.cc.utexas.edu     Disclaimer: My employer doesn't
MaBellNet:(512)459-1602                    even know UseNet exists, let
SlowNet:varies chaotically,                alone that I'm on it and ex-
     e-mail for current value              pressing opinions ...
+---------------------------------------------------------------------+
                       (-:   a.k.a. BB/CIV   :-)
=======================================================================

winfave@dutrun.UUCP (Alexander Verbraeck) (09/08/89)

In article <1780003@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>Does anyone have an algorithm available that will calculate
>an average time of day?
>
>For example:
>   
>     4:30
>     5:30
>     -> average would be 5:00

I do not have a readily available algorithm, but I don't think it would
be very difficult writing one. You need two functions, one that
translates the TOD to the number of minutes since midnight and one that
translates the number of minutes to the TOD using div and mod functions.
It would look something like this:

{ Suppose the time is on a 24 hour clock (00:00 to 23:59)
  and it is coded as an hour field and a minutes field.
  If it is coded as a string, first split in hour and minutes.
}

function TODtoMinutes(Hour,Min:integer):integer;

begin
  TODtoMinutes:=Hour*60+Min;
end;

procedure MinutesToTOD(Input:integer;var Hour,Min:integer);

begin
  Hour := Input div 60;
  Min  := Input mod 60;
  { didn't think too much on this... am not sure it is right...
    always have problems with div and mod... }
end;

procedure AverageTimes(H1,M1,  H2,M2 : integer; var HO,MO:integer);

var T1,T2,T3 : integer;

begin
  T1:=TODtoMinutes(H1,M1);
  T2:=TODtoMinutes(H2,M2);
  T3:=(T1+T2) div 2;
  MinutesToTOD(T3,HO,MO);
end;

---------------------------------------------------------------------
Alexander Verbraeck                            e-mail:
Delft University of Technology                 winfave@hdetud1.bitnet
Department of Information Systems              winfave@dutrun.uucp
PO Box 356, 2600 AJ  The Netherlands
---------------------------------------------------------------------

ok@cs.mu.oz.au (Richard O'Keefe) (09/09/89)

In article <1780003@hpcc01.HP.COM> azarian@hpcc01.HP.COM (Randy Azarian) writes:
>Does anyone have an algorithm available that will calculate
>an average time of day?

In article <898@dutrun.UUCP>, winfave@dutrun.UUCP (Alexander Verbraeck) writes:
> I do not have a readily available algorithm, but I don't think it would
> be very difficult writing one.

I didn't reply to this earlier because I was sure that someone else would
point it out, but "average time of day" is not a well-defined concept.
The point is that things like the mean and median are defined for LINEAR
scales, but time of day is CYCLIC.  Run, do not walk, to the statistics
department of your local university, and ask for advice about "location
measures for circular distributions".  Or post a question in sci.math.stat.

dl2p+@andrew.cmu.edu (Douglas Allen Luce) (09/10/89)

I'm trying to read the contents of a text file into memory and have a single
pointer to it.  I quickly figured up a couple of different ways to do this:
 
First count all the characters in the text file, then allocate enough memory
to hold it, then go back and read in the file.  I figured this would be a bit
slower than:

Read in a line of the text file.  Allocate enough memory to hold it, and
put it into memory.  Do this for each line.
 
The algorithm I worked up for reading it in line-by-line is like this:
 
var
  totalpointer:pointer;
  pst:^string;
  st:string;
 
begin
  reset(textfile);
  totalpointer:=nil;
  while not eof(textfile) do begin
    readln(textfile,st);
    getmem(pst,length(st)+1); {add one for string[0] byte}
    pst^:=st;
    if totalpointer=nil then totalpointer:=pst;
  end;
end;
 
The effect of this is to read the text file into memory and pointer
totalpointer to it.
 
I would figure this works IF getmem returns contiguous blocks of
memory each time it is called in sequence; is this true?  And are
there other operations (other than new) that would interfere with getmem
returning contiguous blocks?
 
Thanks in advance,
 
Douglas Luce
Carnegie Mellon

ken@aiai.ed.ac.uk (Ken Johnson) (09/13/89)

In article <8Z2az8G00WB8M1AUcT@andrew.cmu.edu> dl2p+@andrew.cmu.edu (Douglas Allen Luce) writes:
>I'm trying to read the contents of a text file into memory and have a single
>pointer to it.  I quickly figured up a couple of different ways to do this:
> 
>First count all the characters in the text file, then allocate enough memory
>to hold it, then go back and read in the file.  I figured this would be a bit
>slower than:

Here is an alternative.  I can't spell it out in great detail because I
don't know which operating system you are using.  However, both MS/DOS
and UNIX allow you to find the length of a file from information about
it held in the disk directory.  Read that information, then create a
buffer of the right size and then read the disk into it. 

Good luck,
-- Ken


-- 
Ken Johnson, AI Applications Institute, 80 South Bridge, Edinburgh EH1 1HN
E-mail ken@aiai.ed.ac.uk, phone 031-225 4464 extension 212
`I have read your article, Mr.  Johnson, and I am no wiser than when I
started.' -- `Possibly not, sir, but far better informed.'