How to resolve the algorithm Averages/Mean time of day step by step in the Action! programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Mean time of day step by step in the Action! programming language

Table of Contents

Problem Statement

A particular activity of bats occurs at these times of the day: Using the idea that there are twenty-four hours in a day, which is analogous to there being 360 degrees in a circle, map times of day to and from angles; and using the ideas of Averages/Mean angle compute and show the average time of the nocturnal activity to an accuracy of one second of time.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Mean time of day step by step in the Action! programming language

Source code in the action! programming language

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

  

You may also check:How to resolve the algorithm Loops/Foreach step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Combinations step by step in the Picat programming language
You may also check:How to resolve the algorithm Deming's funnel step by step in the Go programming language
You may also check:How to resolve the algorithm Count in factors step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Total circles area step by step in the FreeBASIC programming language