kerzo
Member
Registered: 10th Nov 02
Location: Norn Iron
User status: Offline
|
i am confused at who to go about this question for a coursework assignment and any help or even a program would be brilliant, here is the question
Construct a ‘C’ program that subtracts one time from another. You may assume that times are expressed in terms of a number of hours and a number of minutes and are in 24-hour format. For example, subtracting 1 hour 30 minutes from 1 hour 0 minutes (representing 010) should result in 23:30 being displayed (i.e. 11:30pm the previous day). The user will enter four values (the hours and minutes of each of the two times) and the program should then output the result of the subtraction in 24-hour format.
|
Mikorsa16v
Member
Registered: 2nd Sep 02
Location: Burgess Hill, West Sussex
User status: Offline
|
right firstly you need to split up the hours and the mins, as two seperate integers and then set the 'limit' as static variables to both, i.e. the limit for hours is 24, and the limit for the mins is 60.
i was gonna say have two classes one for hours and one for mins, but C isnt OO thinking of c++.
i havent coded in C for a year now! so can only help on the structure really, as i look at past assignments lol
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
Easier to convert the dates to a standardised timestamp (number of seconds since 1900) and then perform a simple subtraction. You can then convert back to the real date answer...
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
Or construct an array for hours and an array for minutes... you could then do the maths on the array indices and return the correct value...
|
kerzo
Member
Registered: 10th Nov 02
Location: Norn Iron
User status: Offline
|
oh so confused!!!
|
John_C
Member
Registered: 5th Mar 03
Location: South east, Bromley
User status: Offline
|
c sucks
can't get my head round it
|
kerzo
Member
Registered: 10th Nov 02
Location: Norn Iron
User status: Offline
|
quote: Originally posted by John_C
c sucks
can't get my head round it
same as me pity i have another few weeks of it then onto java
|
John_C
Member
Registered: 5th Mar 03
Location: South east, Bromley
User status: Offline
|
low level bs!
i'm meant 2 b doin c++ tonight as well, thats goin marginally better but not great
|
kerzo
Member
Registered: 10th Nov 02
Location: Norn Iron
User status: Offline
|
16/40 in a class test i got
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
One sec...
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
Apologise for rushed code but it should do the job... there are neater ways of doing this I just haven't got a function reference to hand...
code:
#include <stdio.h>
void main() {
int result, time1_h, time1_m, time2_h, time2_m;
printf("Enter 1st time hour: ");
scanf("%d", &time1_h);
printf("Enter 1st time mins: ");
scanf("%d", &time1_m);
printf("Enter 2nd time hour: ");
scanf("%d", &time2_h);
printf("Enter 2nd time mins: ");
scanf("%d", &time2_m);
result = ((60*time1_h) + time1_m) - ((60*time2_h) + time2_m);
// Fix if time becomes 'yesterday'
while (result < 0) {
result = result + 1440; // Number of minutes in a day
}
// Output result
printf("Time (1st - 2nd): %02d:%02d\n",(result / 60), (int)(result % 60));
}
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
cout << etc is C++...
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
C and C++ are not the same
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
I should make deletion of posts say "comment withdrawn"
|
John_C
Member
Registered: 5th Mar 03
Location: South east, Bromley
User status: Offline
|
fekin pointers
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
Arghhh I'm having a convo with myself
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
the initial joke of 'Your point?' was the fact you were talking to yourself... I had already removed mine, I know they're different - but assumed he said he was doing C++ not C, oops.
Don't put 'Post deleted' its more fun this way
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
quote: Originally posted by John_C
fekin pointers
They're ok once you get your head round the idea of memory and allocation etc... def worth learning properly though or else when you come to harder examples you really get stuck...
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
quote: Originally posted by Paul_J
the initial joke of 'Your point?' was the fact you were talking to yourself... I had already removed mine, I know they're different - but assumed he said he was doing C++ not C, oops.
Don't put 'Post deleted' its more fun this way
I should have just quoted you
|
Paul_J
Member
Registered: 6th Jun 02
Location: London
User status: Offline
|
yes =]
|
kerzo
Member
Registered: 10th Nov 02
Location: Norn Iron
User status: Offline
|
oh my god thank you
i would ask for more but would that just be too greedy!!!
|
Tim
Site Administrator
Registered: 21st Apr 00
User status: Offline
|
No probs you wouldn't believe how bored I was
Sorry but that's ya £12.50's worth
[Edited on 20-11-2003 by Tim]
|
kerzo
Member
Registered: 10th Nov 02
Location: Norn Iron
User status: Offline
|
at least i got good value from it
cheers tim
|
Dan B
Member
Registered: 25th Feb 01
User status: Offline
|
Now there's a different usage for the whole Premium Member thing......don't suppose you fancy doing my C coding, Tim? It'll only take you about 2 months!
I'll buy Premium Membership if you do!
|
Jason
Member
Registered: 5th Aug 03
Location: Northern Ireland
User status: Offline
|
kerzo u'll love j++
|