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

Source code in the visual programming language

Module Module1

    Function TimeToDegrees(time As TimeSpan) As Double
        Return 360 * time.Hours / 24.0 + 360 * time.Minutes / (24 * 60.0) + 360 * time.Seconds / (24 * 3600.0)
    End Function

    Function DegreesToTime(angle As Double) As TimeSpan
        Return New TimeSpan((24 * 60 * 60 * angle \ 360) \ 3600, ((24 * 60 * 60 * angle \ 360) Mod 3600 - (24 * 60 * 60 * angle \ 360) Mod 60) \ 60, (24 * 60 * 60 * angle \ 360) Mod 60)
    End Function

    Function MeanAngle(angles As List(Of Double)) As Double
        Dim y_part = 0.0
        Dim x_part = 0.0
        Dim numItems = angles.Count

        For Each angle In angles
            x_part += Math.Cos(angle * Math.PI / 180)
            y_part += Math.Sin(angle * Math.PI / 180)
        Next

        Return Math.Atan2(y_part / numItems, x_part / numItems) * 180 / Math.PI
    End Function

    Sub Main()
        Dim digitimes As New List(Of Double)
        Dim digitime As TimeSpan
        Dim input As String

        Console.WriteLine("Enter times, end with no input: ")
        Do
            input = Console.ReadLine
            If Not String.IsNullOrWhiteSpace(input) Then
                If TimeSpan.TryParse(input, digitime) Then
                    digitimes.Add(TimeToDegrees(digitime))
                Else
                    Console.WriteLine("Seems this is wrong input: ingnoring time")
                End If
            End If
        Loop Until String.IsNullOrWhiteSpace(input)

        If digitimes.Count > 0 Then
            Console.WriteLine("The mean time is : {0}", DegreesToTime(360 + MeanAngle(digitimes)))
        End If
    End Sub

End Module


  

You may also check:How to resolve the algorithm String length step by step in the Elena programming language
You may also check:How to resolve the algorithm Bitmap/Bézier curves/Cubic step by step in the Racket programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the Action! programming language
You may also check:How to resolve the algorithm Pick random element step by step in the Euphoria programming language