February 7th, 2013, 11:09 PM

Clocks!! please help!!
Time can be stated in 24 hour (Military) format. For example, 5 minutes after one o'clock in the afternoon can be stated as 13:05 hrs. If we include seconds, then 5 minutes and 24 seconds after one o'clock in the afternoon could be stated as 13:05:24 hrs.
The range for the hour portion of the time is from 00 to 23 while for the minute and second, the range is from 00 to 59.
Note that 00:00:00 hrs represents midnight while 12:00:00 represents noon.
Also note that the hour, minute and second are each represented by two digits and are delimited by colons (:).
For more information on this time format please visit this link.
•24 hour clock
Specifications
Your program prompts for and accepts the following information:
•the time on the first clock
•the time on the second clock
•the operation to perform
◦a plus (+) sign means add clock two to clock one giving clock three
■Adding clock two to clock one implies moving clock one forward by the amount of time shown on clock two.
◦a minus () sign means subtract clock two from clock one giving clock three
■Subtracting clock two from clock one implies moving clock one backward by the time shown on clock two.
EXAMPLEs
00:00:00  00:00:01 = 23:59:59 01:01:01  23:59:59 = 01:01:02 01:01:01  00:00:02 = 01:00:59 23:59:59 + 00:00:01 = 00:00:00 01:01:59 + 00:00:01 = 01:02:00 01:01:01 + 23:59:59 = 01:01:00
You may assume that the user shall input this information correctly.
Design your program so that it displays clock three, the result of adding or subtracting clocks.
So far I have:
main() {
int hr1, min1, sec1, hr2, min2, sec2, hr3, min3, sec3, input;
printf("Enter time one, seperated by colons: ");
scanf("%d:%d%d", &hr1, &min1, &sec1);
printf("Enter time two, seperated by colons: ");
scanf("%d:%d:%d", &hr2, min2, sec2);
printf("Enter 1 to add the two times or enter 2 to subtract the two times: ");
scanf("%d", &input);
}
and thats all i have.
i would really appreciate any help from anyone asap
thankyou!
February 8th, 2013, 12:43 AM

Do you know how many seconds there are in a minute?
Do you know how many seconds there are in an hour?
Convert each hh:mm:ss into a total number of seconds elapsed.
Do the math, then extract hh:mm:ss from the result.
February 8th, 2013, 03:06 PM

?
i really dont understand. could you explain it further or give me an example ?
February 8th, 2013, 03:18 PM

What part do you not understand? What exactly is your question? Where are you having a problem?
Until we know those things, we won't be able to help. You need to help us help you.
PS
salem was talking about converting hours:minutes:seconds to one common unit, seconds. After having converted both times to seconds, do the math and then convert the result back to HH:MM:SS notation.
Or you could just treat the times as sexagesimal (base 60) numbers with three digits: hours, minutes, seconds. Do the math directly, then correct the result (if the resultant digit is negative, then borrow 1 from the next higher digit; if the resultant digit is 60 or greater then carry 1 to the next higher digit).
Both approaches are very simple and straightforward. So much so that we find it very difficult to imagine what anybody would not understand about it. If you still do not understand, then explain to us very specifically what it is that you don't understand.
Last edited by dwise1_aol; February 8th, 2013 at 03:25 PM.
February 8th, 2013, 03:24 PM

so far i have:
So far I have:
main() {
int hr1, min1, sec1, hr2, min2, sec2, hr3, min3, sec3, input;
printf("Enter time one, seperated by colons: ");
scanf("%d:%d%d", &hr1, &min1, &sec1);
printf("Enter time two, seperated by colons: ");
scanf("%d:%d:%d", &hr2, min2, sec2);
printf("Enter 1 to add the two times or enter 2 to subtract the two times: ");
scanf("%d", &input);
}
i need help to start the basic calculations
all i need is an example to get me started
February 8th, 2013, 03:28 PM

This line is incorrect:
scanf("%d:%d:%d", &hr2, min2, sec2);
you left out some ampersands. Compare it to the preceding scanf which was correct.
Give us two test problems, one for adding two times and another for subtracting two times. Then we can show you how to work them out by hand.
Also read the postscript I added to my previous message.
February 8th, 2013, 03:40 PM

i understand what you are saying, but having a bit of trouble on how to input that in my program
example please ?
February 8th, 2013, 04:12 PM

