Help with beginner problem

So I'm trying to code this program but the code I keep trying is wrong or just doesn't give me the right answers. I need to code a program that will compute the angles of clock hands in military time. The angles that I need are:
Angle between Second and Minute hands
Angle between Minute and Hour hands

Personally I think my equation is wrong but I'm having trouble fixing it. Can someone tell me what is wrong with my code and give me some advice on how to fix it? Thank you in advance for any help!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	double hour, hour_ang, min, min_ang, sec, sec_ang, sec_min_angle, hour_min_angle;

	hour = 0;
	min = 0;
	sec = 0;

	cout << fixed;
	cout << showpoint;
	cout << setprecision(4);

	cout << "Please enter the hour [0-23]: ";
	cin >> hour;

	cout << "Please enter the minute [0-59]: ";
	cin >> min;

	cout << "Please enter the seconds [0-59]: ";
	cin >> sec;

	if (hour>12)
	{
		hour = hour - 12;
	}
	
	hour_ang = hour * 30 + ((min / 60) * 6) + ((sec / 3600) * 6);
	min_ang = min*6.0 + (sec*0.6);
	sec_ang = sec*6.0;
	
	if (sec_ang < min_ang)
	{
		sec_min_angle = abs(min_ang - sec_ang);
	}
	else
		sec_min_angle = abs(sec_ang - min_ang);

	if (hour < min)
	{
		hour_min_angle = abs(min_ang - hour_ang);
	}
	else
		hour_min_angle = abs(hour_ang - min_ang);

	cout << "Angle between the Second and Minute Hands: " << sec_min_angle << endl;
	cout << "Angle between the Minute and Hour Hands: " << hour_min_angle << endl;


}
Hey

First of all I am completely new here, so my comment might not have much value, but...

If I input time 18:30:00 I should get 180 to the first and 0.00 to the second. The second answer gives incorrectly 3 degrees.

However if you delete
((min / 60) * 6) + ((sec / 3600) * 6);
(sec*0.6);

and make it (in line 32-34)

hour_ang = hour * 30;
min_ang = min*6.0;
sec_ang = sec*6.0;

the answers will get back 180 degrees and 0 degrees, which is correct to time 18:30:00 right?

Hope that helps,

Greetings,
Cortega
Last edited on
Switching the equations does work, but it works for most generic angles. If I enter a complicated angle however, I don't get decimals in my angles. It just rounds to whole numbers which doesn't really make sense. I'm not sure why it is doing that.
Last edited on
Ah I understand now.

When we look at seconds we divide the clock in 360 degrees. making 45 seconds at 270 degrees.

But for your wanted calculation to work the seconds need to be seen as the distance between 1 min and the next one min.

so place hour hand at 1 oclock and place the next hand 10.5 degrees lower and you have a hand that is 1min45 sec lower. but if you enter time 1h6min45 sec you get a different output. because when you place a hand 45 sec lower it is 3/4th of a min in distance but you told the program that 45 sec is not 3/4th of a min but infact 3/4th of the entire clock....

but i dont know how to solve that either, will keep thinking about it...
Ok thank you. Yeah it's really stumping me.
is your clock "smooth"? (does the minute hand "tick" between minutes or sweep?)

at half past the hour, do you expect the hour hand to be halfway between the hours?
at half past the minute, do you expect the minute hand to be halfway between the minutes?


maybe you need to add something not just hour, sec, min, but a new unit like a realminute. where the realminute is capable of using seconds without looking at the actual secondshand

first convert sec to percentage_of_minute

then the real_minute = minute + percentage_minute

while the hand of the seconds is at 45 sec/270 degrees, the hand of the actual time can only be at either 6 or 7 but the hand of the real_min can be between 6 and 7.

at 45 sec, the second hand would be at 270%, but it would also be represented between 6 and 7. 45 sec is 75% of min
real minute would be 0.75 min

the min hand at 6, but the real_min hand can be at 6,75. the reason why you would keep the min hand in addition to the real min hand would be to calculate the real_min hand in the first place...

I dont know how to do that in code, its my second day learning about C++, and it is probably not the prettiest way to code it.


Yes the clock does sweep, so yes we expect the hands to be halfway when the times are half past.
first of all, when coding equations define a constant for EVERYTHING, it will stop your brain hurting later.
1
2
3
4
5
    const double degPerSec  = 360.0 / 60.0;
    const double degPerMin  = 360.0 / 60.0;
    const double degPerHour = 360.0 / 12.0;
    const double secsPerMin = 60.0;
    const double minsPerHour = 60.0;


then tackle the problem from the bottom up, by that i mean seconds influece minutes influence hours so calculate them in that order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    // the easy bit    
    double sec_angle = sec * degPerSec;
    
    // calculate the partial minute that seconds equates to.
    double partialMin = sec / secsPerMin;
    
    // then take it into account when calculating the minute angle.
    double min_angle = (min+partialMin) * degPerMin;
    
    // calculate the partial hour that all minutes equate to.
    double partialHour = (min+partialMin) / minsPerHour;
    
    // then take it into account when calculating the hour angle.
    double hour_angle = (hour+partialHour) * degPerHour;


I'll leave the angles you you :)
cool stuff Jaybob66!

If you are done with the complete code Spaceman Zeta would you be so kind to post it here or pm it? I wont use it for anything ofcourse but I really really really want to learn from this example.

Good luck :)

@Cortega
The only learning that needs to be done is how to approach a problem.

1. always use const, it removes "magic numbers" from your code and stops you thinking at the micro level.

2. break it down.

3. find the dependencies and take the bottom first so you can build on it

when you read my code it's easy, because it follows common sense, you dont have to concentrate on the syntax of the language as the code is more or less "human readable" by that i mean you could read it out loud and it would make sense to a listener.

GL/HF

NB: You aleady identified the key points in your post, all i can add to that is dont mix it up, adding percentages to the mix added complexity.
Last edited on
Thank you very much! I've implemented your ideas into my code and it works perfectly!
sweet :)
Topic archived. No new replies allowed.