How to resolve the algorithm Averages/Mean time of day step by step in the Delphi 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 Delphi 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 Delphi programming language

Source code in the delphi programming language

program Averages_Mean_time_of_day;

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  System.Math;

const
  Inputs: TArray = ['23:00:17', '23:40:20', '00:12:45', '00:17:19'];

function ToTimes(ts: TArray): TArray;
begin
  SetLength(result, length(ts));
  for var i := 0 to High(ts) do
    Result[i] := StrToTime(ts[i]);
end;

function MeanTime(times: TArray): TTime;
var
  ssum, csum: TTime;
  h, m, s, ms: word;
  dayFrac, fsec, ssin, ccos: double;
begin
  if Length(times) = 0 then
    exit(0);

  ssum := 0;
  csum := 0;
  for var t in times do
  begin
    DecodeTime(t, h, m, s, ms);
    fsec := (h * 60 + m) * 60 + s + ms / 1000;
    ssin := sin(fsec * Pi / (12 * 60 * 60));
    ccos := cos(fsec * Pi / (12 * 60 * 60));
    ssum := ssum + ssin;
    csum := csum + ccos;
  end;
  if (ssum = 0) and (csum = 0) then
    raise Exception.Create('Error MeanTime: Mean undefined');

  dayFrac := frac(1 + ArcTan2(ssum, csum) / (2 * Pi));
  fsec := dayFrac * 24 * 3600;

  ms := Trunc(frac(fsec) * 1000);
  s := trunc(fsec) mod 60;
  m := trunc(fsec) div 60 mod 60;
  h := trunc(fsec) div 3600;

  Result := EncodeTime(h, m, s, ms);
end;

begin
  writeln(TimeToStr(MeanTime(ToTimes(Inputs))));
  readln;
end.


  

You may also check:How to resolve the algorithm Queue/Definition step by step in the R programming language
You may also check:How to resolve the algorithm Chinese zodiac step by step in the Go programming language
You may also check:How to resolve the algorithm Terminal control/Ringing the terminal bell step by step in the gnuplot programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the Gema programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Logo programming language