Max/Min Time Vexation

This is a discussion on Max/Min Time Vexation within the C Programming forums, part of the General Programming Boards category; I am trying to find the max/minimum time a record came in. Thought it should be pretty straightforward, but alas. ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    84

    Question Max/Min Time Vexation

    I am trying to find the max/minimum time a record came in. Thought it should be pretty straightforward, but alas. I first initialize the first (f) and last (l) possible time to the first record and go from there...
    Here is the core of my code:

    Code:
      int f_yr, f_month, f_day, f_hour, f_min, f_sec;
      int l_yr, l_month, l_day, l_hour, l_min, l_sec;
    
      for(i=0; i < (n_stn-1); ++i){
          if(i == 0) {
             f_yr = data[i].year;
             f_month = data[i].month;
             f_day = data[i].day;
             f_hour = data[i].hour;
             f_min = data[i].min;
             f_sec = data[i].sec;
    
             l_yr = data[i].year;
             l_month = data[i].month;
             l_day = data[i].day;
             l_hour = data[i].hour;
             l_min = data[i].min;
             l_sec = data[i].sec;
          } else {
              if(data[i].year <= f_yr && data[i].month <= f_month &&
                 data[i].day <= f_day && data[i].hour <= f_hour &&
                 data[i].min <= f_min && data[i].sec <= f_sec ) {
                 f_yr = data[i].year;
                 f_month = data[i].month;
                 f_day = data[i].day;
                 f_hour = data[i].hour;
                 f_min = data[i].min;
                 f_sec = data[i].sec;
              }
              
              if(data[i].year >= l_yr && data[i].month >= l_month &&
                 data[i].day >= l_day && data[i].hour >= l_hour &&
                 data[i].min >= l_min && data[i].sec >= l_sec ) {
                 l_yr = data[i].year;
                 l_month = data[i].month;
                 l_day = data[i].day;
                 l_hour = data[i].hour;
                 l_min = data[i].min;
                 l_sec = data[i].sec;
              }
          }
      }
    
      printf("The first record is: %d/%d/%d %d:%d:%d \n",
              f_month, f_day, f_yr, f_hour, f_min, f_sec);
      printf("The last record is: %d/%d/%d %d:%d:%d \n",
              l_month, l_day, l_yr, l_hour, l_min, l_sec);
    The Output:
    The first record is: 9/24/2009 21:20:50
    The last record is: 9/24/2009 22:53:57

    Problem though, upon looking at the input:
    2009-09-24 21:10:50 (The real apparent minimum - upon inspection)
    2009-09-24 21:20:50
    2009-09-24 22:39:53
    2009-09-24 22:39:54
    2009-09-24 22:39:56
    2009-09-24 22:40:08
    2009-09-24 22:40:08
    2009-09-24 22:40:23
    2009-09-24 22:40:24
    2009-09-24 22:53:57
    2009-09-24 22:54:05 (The real apparent maximum - upon inspection)
    2009-09-24 22:54:04


    Caveat: I say apparent as the input file I have has over 2000-3000 depending on the day. But I checked and the first and last record to see if they were select max and min by the program (in this case the first record should have been min to what it selected and the last record should not have been - i.e. the program's last time in was greater than the last) and their was no error there.

    Any ideas?
    Last edited by towed; 06-10-2010 at 11:01 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your "AND to the max" logic is not what you want. What happens when in the last record (for example), the minute value is greater in this record, (making it a later record), BUT THE SECOND value is less?

    AH!!!

    Same problem with the first record logic, of course, since they're the same fish.

    You need to "step" through these comparisons:
    Code:
    if(f_year <= data[i].year) {
      if(f_month <= data[i].month) {
         //etc.
      }
    }
    It will complicate the logic, but think it through in situations where the year is <, (for instance), but the date is >, etc.
    Last edited by Adak; 06-10-2010 at 01:59 AM.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    Why is it necessary to "step" through with nested if loops instead of just using the AND logical operator? I feel they both "skin the cat".

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by towed View Post
    Why is it necessary to "step" through with nested if loops instead of just using the AND logical operator? I feel they both "skin the cat".
    What happens when in the last record (for example), the minute value is greater in this record, (making it a later record), BUT THE SECOND value is less?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  2. Replies: 11
    Last Post: 03-29-2009, 01:27 PM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21