Here's how the standard C library does it in time.h.
C uses UNIX time, which is the number of seconds since 00:00:00 on 01 Jan 1970. When you call time() to get the system's current time, that's what you get. And you also have a struct tm whose fields are hours, minutes, seconds, day, month, year, etc. You have functions which will convert time in seconds since beginning of time (ie, 00:00:00 on 01 Jan 1970) to a struct tm and you have a function that will take a struct tm whose fields you had filled with the appropriate values and it will convert that to time in seconds.
That is salem's approach. Write code that converts from HH:MM:SS to seconds and other code that converts from seconds to HH:MM:SS  personally, I would delegate that code to two functions, but I don't know how far along you are in the course. Then your program will input the two times in HH:MM:SS (which you've already done), convert both to seconds, perform the math, and then convert the result back to HH:MM:SS.
Of course, we cannot simply give you the code to do that. But knowing that there are 3600 seconds in an hour will clue you in on how to convert from hours to seconds, just as knowing that there are 60 seconds in one minute will serve you in converting minutes to seconds. Easypeasy. Then to convert from seconds to hours, etc, would involve using integer division (/) and modulo (%) operations. Start with a modulo60 to get the seconds field, then divide by 60 to see how many minutes that leaves you with, etc. Still easy and it's a technique that you will use in many other programs.
From the examples you gave us (well, that your instructor had given you, but these will be your test data):
EXAMPLEs
00:00:00  00:00:01 = 23:59:59
01:01:01  23:59:59 = 01:01:02
01:01:01  00:00:02 = 01:00:59
23:59:59 + 00:00:01 = 00:00:00
01:01:59 + 00:00:01 = 01:02:00
01:01:01 + 23:59:59 = 01:01:00
Consider 01:01:59 + 00:00:01. Just as you would do in adding two threedigit decimal numbers, add the corresponding digits together, giving you 01:01:60. Now going from right to left, perform the carries. A minutes or seconds digit can only range from 0 to 59, so 60 is too large. Subtract 60 from a MM or SS digit that's too large and add one to the next higher digit, which gives us 01:02:00.
Now a subtraction: 01:01:01  23:59:59. Working from right to left, subtract the seconds, 0159. Since 1 is less than 59, borrow one from the minutes column and add it to the 1, so now we have 6159 which is 2. Now in the minutes column we have 0  59, since we borrowed one for the seconds. Borrow one hour so now we have 6059 which is 1. Now in the hours column we have 0  23, so we borrow one day (which we're not keeping track of in this program) and now have 2423 which is 1. So 01:01:01  23:59:59 = 01:01:02.
Pick your poison. What I just did is simple because it's how we would do it by hand, but the coding could get a bit more involved. Or convert between seconds and HH:MM:SS, which is more computerlike and one that a person might not want to do by hand.
PS
Just to make it clear, you would probably want to go with salem's approach, since it will be easier to code. For those cases where you end up in the following or preceding day, you can determine how many seconds there are in a day by calculating how many seconds there are in 24 hours.
PPS
Please use code tags to post your code; eg:
Code:
int main()
{
int hr1, min1, sec1, hr2, min2, sec2, hr3, min3, sec3, input;
printf("Enter time one, seperated by colons: ");
scanf("%d:%d%d", &hr1, &min1, &sec1);
printf("Enter time two, seperated by colons: ");
scanf("%d:%d:%d", &hr2, min2, sec2);
printf("Enter 1 to add the two times or enter 2 to subtract the two times: ");
scanf("%d", &input);
return 0;
}
Last edited by dwise1_aol; February 8th, 2013 at 04:23 PM.
February 8th, 2013, 04:35 PM

thanks for explaining it a bit more, but i already understood what i have to do.
all i need help with is continuing the program. the actuall calculations.
as i showed i already have this done:
main() {
int hr1, min1, sec1, hr2, min2, sec2, hr3, min3, sec3, input;
printf("Enter time one, seperated by colons: ");
scanf("%d:%d%d", &hr1, &min1, &sec1);
printf("Enter time two, seperated by colons: ");
scanf("%d:%d:%d", &hr2, &min2, &sec2);
printf("Enter 1 to add the two times or enter 2 to subtract the two times: ");
scanf("%d", &input);
}
its not much, but the calculations are usually what prevents me from completing a program
if you can show me how to put that into my code, i would really appreciate it
February 8th, 2013, 04:50 PM

Well all you've managed so far is to basically ignore everyone and reposted the SAME code three times, every time without code tags.
Along with the refrain "please do all my homework for me".
Consider a time like
09:22:47
Can you figure out how many seconds have elapsed since midnight?
If you can, you're more than halfway there.
February 8th, 2013, 04:53 PM

09:22:47
well sorry for bothering everyone, im just really desperate
and as for your question, 33767 seconds
February 8th, 2013, 05:00 PM

I don't see any way I can give you an example without just giving you the code. We are honorbound to not do that, since it doesn't help you learn. The goal is for you to learn.
Think it through and writing down in English (or your native language) what you need to do in each step. For example, to convert from HH:MM:SS to total seconds:
1. Calculate the number of seconds in HH (1 hr == 3600 seconds)
2. Calculate the number of seconds in MM (1 min == 60 seconds)
3. Calculate the number of seconds in SS (1 sec == 1 second)
4. Add together the results of steps 1 through 3 to get the total seconds.
Once you have those steps, you can write the code.
Going from total seconds to HH:MM:SS:
1. Convert the total seconds to minutes by dividing it by the number of seconds in a minute. The remainder of the division, which you get with the modulo (%), is the value of the SS.
NOTE: Do the modulo before you do the divide.
2. Convert the minutes to hours by dividing it by the number of minutes in an hour. Again, the remainder is the value of the MM field.
3. You're left with the hours which go into the HH field.
Simple.
Post your code using code tags.
February 8th, 2013, 05:07 PM

Originally Posted by dwise1_aol
I don't see any way I can give you an example without just giving you the code. We are honorbound to not do that, since it doesn't help you learn. The goal is for you to learn.
Think it through and writing down in English (or your native language) what you need to do in each step. For example, to convert from HH:MM:SS to total seconds:
1. Calculate the number of seconds in HH (1 hr == 3600 seconds)
2. Calculate the number of seconds in MM (1 min == 60 seconds)
3. Calculate the number of seconds in SS (1 sec == 1 second)
4. Add together the results of steps 1 through 3 to get the total seconds.
Once you have those steps, you can write the code.
Going from total seconds to HH:MM:SS:
1. Convert the total seconds to minutes by dividing it by the number of seconds in a minute. The remainder of the division, which you get with the modulo (%), is the value of the SS.
NOTE: Do the modulo before you do the divide.
2. Convert the minutes to hours by dividing it by the number of minutes in an hour. Again, the remainder is the value of the MM field.
3. You're left with the hours which go into the HH field.
Simple.
Post your code using code tags.
basically you said:
3600+60+1 = 3661 total seconds
3661 / 60 = 61.01 minutes
61.01 / 60 = 1.01 hours
am i missing anything ?
February 8th, 2013, 05:37 PM

Originally Posted by mjmjmjmj
basically you said:
3600+60+1 = 3661 total seconds
OK, that would work for 01:01:01, but what about 15:25:29?
Originally Posted by mjmjmjmj
3661 / 60 = 61.01 minutes
61.01 / 60 = 1.01 hours
am i missing anything ?
No, 3661/60 = 61 minutes. No fractional minutes, because that is integer division.
When I went through elementary school, we first learned integer division, in which the quotient was a whole number (what in programming we call an integer) and we would also get a remainder. So 3661 / 60 would give us 61 R 1. But in C the divide and the remainder (AKA "modulo") operations are separate, so you need to do the modulo (%) first (as I NOTEd for you) to get the remainder, because the integer divide will eliminate all trace of any fraction.
C does also do floatingpoint division, which is what you showed me, and it would work here too, except you'd have extra steps you'd have to go through. Here are the general rules for division in C:
For a / b
if a and b are both integer types, then you use integer division.
if either a or b is a floatingpoint type (ie, float or double), then you use floatingpoint division.
For example:
9 / 10 = 0, because it's integer division
9 / 10.0 = 0.9, because it's floatingpoint division
also, 9.0 / 10 = 0.9, and 9.0 / 10.0 = 0.9
and (double)9 / 10 = 0.9.
It will be much easier for you to use integer division. Just remember to do the modulo first and then the divide.
February 8th, 2013, 06:05 PM

Originally Posted by dwise1_aol
OK, that would work for 01:01:01, but what about 15:25:29?
No, 3661/60 = 61 minutes. No fractional minutes, because that is integer division.
When I went through elementary school, we first learned integer division, in which the quotient was a whole number (what in programming we call an integer) and we would also get a remainder. So 3661 / 60 would give us 61 R 1. But in C the divide and the remainder (AKA "modulo") operations are separate, so you need to do the modulo (%) first (as I NOTEd for you) to get the remainder, because the integer divide will eliminate all trace of any fraction.
C does also do floatingpoint division, which is what you showed me, and it would work here too, except you'd have extra steps you'd have to go through. Here are the general rules for division in C:
For a / b
if a and b are both integer types, then you use integer division.
if either a or b is a floatingpoint type (ie, float or double), then you use floatingpoint division.
For example:
9 / 10 = 0, because it's integer division
9 / 10.0 = 0.9, because it's floatingpoint division
also, 9.0 / 10 = 0.9, and 9.0 / 10.0 = 0.9
and (double)9 / 10 = 0.9.
It will be much easier for you to use integer division. Just remember to do the modulo first and then the divide.
okay, now that you've explained the basics, my problem is putting that in to my program.
like i noted before, i understand the question, but i cannot put it into my program.
im not asking you to do my program, but can you give me a rough example on how, for example how seconds would look in a program ?