Lesson 5 (Exercise 3)

Angle between the hour and the minute hand of a clock

From CS100 Summer Homework

文档内容

Older mechanical clocks do not only move the second or minute hands continuously at every second, but even the hour hand. The task of this exercise is straightforward. Given a time (in 24-­‐hour format and with minute precision, as in 00:12, 13:04, etc.), write a C program that calculates the angle between the hour and the minute hand (in degrees).
Examples input/output sessions with the program are given as follows:

What time is it? 10:10
The angle between the hands is 115.0 degrees.

What time is it? 21:45
The angle between the hands is 22.5 degrees.

The red text represents the user input. Further notes on correctness:

  • The angle refers to the one that is less than 180 degrees in absolute terms (and should be reported without sign).
  • Knowing that the time is in 24-­‐hour format, you may have to deal with the hours upfront.
  • Please format the output with one decimal digit.

It may be useful to familiarize yourself with the math library and some of its functions. Add #include <math.h> to the top of your program to include the math functions, and— depending on your system and compiler—you may also have to add the option –lm to your compiler command to activate the math functions. Some useful commands are:

int remainder = var1 % var2;	//Takes var1 modulo var2,
				//i.e. the remainder of
				//the division of var1 by var2
fabs(var1);			//Takes the absolute value of
			    	//a floating point variable

Again, consider the input/output examples on the webpage to check the correctness of your code, and follow the instructions on the webpage to login to our online grading system and submit your solution there. Please follow the exact instructions on how to register such that we are able to unambiguously identify you. This is very important as the summer-­‐ homeworks will be graded and already make up for a small part of your overall grade.

在线测试 (语言:C;时间限制:1s)
当前状态

无提交记录

有话想说?