You are asked to find the angle between the minute hand and the hour hand on a r
ID: 3848942 • Letter: Y
Question
You are asked to find the angle between the minute hand and the hour hand on a regular analog clock. Assume that the second hand, if there were one, would be pointing straight up at the 12. Give all angles as the smallest positive angles. For example 9: 00 is 90 degrees: not -90 or 270 degrees. Input: The input is a list of times in the form H: M, each on their own line, with 1 lessthanorequalto H lessthanorequalto 12 and 00 lessthanorequalto M lessthanorequalto 59. The input is terminated with the time '0: 00'. Note that H may be represented with 1 or 2 digits (for 1-9 or 10-12, respectively): M is always represented with 2 digits (the input times are what you typically see on a digital clock). Output: The output displays the smallest positive angle in degrees between the hands for each time. The answer should between 0 degrees and 180 degrees for all input times. Display each angle on a line by itself in die same order as the input. The output should be rounded to the nearest 1/1000, i.e., three places after the decimal point should be printed.Explanation / Answer
Solution:-
The below given C code calculates angle between hour hand and minute hand of an analog watch. The function calculates calcAngle calculates the angle. A driver program also there to test the program.
---------------------------------------------------------------------------------------------------
// Calculate angle between hour and minute hand of an // analog watch
#include <stdio.h>
#include <stdlib.h>
// function to find minimum of two integers
int min(int x, int y) { return (x < y)? x: y; }
int calcAngle(double h, double m)
{
// Input validation
if (h <0 || m < 0 || h >12 || m > 60)
printf("Wrong input");
if (h == 12) h = 0;
if (m == 60) m = 0;
// Calculating angles of hour and minute hands
// with reference to 12:00
int hour_angle = 0.5 * (h*60 + m);
int minute_angle = 6*m;
// Find the difference between two angles
int angle = abs(hour_angle - minute_angle);
// Return the smaller angle of two possible angles
angle = min(360-angle, angle);
return angle;
}
// Driver program to test above function
int main()
{
printf("%d ", calcAngle(9, 60));
printf("%d ", calcAngle(3, 30));
return 0;
}
---------------------------------------------------------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.