Construct a C++ program that determines the position of the hour and minute hand
ID: 3874134 • Letter: C
Question
Construct a C++ program that determines the position of the hour and minute hands of an analog clock at a moment in time. Using a 12-hour clock (where the hour hand moves between the integers 1 thru 12 and the minute hand moves between the integers 0 and 59), read the current clock time from the user and then read a time increment to add to the current time. For this second time, calculate where the location of the hour and minute hand will be. Please include at least one const int declaration. Your program should allow the user to repeat this calculation as often as the user wishes.
The following is a sample program(User input is in bold) :
Enter the current hour (1-12): 1
Enter the current minute (0-59): 10
Enter the desired time increment (in minutes): 6
Based on these values, the hour hand will be at 1 and the minute hand will be at 16
Enter the current hour (1-12): 10
Enter the current minute (0-59): 20
Enter the desired time increment (in minutes): -50
Based on these values, the hour hand will be at 9 and the minute hand will be at 30
Enter the current hour (1-12): 12
Enter the current minute (0-59): 50
Enter the desired time increment (in minutes): 50
Based on these values, the hour hand will be at 1 and the minute hand will be at 40
Please write an explanatory comment after each line of code, thank you in advance.
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int currHr, currMin, ReqMin, ReqHr;
while(1) /*Continue Infintite times. To end press ctrl + C */
{
cout<<"Enter the current hour (1-12):";
cin>>currHr; /* Read the current hour from user */
cout<<"Enter the current minute (0-59):";
cin>>currMin;/*Read the current minute from user */
cout<<"Enter the desired time increment (in minutes):";
cin>>ReqMin; /*Read the required time to increment/decrement */
if(ReqMin < 0) /*conditions when you are trying to decrement the time */
{
if(((currMin + ReqMin) <= 0) && ((currMin + ReqMin) > -60)) /*If the current time is 12:03 and you are trying to decrement by 4 minutes then hours calculation should be decrement by 1 and the minutes is as needed. */
ReqHr = -1; /*Required hours is decremented by 1 */
else
ReqHr = ((-currMin+ReqMin)/60); /*Else the required hours calculation is based on total minutes you wanted to decrement modulo 60 */
}
else
ReqHr = ((currMin+ReqMin)/60); /*If you want to increment then hours will be total minutes modulo 60 */
ReqMin = (ReqMin%60); /*Required minutes should always be modulo with 60 as we have already taken care the hours part */
if((currHr + ReqHr) <= 0) /*If the current hours + Required hours is less than or equal to zero, then add 12 to get the correct hour */
currHr = 12 + currHr + ReqHr;
else if((currHr + ReqHr) > 12) /* If current hours + Required hours is more than 12 then decrement hours by 12 to get the correct hour */
currHr = currHr + ReqHr - 12;
else /* Else you just add the hours normally. */
currHr = currHr + ReqHr;
if((currMin + ReqMin) < 0) /*If the current minutes + Required minutes is less than or equal to zero, then add 60 to get the correct minutes */
currMin = 60 + currMin + ReqMin;
else if((currMin + ReqMin) >= 60) /* If current minutes + Required minutes is more than 60 then decrement minutes by 60 to get the correct minutes */
currMin = currMin + ReqMin - 60;
else /* Else you just add the minutes normally. */
currMin = currMin + ReqMin;
cout<<"Based on these values, the hour hand will be at "<<currHr<<" and the minute hand will be at "<<currMin<<endl;
}
return 0;
}