help in writing comparison function

Hello

I am writing a function its job as follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
It simply returns -1 if time0 is before time1, 0 if the times are equal,
1 if time0 is after time1. Recall that we’re (arbitrarily) saying that the 
first time in a week is    Sunday  12:00 AM    , and the last time in a week 
is    Saturday  11:59 PM (so the first possible time is one minute after the 
last possible time).

	For instance, compare ( {0, true, 11, 30}, {6, false, 9, 0} ) return -1 (Sunday is before Saturday)
	For instance, compare( {3, true, 6, 0}, {3, true, 12, 0} ) returns 1 
(Wednesday dinner is after Wednesday lunch)
	For instance, compare( {1, false, 6, 30}, {1, false, 6, 30} ) returns 0
 (Monday morning is Monday morning)
	For instance, compare( {6, false, 10, 0}, {6, false, 10, 1} ) returns -1 (one minute makes a difference)
	For instance, compare( {6, true, 8, 0}, {6, true, 1000, 0} ) complains
 and dies (sorry, Saturdays aren’t that long)


Here is my code I am not sure where is the wrong cause it works when I test it
and run this value it crashes

compare( {3, true, 6, 0}, {3, true, 12, 0} )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 int compare(const Time & time0, const Time & time1)
{
	if (ok(time0) && ok(time1))
	{
		if (time0.day < time1.day)
			return -1;
		else if (time0.day > time1.day)
			return 1;
		
		if (time0.pm < time1.pm)
			return -1;
		else if (time0.pm > time1.pm)
			return 1;
		
		if (time0.hour < time1.hour)
			return -1;
		else if (time0.hour > time1.hour)
			return 1;
		
		if (time0.minute < time1.minute)
			return -1;
		else if (time0.minute > time1.minute)
			return 1;
		else return 0;
			
	}
	else
	{
		if (time0.day > 6 || time1.day > 6)
			die("Only 7 days a week");
		else if (time0.hour > 12 || time1.hour > 12)
			die("Only 24 hours in a day");
		else if (time0.minute > 59 || time1.minute > 59)
			die("Only 60 minutes in an hour");
	}
}
Last edited on
Works fine for me.
Show definition of Time struct. Pinpoint location where you code crashes (add debug output between series of ifs)
Also definition of ok() would be fine too
1
2
3
4
5
6
struct Time{
	unsigned day;
	bool pm;
	unsigned hour;
	unsigned minute;
};


1
2
3
4
5
6
7
bool ok(const Time & time)
{
	if (time.day > 6 || time.hour > 12 || time.minute > 59)
		return false;

	return true;
}


it didn't crash it returned the wrong value
compare( {3, true, 6, 0}, {3, true, 12, 0} )
it should return 1 instead of -1
csharp wrote:
when I test it and run this value it crashes
csharp wrote:
it didn't crash
:/

You forgot to specially hanlde "12 hours". Because 12 pm < 1 pm.

I would suggest just to convert both times to minutes and compare them:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int compare(const Time & time0, const Time & time1)
{
    if (ok(time0) && ok(time1)) {
        int t0 = to_minutes(time0), 
            t1 = to_minutes(time1);
    	return (t0 < t1)?-1:
               (t0 > t1)? 1:
                          0;
    } else {
        if (time0.day > 6 || time1.day > 6)
            die("Only 7 days a week");
        else if (time0.hour > 12 || time1.hour > 12)
            die("Only 24 hours in a day");
        else if (time0.minute > 59 || time1.minute > 59)
            die("Only 60 minutes in an hour");
    }
}
Last edited on
I think I have to write to_minutes function right ?
Yes. Remember that you still have to handle 12 hours in it.
I am still not sure why you want to convert time0 and time1

but I think you mean convert hours to mins like to_minutes(time0.hour)
why you want to convert time0 and time1
To convert it to time representation almost every implementation uses. Easy to compare, easy to store, easy to pass, shorter code... Storing time as amount of seconds (munutes/days/years/etc.) passed from arbitrary point in time is de-facto standard.

Example of to_minutes function:
1
2
3
4
5
6
7
8
int to_minutes(const Time& t)
{
    constexpr int min_hour = 60;
    constexpr int pm_fix   = 12 * min_hour;
    constexpr int min_day  = 24 * min_hour;
    return  t.day*min_day + (t.pm ? pm_fix : 0) + t.minute + 
           (t.hour != 12 ? t.hour*min_hour : 0);
}
Topic archived. No new replies allowed.