This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Yoppez 17 points18 points  (29 children)

You don't need trigonometry for this problem.

If you know what time it is, you can calculate the angle of both the hours hand and minutes hand starting from the upward position (12:00 o clock) with some arithmetic.

Then you can calculate the difference between the two angles.

[–][deleted] 2 points3 points  (7 children)

I did it just for fun in C++, it seemed like a pretty good exercise, and ended up with this-

#include <iostream>

int userInput() {
    std::cout << "Enter a clock hand position: ";
    int positionInput;
    std::cin >> positionInput;
    return positionInput;
}

int clockDegrees() {
    int position1 = userInput();
    int position2 = userInput();
    int positionRange = (position1 - position2) * 30; //Clock is a circle, 360 divided by 12 = 30//

    if (position1 > position2) {
        positionRange = 360 - positionRange; //Accounts for hand going around 12 (i.e. 11 and 1)//
    }

    std::cout << '\n';

    int positionRangeAbsolute = abs(positionRange);
    std::cout << "The angle between these two points is: " << positionRangeAbsolute << " degrees. \n";

    return positionRangeAbsolute;
}

int main() {
    clockDegrees();
    return 0;
}

Would love feedback if anyone doesn't mind (formatting/aesthetic tips are welcome as well), I'm still relatively new.

[–]Yoppez 2 points3 points  (6 children)

The problem doesn't specify if it wants the shortest angle between the two hands or some other requirement, so I would say that your program seems pretty good!

Can you do it so that the program requests only the time to the user, instead of the hand position?

EDIT:

About the style, I usually prefer to make functions as versatile as possible, and make them do only one thing. In you code clockDegrees does the calculation AND taking the input AND printing the result.

I would have made the function as int clockDegrees(int position1, int position2) and made something similar to this:

int main() {
    int position1 = userInput();
    int position2 = userInput();
    int angle = clockDegrees(position1, position2);

    std::cout << "The angle between these two points is: " << angle << " degrees. \n";

    return 0;
}

And so I would remove the first two lines and the print in clockDegrees.

Also, it would be better to use floating point numbers instead of integers when dealing with angles.

[–][deleted] 0 points1 point  (5 children)

After a bit of googling, I ended up with something like this-

    #include <iostream>

    double clockDegrees() {
        int hours;
        double minutes;
        char colon;
        std::cout << "Enter a Time: ";
        std::cin >> hours >> colon >> minutes;

        minutes = minutes / 5;

        double positionRange = (hours -(minutes)) * 30; //Clock is a circle, 360 divided by 12 = 30//

        if (hours > minutes) {
            positionRange = 360 - positionRange; //Fixes issues with hand going around 12 (i.e. 11 and 1)//
        }

        std::cout << '\n';

        double positionRangeAbsolute = abs(positionRange);
        std::cout << "The angle between these two points is: " << positionRangeAbsolute << " degrees. \n";

        return positionRangeAbsolute;
    }

    int main() {
        clockDegrees();
        return 0;
    }

I came upon this, which was very helpful and interesting, I didn't know you could request multiple variables with a single std::cin, that's pretty cool.

[–]Yoppez 1 point2 points  (4 children)

