Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 8th, 2013, 12:09 AM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0

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 (.

•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!

#2
February 8th, 2013, 01:43 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
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.
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
February 8th, 2013, 04:06 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
?

i really dont understand. could you explain it further or give me an example ?

#4
February 8th, 2013, 04:18 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,866
Time spent in forums: 3 Months 1 Day 7 h 39 m 48 sec
Reputation Power: 2199
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 straight-forward. 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 04:25 PM.

#5
February 8th, 2013, 04:24 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
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

#6
February 8th, 2013, 04:28 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,866
Time spent in forums: 3 Months 1 Day 7 h 39 m 48 sec
Reputation Power: 2199
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.

#7
February 8th, 2013, 04:40 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
i understand what you are saying, but having a bit of trouble on how to input that in my program

#8
February 8th, 2013, 05:12 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,866
Time spent in forums: 3 Months 1 Day 7 h 39 m 48 sec
Reputation Power: 2199
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. Easy-peasy. Then to convert from seconds to hours, etc, would involve using integer division (/) and modulo (%) operations. Start with a modulo-60 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):
Quote:
 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 three-digit 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, 01-59. Since 1 is less than 59, borrow one from the minutes column and add it to the 1, so now we have 61-59 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 60-59 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 24-23 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 computer-like 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
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 05:23 PM.

#9
February 8th, 2013, 05:35 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
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

#10
February 8th, 2013, 05:50 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
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 half-way there.

#11
February 8th, 2013, 05:53 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
09:22:47

well sorry for bothering everyone, im just really desperate

and as for your question, 33767 seconds

#12
February 8th, 2013, 06:00 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,866
Time spent in forums: 3 Months 1 Day 7 h 39 m 48 sec
Reputation Power: 2199
I don't see any way I can give you an example without just giving you the code. We are honor-bound 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.

#13
February 8th, 2013, 06:07 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
Quote:
 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 honor-bound 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 ?

#14
February 8th, 2013, 06:37 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,866
Time spent in forums: 3 Months 1 Day 7 h 39 m 48 sec
Reputation Power: 2199
Quote:
 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?

Quote:
 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 floating-point 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 floating-point type (ie, float or double), then you use floating-point division.

For example:
9 / 10 = 0, because it's integer division
9 / 10.0 = 0.9, because it's floating-point 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.

#15
February 8th, 2013, 07:05 PM
 mjmjmjmj
Registered User

Join Date: Feb 2013
Posts: 9
Time spent in forums: 1 h 47 m 54 sec
Reputation Power: 0
Quote:
 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 floating-point 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 floating-point type (ie, float or double), then you use floating-point division. For example: 9 / 10 = 0, because it's integer division 9 / 10.0 = 0.9, because it's floating-point 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 ?