473,382 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Difference between times

Can All of You help me to solve the C++ problem of making the difference between 2 hours???(hh:mm:ss)
for example:
hour 1 : 2:59:30
hour 2 : 1:29:10 (-)
so the answer is 1:30:20
can you make this program?????
Help Me please This is for the final exam.
Nov 30 '06 #1
3 15686
DeMan
1,806 1GB
I would suggest: make everything metric first. Our smallest units is seconds so we'll do our calculation in seconds.

so (((hours*60)+minutes)*60)+seconds=total number of seconds.

or in function (sorry, you'll have to parse the time strings yourself)

Expand|Select|Wrap|Line Numbers
  1. int totalSeconds(int hours, int minutes, int seconds)
  2. {
  3.   return (((hours*60)+minutes)*60+seconds);
  4. }
  5.  
once you have both in seconds we can do the arithmetic, and then convert back into the time we want. To avoid using pointers, or anything complicated make 2 functions:

getHours & getMinutes
Expand|Select|Wrap|Line Numbers
  1. int getHours(int seconds)
  2. {
  3.   return seconds / 3600   /*60*60 = 3600 if it doesn't make necessary changes*/
  4. }
  5.  
  6. int getMinutes(int seconds)
  7. {
  8.   return seconds / 60 /*divide seconds by 60 */
  9. }
  10.  
(Check my divide signs, cos I have a habbit of putting them the wrong way around.)

Because we do integer arithmetic any remainder is dropped. so all we need to do is call these functions

Expand|Select|Wrap|Line Numbers
  1. int hours = getHours(seconds);
  2. seconds=seconds-(hours*3600); /* make sure we take our hours off before we calculate minutes */
  3. int minutes=getMinutes(seconds);
  4. seconds=seconds-(minutes*60) /* as before but with minutes*/
  5.  
Now all that is left is to reconstruct the time string, which (assuming all you want to do is output) is

[code]
printf("difference: %d:%d:%d \n", hours, minutes, seconds)
[/codes]


Forgive me if i am inconsistent in variable names, the logic should be approximately right.

Hope this helps!!!
Nov 30 '06 #2
can you give me the complete program please...
because it's my first program in c++
i usually use turbo pascal....
i don't know anything about c++
Dec 2 '06 #3
DeMan
1,806 1GB
The Code given does the bulk of the working out. The basics of writing any program in c are availabel in a turotial on this site (or elsewhere on the net if you search for "C Tutorial", or "beginner c" or even "Hello World"). So I leave it to you to discover how to investigate how to make all the pieces work, but I am happy to give some pointers on the way....

Rather than recieve input in the form hh:mm:ss, it would be easist to prompt the user for hours, minutes and seconds individually, then you don't have to worry about parsing a String (if it has to be entered in String format, let me know).

An example of how to recieve input from the user (which I assume is where you are getting it from) would be:

Expand|Select|Wrap|Line Numbers
  1. int hours1;
  2. printf("Please enter the number of hours");
  3. scanf("%d", &hours1);
  4.  
It would be a good idea to verify that the input they give is reasonable in the context (eg 0<=hours<24), and you can use a similar statement for minutes and seonds.

Once you have recieved hours, minutes and seconds, you can use the methods posted earlier:

Expand|Select|Wrap|Line Numbers
  1. int total_seconds1=totalSeconds(hours1, minutes1, seconds1);
  2. int total_seconds2=totalSeconds(hours2, minutes2, seconds2);
  3. int total_difference = total_seconds2 - total_seconds1;
  4.  
  5. int hours=getHours(total_difference);
  6. total_difference =total_difference -(hours*3600); //take off the seconds we have finished with.
  7.  
  8. int minutes=getMinutes(total_difference );
  9. int seconds=total_difference - (minutes*60);
  10.  
  11.  
The earlier post shows how to print the result to the screen.
There should now be plenty of code around for you to play with

NOTE: Very few people here will reply by giving out large free chunks of code. Most are more than happy to help, but do not want to do other people's assignments. If you have absolutely no idea about how to attack a problem, your first port of call should be lecturer/tutor/prac supervisor/teacher. Furthermore, in my experience, if you desperately need this for a final exam, something similar must have been covered during the year. If this question appeared in a previous exam (and you have done no c++ this year), then chances are that they did the year it appeared in the exam (unless which language you use for this course is optional, in which case the exam SHOULD cater for that). Keep in mind also that exams are designed to test what you know. If you can write an equivalent program in pascal (or any other language), do it. While you will not get full marks for the question, you will probably get some credit for at least showing you know something (that is you may get 4/5 marks for a question because you showed you understood the problem even if you didn't know the exact fix). In quantitative subjects (that is ones that have a clear right or wrong answer (the opposite being qualitative, which are usually humatities subjects with essay based answers)), the worst answer you can do is to do nothing. Making an attempt at a question (no matter how far off track you are), will often give you some marks, because (even if you are way off track) you may have inadvertently shown that you understand something else. Most people who run courses WANT YOU TO SUCCEED (firstly because it gives them satisfaction in their work, and secondly because tertiary institutions build reputations on success, and so assess how well their teachers are doing compared to other institutions). Basically attempt all questions on exams even if you have no idea what is going on (also never scribble something out so that it can't be seen, if you cross something out and it is still visible, you may get (some) credit if it was (even slightly) correct).
I have (as is my habbit) waffled on here, and you can choose to ignore my advice, but I'm sure most people who have been through the stresses of earning a qualification will agree (in hindsight) that the stress is worth it.

GOOD LUCK IN YOUR EXAMS!!
Dec 3 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Andrew | last post by:
1) I have mySQL 3.23 installed. I have a record with two date/times fields in YYYY-MM-DD HH:MM:SS format. How do I find the difference between those two date/times? (I would also like to know it...
4
by: Arjen | last post by:
Hi, Whats the difference between Request.IsAuthenticated and User.Identity.IsAuthenticated? Thanks! Arjen
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
2
by: slinky | last post by:
Anyone know how to calculate the difference between two times displayed in two textboxes? I'm starting out with two textboxes: "txtCallTimeBegins" & "txtCallTimeEnds" this yielded: 6/7/2007...
3
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.