How to resolve the algorithm Averages/Mean angle step by step in the ALGOL 68 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Mean angle step by step in the ALGOL 68 programming language

Table of Contents

Problem Statement

When calculating the average or mean of an angle one has to take into account how angles wrap around so that any angle in degrees plus any integer multiple of 360 degrees is a measure of the same angle. If one wanted an average direction of the wind over two readings where the first reading was of 350 degrees and the second was of 10 degrees then the average of the numbers is 180 degrees, whereas if you can note that 350 degrees is equivalent to -10 degrees and so you have two readings at 10 degrees either side of zero degrees leading to a more fitting mean angle of zero degrees. To calculate the mean angle of several angles:

(Note that, since the mean is the sum divided by the number of numbers, and division by a positive real number does not affect the angle, you can also simply compute the sum for step 2.) You can alternatively use this formula:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Mean angle step by step in the ALGOL 68 programming language

Source code in the algol programming language

#!/usr/bin/a68g --script #
# -*- coding: utf-8 -*- #

PROC mean angle = ([]#LONG# REAL angles)#LONG# REAL:
(
  INT size = UPB angles - LWB angles + 1;
  #LONG# REAL y part := 0, x part := 0;
  FOR i FROM LWB angles TO UPB angles DO
      x part +:= #long# cos (angles[i] * #long# pi / 180);
      y part +:= #long# sin (angles[i] * #long# pi / 180)
  OD;
 
  #long# arc tan2 (y part / size, x part / size) * 180 / #long# pi
);
 
main:
(
  []#LONG# REAL angle set 1 = ( 350, 10 );
  []#LONG# REAL angle set 2 = ( 90, 180, 270, 360);
  []#LONG# REAL angle set 3 = ( 10, 20, 30);
 
  FORMAT summary fmt=$"Mean angle for "g" set :"-zd.ddddd" degrees"l$;
  printf ((summary fmt,"1st", mean angle (angle set 1)));
  printf ((summary fmt,"2nd", mean angle (angle set 2)));
  printf ((summary fmt,"3rd", mean angle (angle set 3)))
)

  

You may also check:How to resolve the algorithm Create an object at a given address step by step in the S-BASIC programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the Raven programming language
You may also check:How to resolve the algorithm Abundant, deficient and perfect number classifications step by step in the Ceylon programming language
You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the ScriptBasic programming language