## Clock Angles

### July 1, 2016

We have a fun little program for a lazy summer Friday:

Write a program that, given a time as hours and minutes (using a 12-hour clock), calculates the angle between the two hands. For instance, at 2:00 the angle is 60°.

Your task is to write a program that calculates clock angles. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Pages: 1 2

Here’s my solution in APL.

ANGLE←{|((⍵+⍺×60)×0.5)-⍵×6}

Here is my take on the problem, using Julia:

function convert_to_num{T <: AbstractString}(A::Array{T, 1})

n = length(A)

Z = zeros(n)

for i = 1:n

m = length(A[i])

e = 0

for j = m:-1:1

Z[i] += (Float64(A[i][j]) – 48)*10^e

e += 1

end

end

return Z

end

function main(z::AbstractString)

max_minutes = 720.0

Z = convert_to_num(split(z, ":"))

total_minutes = 60Z[1] + Z[2]

angle_small_hand = 360total_minutes / max_minutes

angle_large_hand = 6Z[2]

temp = abs(angle_small_hand – angle_large_hand)

return min(temp, 360.0 – temp)

end

BTW, the solution in the previous comment is off, since the angle of the hands is by default less or equal to 180 deg, while that solution allows for angles larger than 180 deg.

A Haskell function.

The solution page says:

> (angle 3 45)

157.5

I’d have guessed 180.

@fd: At 3:45, the minute hand is on the 9, but the hour hand has moved past the 3 and is 3/4 of the way to 4, making the angle less than 180°.

Here’s an updated Haskell version, taking into account the movement of the hour hand as mentioned above.

import java.util.Scanner;

public class clock_angles {

public static void main(String[] args) {

Scanner s = new Scanner(System.in); // TODO Auto-generated method stub

double hh,mm;System.out.println(“enter hour”);hh=s.nextDouble();System.out.println(“enter min”);mm=s.nextDouble(); double h=hh+mm/60;

double dh=360*h/12;

double m=hh*60+mm;

double mh=360*mm/60;

System.out.println(Math.abs(dh-mh));

}

}

#include

#include

#include

#include

using namespace std;

//Function incorporating Solution.

void Solution(void);

int main(void)

{

Solution(); return 0;

}

class Angular_Time

{

public:

//Function to Parse Time.

void Set_Time(string);

//Function to Compute Angle.

int Get_Angle();

//Constructor.

Angular_Time(string);

private:

//Function to Compute Angle.

void Compute_Angle();

//Function to return int Equivalent.

int toInt(char);

//For Storing Time.

int Hours, Minutes;

//For Easier Parsing.

string TimeStr;

//Angle.

int Angle;

//for Computations.

const int Hour_Jump, Minute_Jump;

};

void Solution(void)

{

//Gettting Time.

string TakeTime;

cin >> TakeTime;

//Angle Object.

Angular_Time MyAngle(TakeTime);

//Result.

cout << MyAngle.Get_Angle() << endl;

}

//**/Class Member Functions.//**/

void Angular_Time::Set_Time(string Time)

{

//For Reference.

(*this).TimeStr = Time;

//Finding Hours and Minutes;

if (TimeStr.length() == 4)

{

(*this).Hours =

toInt(TimeStr[0]);

(*this).Minutes =

toInt(TimeStr[3]) + toInt(TimeStr[2]) * 10;

}

else

{

(*this).Hours =

toInt(TimeStr[1]) + toInt(TimeStr[0]) * 10;

(*this).Minutes =

toInt(TimeStr[4]) + toInt(TimeStr[3]) * 10;

}

//Computing Angle.

(*this).Compute_Angle();

}

void Angular_Time::Compute_Angle()

{

//Computing Angles for Hours.

int Hour_Angle =

(*this).Hours*Hour_Jump;

//Computing Angles for Minutes.

int Min_Angle =

(*this).Minutes*Minute_Jump;

int Differernce = abs(Hour_Angle – Min_Angle);

//Edge Case.

(Differernce == 360) ?

((*this).Angle = 0) : ((*this).Angle = Differernce);

}

int Angular_Time::toInt(char ConvChar)

{

return int(ConvChar) – 48;

}

int Angular_Time::Get_Angle()

{

return (*this).Angle;

}

Angular_Time::Angular_Time(string TimeStr)

:Hour_Jump(30), Minute_Jump(6)

{

(*this).Set_Time(TimeStr);

}

Somewhat shorter in Forth in one someway

The solution was reach by considering the problem as a whole (both equations for hours and minutes) and then simplified the resultant equation using math. This was done since the goal was only finding angles (not how to calculate them in stages – hours, minutes then the abs diff – but YMMV)

from Wikipedia https://en.wikipedia.org/wiki/Clock_angle_problem

hours = 0.5(60H + M)

minutes = 6M

Thus angle will be (without considering any hand as a reference)

0.5(60H + M) – 6M

30H + 0.5M – 6M

30H – 5.5M

30H – 11/2M

since we are using absolute value after difference then we can reorder ’30H – 11/2M’ to ’11/2M – 30H’

’12 mod’ for when clock is 12

The solution is at http://ideone.com/Ah603B

class ClcokAngle

{

double angle;

public:

int min;

int hour;

ClcokAngle()

{

min=0;

hour=0;

angle=0;

}

ClcokAngle(int min,int hour)

{

this->min=min;

this->hour=hour;

this -> angle=0;

}

void Setmin(int min)

{

this->min=min;

}

void Sethour(int hour)

{

this->hour=hour;

}

int getmin()

{

return this->min;

}

int gethour()

{

return this->hour;

}

double GetAngle()

{

angle = 0.5*((60*hour)-(11*min));

if (angle < 180)

{

return angle;

}

else

{

angle = 360-angle;

return angle;

}

}

};//end of class