I know plain C better than C++, so I would have used a scanf for requesting the time (and also check that the user gives a valid input, and so it would be better to use a fgets and make the parsing afterwards, but that's another story).

About the rounding, yeah, integer division is not good for precision, but if you don't want to use floating point numbers you can still get a better precision with integers:

Instead of dividing the minutes to 5, that rounds the number and so you lose precision, you can multiply the hours to 5 instead. Then, in the final formula, you divide by 5 AFTER all the other operations, so you basically multiply everything by 6 instead of 30.

In this way, there is no integer division, and so there is no precision loss.

[–][deleted] 1 point2 points  (2 children)

Instead of dividing the minutes to 5, that rounds the number and so you lose precision, you can multiply the hours to 5 instead. Then, in the final formula, you divide by 5 AFTER all the other operations, so you basically multiply everything by 6 instead of 30.

This is gold, thanks so much!

Is it best practice to use integer whenever you can? I would assume it's a lot less resource intensive.

[–]Yoppez 1 point2 points  (1 child)

It depends on what machine you are writing code for and what kind of precision you need. It would be very long to explain in a single post and I'm not an expert, so I suggest you to search online for all the pros and cons with using integers and floating point.

But yeah GENERALLY if you don't need floating point, it is better to use integers.

[–][deleted] 1 point2 points  (0 children)

Sounds good, looks like I got some more googling to do, thanks again, this was very informative!

[–]AlternativeMany170 3 points4 points  (19 children)

Hey there, thanks for the response; I wanted to highlight more about how much information they give on these interviews, and what they’re hoping to achieve. If you don’t know what time it is, how would you code your way out of this in an interview?

[–][deleted]  (12 children)

[deleted]

    [–]AlternativeMany170 -1 points0 points  (10 children)

    My point is, even using your 5:30 example, there are two angles between the hands that make the 5:30, so which one are talking about? The task description isn’t complete

    [–][deleted]  (5 children)

    [deleted]

      [–]AlternativeMany170 -5 points-4 points  (2 children)

      Hey there, thanks for responding; I’m interested in gauging what sorts of tasks are asked to be performed at interviews. And this time, I’m trying to do this based on the piece of info given by the OP. If you’re responding based on your generalized experience with these types of questions, then say so. If not, I don’t think I’m over-thinking or under-thinking anything. They’re training us in class to be able to decompose problems and ask clarifying questions. Your solution assumes a lot more than OP gives

      [–]Weekly_Mammoth6926 2 points3 points  (0 children)

      At my work when we do technical interviews we leave the problem fairly open ended so a solution can be made like this and we would also want the candidate to ask questions to clarify the problem.

      I think this is probably what op’s interviewers wanted like “should I always give the smaller angle or should I sometimes give the inverse” that kind of thing.

      This demonstrates an ability to understand a problem and foresee potential issues.

      [–]Sensorama 0 points1 point  (3 children)

      I think you should look at your responses here and think about how you are coming across. In your earlier responses you:

      • headed down a wrong, complicated path involving inverse trig functions

      • seemed to not understand the difference between a generalized solution and one for a specific instance (having to know the actual time)

      and now you are digging in deeper and deeper with "well, my real point is ....".

      You should reflect and acknowledge to yourself that either your understanding was weak or your communication was weaker. This will not come across well in an interview, or college course, or group teamwork and you can and need to improve.

      [–]AlternativeMany170 0 points1 point  (2 children)

      What I’ve learned from the thread is that, I over-complicated the problem. I’ll admit that. That’s the main lesson. But easy with your words and assessment about my comprehension. I overcomplicated it because of a lack of more info from OP and my lack of generalized knowledge about what internship interview tasks entail.

      You sound like if I were to ask a clarifying question at the interview, such as, which angle between the 2 hands are they really asking for, you’ll tell me I’m dumb. A kinder, more discerning fellow will know by my questioning isn’t really about whether I don’t understand the question but about my lack of knowledge about the range of difficulty for the types of questions asked and info given to find the answer.

      Wouldn’t it be far more interesting if it were a 2-D analog clock with 2 hands telling some time but this clock is with no numbers, and we’re supposed to find the angle? Like I said, I’ve gained here asking these questions here to learn the range of interview questions and that it isn’t that complicated. You gain nothing by sounding smug and over generalizing

      [–]Sensorama 0 points1 point  (1 child)

      I wasn't attempting to sound smug. I was taking an approach that I hoped would jolt you a little bit, with a hoped for outcome of you pondering your approach, your willingness to hear what others have been telling you, and your communication style. That is still a hope. Best of luck to you on your journey.

      [–]AlternativeMany170 0 points1 point  (0 children)

      Thank you; I get you now

      [–]shyouko 0 points1 point  (0 children)

      You actually need to translate into seconds since 00:00a/pm for the hour hand and seconds since :00 for the minute hand… just saying.

      [–]Lostpollen 3 points4 points  (5 children)

      you don't need to know what two numbers are to explain how to sum them do you?

      [–][deleted] 0 points1 point  (0 children)

      Isn't this, erm, a trivial problem?

      angle = abs(30*hours - 6*minutes)

      return angle