![]() |
| | #1 |
| Registered User Join Date: Feb 2005
Posts: 59
| Time between Military Hours For example the hours and minutes between 0900 and 1730; 1730 and 0900 the next day. Now I just converted all the times to seconds because I find that to be easier. this is what I do.. hours for time 1 = time1 / 100 minutes for time 1 = time1 % 100 calculate total no of seconds = (hours * 3600) + (minutes * 60) hours for time 2 = time2 / 100 minutes for time 2 = time2 % 100 calculate total no of seconds = (hours * 3600) + (minutes * 60) Now this is where I have trouble.. I know I can do it with an if statement but we aren't allowed to use that yet... remaining total seconds = seconds in 24 hours - abs((total num of secs for time 1 - total num of secs for time 2) number of minutes = remaing total seconds / 60 number of hours = num of minutes / 60 minutes left over = number of minutes % 60 Then I output hours and minutes like 8 hours and 30 minutes So if I take away the number of seconds in a day in this: remaining total seconds = seconds in 24 hours - abs((total num of secs for time 1 - total num of secs for time 2) Then when I want to find the number of hours and minutes between 0900 -> 1730 I get 8 hours and 30 minutes which is correct. But if I do 1730 -> 0900 the next morning I still get 8 hours 30 minutes. And if I leaving the number of seconds in a day in I get the correct answer for 1730 -> 0900 but not for 0900 -> 1730. I would appreciate any help you can give me this is a puzzle for me.. Last edited by StarOrbs; 02-26-2005 at 08:13 PM. |
| StarOrbs is offline | |
| | #2 |
| Registered User Join Date: Aug 2001
Posts: 244
| it is assumed that time 2 is greater than time 1 (actually you need to check that this holds but since you must not use ifs you cant do the check here) .) convert time 1 to minutes .) convert time 2 to minutes tmp = time2- time1; if i am not mistaken the difference in hours an minutes would be hours = tmp / 60; minutes = tmp % 60; // where this is the remainder of the division
__________________ signature under construction |
| Raven Arkadon is offline | |
| | #3 |
| Registered User Join Date: Feb 2005
Posts: 59
| Thats the problem it's suppose to work when time1 is greater and less than time2. |
| StarOrbs is offline | |
| | #4 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| Perhaps you could do this (in pseudocode): Code: time2 = (however you found time2); time2 += (1 day); timediff = (time2 - time1) % (1 day); Interesting problem.
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. Last edited by Hunter2; 02-26-2005 at 11:02 PM. |
| Hunter2 is offline | |
| | #5 | |
| 30 Helens Agree Join Date: Jan 2002
Posts: 607
| Quote:
__________________ AIM: Neandrake EMAIL: nta0 @ yahoo . com Operating System: Windows XP SP2 Compiler: GCC IDE: Notepad++ Don't give up your freedom to think - www.cognitiveliberty.org | |
| neandrake is offline | |
| | #7 |
| Registered User Join Date: Mar 2002
Posts: 1,595
| Again, pencil and paper (or word processor) should be of help here. Assume I give you two times in terms of just military time and don't give you a date. The difference is just the larger minus the smaller. But now assume there is a date attached. If the dates are the same, then the difference is still just the larger minus the smaller. But, if the dates are different, then you need to account for that difference. So first determine which date comes firt. Subtract the time from 2400 for that date and store as part1 and add the time from the latter date to part1 to get total. If there are any full days between the two dates, account for that, too. DateTime date1 = Feb 28, 2005 1320 DateTime date2 = Feb 26, 2005 1910 date1.year == date2.year date1.month == date2.month but date1.day is after date2.day, therefore date1 is "larger"/"later"/whatever. So 2400 - 1910 = 450 + 1320 = 1810 but date1.day is separated from date2 by 1 full day, so need to add 2400 for a result that the two times are 4210 min apart in military time lingo or 42 h and 10 min apart in routine speak. Now if military time is entered as a single entity then will need to separate digits into separate hour and minute components, whereas if entries are separate to begin with, that won't be necessary. Now put this all in the appropriate loops and if/else control statements and create an addition/subtraction mechanism to account for base 60 instead of base 10 when it comes time to add/subtract minutes and I think the process can be accomplished.
__________________ You're only born perfect. |
| elad is offline | |
| | #8 | |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| >>and if/else control statements Aye, there's the catch. Quote:
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. | |
| Hunter2 is offline | |
| | #9 |
| Cheesy Poofs! Join Date: Sep 2002 Location: Boulder
Posts: 1,728
| I've had similar situations where professors won't allow concepts that haven't been taught yet to be used in current assignments. Its usually because the teacher wants to reinforce the topic at hand. For example this question can be done easily in only a line or two with if statements, but I doubt that has anything do do with what this weeks topic is. Are you only given the two military times with no dates attached where its assumed that there is never more than 24 hours between two times? If so, then another way to solve this is with a for loop that starts at the first time and ends at the second time and counts the minutes until the loop is done. For example, say you were counting the number of days between two calendar dates and you've converted the dates to day number (Jan 1=1, Dec 31=365) then the loop would be like this: Code: int numdays=0;
for(int x=day1; x!=day2; x++)
{
numdays++;
x%=365;
}
// numdays should now have the total numbers of days between day1 and day2
|
| PJYelton is offline | |
| | #10 |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| I don't believe this offers any improvement. Rather, you'll get a semi-infinite loop when day2 < day1 (eventually the loop will stop, when the counter wraps around and comes back). Anyway, this introduces a test condition. If you could use for, you could fake an if statement like so: Code: for(; day2 < day1; )
{
day2 += (1 day);
break;
}
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. |
| Hunter2 is offline | |
| | #11 |
| Cheesy Poofs! Join Date: Sep 2002 Location: Boulder
Posts: 1,728
| Well, didn't say it was an improvement, just another idea ![]() It won't be infinite because x will revert to zero as soon as it hits 365 due to the x%=365 line. |
| PJYelton is offline | |
| | #12 |
| Registered User Join Date: Mar 2002
Posts: 1,595
| Have to admit that I didn't read the OP close enough to pick up the "no ifs" qualification. Sorry about that. ____________________________________________ >>It won't be infinite because x will revert to zero as soon as it hits 365 due to the x%=365 line. True, it won't be an infinite loop, but when day2 is earlier than day1 then numdays will always == 365 - day2 at the end of the loop, not the difference between day1 and day2. ____________________________________________ I don't see a way to do this if you can't use ifs and you can't be sure that a > b and you have to hard code result = a - b.
__________________ You're only born perfect. |
| elad is offline | |
| | #13 |
| Cheesy Poofs! Join Date: Sep 2002 Location: Boulder
Posts: 1,728
| Not to beat a dead horse because Hunter2's solution is more efficient than mine, but my code should work. For example, say we are finding the number of days between May 1st to April 1st which is a difference of 335 days. Ok, day1 would equal 121 and day2 would equal 91. The variable x would start at 121 and increment, when it hits 365 it will revert to zero since 365%365==0. At this point the loop would have executed 244 times thus numdays will equal 244. Now the loop will keep executing 91 more times incrementing numdays 91 more times at which point x will equal day2. So numdays will have a grand total of 244+91=335 which is the correct answer. Last edited by PJYelton; 02-28-2005 at 05:30 PM. |
| PJYelton is offline | |
| | #14 | |
| Carnivore ('-'v) Join Date: May 2002
Posts: 2,866
| >>It won't be infinite because x will revert to zero as soon as it hits 365 due to the x%=365 line. Oops, missed the mod line. Guess it does work after all ![]() >>will always == 365 - day2 No, it will be the number of days from day2 to day1 the next year - the loop condition is !=, not <. Quote:
Does my solution not work? It seemed to work for the 2 test cases I worked out on paper.
__________________ Just Google It. √ (\ /) ( . .) c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination. | |
| Hunter2 is offline | |
| | #15 |
| Cheesy Poofs! Join Date: Sep 2002 Location: Boulder
Posts: 1,728
| As far as I can tell your solution works and is quick and efficient. I doubt there's a better solution than that. Only problem is if he's rarely worked with modulus and he has to turn around and explain his reasoning to his teacher |
| PJYelton is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Time to seconds program | Sure | C Programming | 1 | 06-13-2005 08:08 PM |
| Killing someones grandparents | nickname_changed | A Brief History of Cprogramming.com | 37 | 09-07-2003 07:56 AM |
| The Timing is incorret | Drew | C++ Programming | 5 | 08-28-2003 04:57 PM |
| calculating user time and time elapsed | Neildadon | C++ Programming | 0 | 02-10-2003 06:00 PM |