Averages/Mean time of day: Difference between revisions

Added solution for Action!
m (→‎{{header|Nim}}: Update code to work with newer versions, remove reimplementations of stdlib functions)
(Added solution for Action!)
Line 44:
{{out}}
<pre>23:47:43</pre>
 
=={{header|Action!}}==
Action! does not support trigonometry, therefore a simple arithmetic solution has been proposed.
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<lang Action!>INCLUDE "H6:REALMATH.ACT"
 
DEFINE PTR="CARD"
TYPE Time=[BYTE s,m,h]
REAL r60
 
PROC PrintB2(BYTE b)
IF b<10 THEN Put('0) FI
PrintB(b)
RETURN
 
PROC PrintTime(Time POINTER t)
PrintB2(t.h) Put(':)
PrintB2(t.m) Put(':)
PrintB2(t.s)
RETURN
 
PROC Decode(CHAR ARRAY st Time POINTER t)
CHAR ARRAY tmp
 
IF st(0)#8 THEN Break() FI
SCopyS(tmp,st,1,2) t.h=ValB(tmp)
SCopyS(tmp,st,4,5) t.m=ValB(tmp)
SCopyS(tmp,st,7,8) t.s=ValB(tmp)
RETURN
 
PROC TimeToSeconds(Time POINTER t REAL POINTER seconds)
REAL r
 
IntToReal(t.h,seconds)
RealMult(seconds,r60,seconds)
IntToReal(t.m,r)
RealAdd(seconds,r,seconds)
RealMult(seconds,r60,seconds)
IntToReal(t.s,r)
RealAdd(seconds,r,seconds)
RETURN
 
PROC SecondsToTime(REAL POINTER seconds Time POINTER t)
REAL tmp1,tmp2
 
RealAssign(seconds,tmp1)
RealMod(tmp1,r60,tmp2)
t.s=RealToInt(tmp2)
RealDivInt(tmp1,r60,tmp2)
RealMod(tmp2,r60,tmp1)
t.m=RealToInt(tmp1)
RealDivInt(tmp2,r60,tmp1)
t.h=RealToInt(tmp1)
RETURN
 
PROC AverageTime(PTR ARRAY times BYTE count Time POINTER res)
BYTE i
Time t
REAL avg,rcount,seconds,halfDay,day
 
IntToReal(0,avg)
IntToReal(count,rcount)
ValR("43200",halfDay) ;seconds in the half of day
ValR("86400",day) ;seconds in the whole day
FOR i=0 TO count-1
DO
Decode(times(i),t)
TimeToSeconds(t,seconds)
IF RealLess(seconds,halfDay) THEN
RealAdd(seconds,day,seconds) ;correction of time
FI
RealAdd(avg,seconds,avg)
OD
RealDivInt(avg,rcount,avg)
WHILE RealGreaterOrEqual(avg,day)
DO
RealSub(avg,day,avg) ;correction of time
OD
SecondsToTime(avg,res)
RETURN
 
PROC Main()
DEFINE COUNT="4"
PTR ARRAY times(COUNT)
Time t
 
Put(125) PutE() ;clear the screen
IntToReal(60,r60)
times(0)="23:00:17" times(1)="23:40:20"
times(2)="00:12:45" times(3)="00:17:19"
 
AverageTime(times,COUNT,t)
Print("Mean time is ") PrintTime(t)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Mean_time_of_day.png Screenshot from Atari 8-bit computer]
<pre>
Mean time is 23:47:40
</pre>
 
=={{header|AutoHotkey}}==
Anonymous user