Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #72251 > unrolled thread

Program to compute elapsed time, learning structures and functions

Started byAlla _ <modelling.data@gmail.com>
First post2015-10-09 02:36 -0700
Last post2015-11-05 15:50 -0800
Articles 9 on this page of 49 — 12 participants

Back to article view | Back to comp.lang.c


Contents

  Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 02:36 -0700
    Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 10:48 +0100
      Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 03:06 -0700
    Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 11:01 +0100
      Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 03:07 -0700
    Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 13:26 +0100
      Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 06:49 -0700
        Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 16:20 +0100
          Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 09:27 -0700
          Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:29 +0100
          Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-09 14:06 -0700
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-09 22:24 +0100
              Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-09 15:51 -0700
        Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 18:26 +0100
          Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-09 19:24 +0100
            Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-10-09 19:50 +0100
              Re: Program to compute elapsed time, learning structures and functions Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-09 15:56 -0400
        Re: Program to compute elapsed time, learning structures and functions James Kuyper <jameskuyper@verizon.net> - 2015-10-09 15:31 -0400
        Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-10 06:57 -0700
          Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 17:25 +0100
            Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 19:12 +0100
            Re: Program to compute elapsed time, learning structures and functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-10 20:56 +0100
              Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-10 21:52 +0100
            Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-10 17:57 -0700
              Re: Program to compute elapsed time, learning structures and functions raltbos@xs4all.nl (Richard Bos) - 2015-10-11 10:09 +0000
              Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-11 11:26 +0100
                Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-11 14:12 -0700
              Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:51 -0700
                Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-15 11:25 -0700
                  Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 05:34 -0700
                    Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-10-16 08:52 -0700
            Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-15 10:46 -0700
              Re: Program to compute elapsed time, learning structures and functions Bartc <bc@freeuk.com> - 2015-10-15 19:06 +0100
                Re: Program to compute elapsed time, learning structures and functions Tim Rentsch <txr@alumni.caltech.edu> - 2015-10-16 05:24 -0700
            Re: Program to compute elapsed time, learning structures and functions glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-10-17 00:10 +0000
        Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 09:52 -0700
    Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-10-09 06:52 -0700
      Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-10-18 10:21 -0700
        Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 00:56 -0800
          Re: Program to compute elapsed time, learning structures and functions Barry Schwarz <schwarzb@dqel.com> - 2015-11-05 01:55 -0800
            Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 02:55 -0800
            Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 03:01 -0800
              Re: Program to compute elapsed time, learning structures and functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-05 03:41 -0800
                Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 06:55 -0800
            Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 03:03 -0800
              Re: Program to compute elapsed time, learning structures and functions Richard Heathfield <rjh@cpax.org.uk> - 2015-11-05 13:27 +0000
                Re: Program to compute elapsed time, learning structures and functions Alla _ <modelling.data@gmail.com> - 2015-11-05 06:54 -0800
          Re: Program to compute elapsed time, learning structures and functions Keith Thompson <kst-u@mib.org> - 2015-11-05 12:42 -0800
            Re: Program to compute elapsed time, learning structures and functions Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-11-05 15:50 -0800

Page 3 of 3 — ← Prev page 1 2 [3]


#74946

FromAlla _ <modelling.data@gmail.com>
Date2015-11-05 02:55 -0800
Message-ID<0884e052-6af6-4270-9182-109af39cd354@googlegroups.com>
In reply to#74941
Thank you so much! I see what I have to correct. Thank you :)

[toc] | [prev] | [next] | [standalone]


#74947

FromAlla _ <modelling.data@gmail.com>
Date2015-11-05 03:01 -0800
Message-ID<bb01059d-6629-44eb-81e6-0ef37b765fb2@googlegroups.com>
In reply to#74941
This looks so much better )
// Program to computed elapsed time between two times
#include <stdio.h>

struct time
{
    int hour;
    int minutes;
    int seconds;
};

struct time elapsed_time (struct time time_1, struct time time_2);

int main (void)
{
    struct time time_one, time_two, elapsed;
    
    printf("Please, enter two times (hh:mm:ss):\n");
    scanf ("%d:%d:%d  %d:%d:%d", &time_one.hour, &time_one.minutes, &time_one.seconds, &time_two.hour, &time_two.minutes, &time_two.seconds);
    
    elapsed = elapsed_time (time_one, time_two);
    
    printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
    
    return 0;
}

// Function to compute elapsed time

struct time elapsed_time (struct time time_1, struct time time_2)
{
    struct time result;
    
    result.seconds = time_2.seconds - time_1.seconds;
    
    if (result.seconds < 0)
    {
        result.seconds += 60;
        time_2.minutes--;
    }
    
        
    result.minutes = time_2.minutes - time_1.minutes;
        
    if (result.minutes < 0)
    {
        result.minutes += 60;
        time_2.hour --;
    }
    
    result.hour = time_2.hour - time_1.hour;
    
    if (result.hour < 0)
        
        result.hour += 24;
        
        
    return result;
}

[toc] | [prev] | [next] | [standalone]


#74949

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2015-11-05 03:41 -0800
Message-ID<228b8378-a6ec-413c-8f6a-acebcd7c0397@googlegroups.com>
In reply to#74947
On Thursday, November 5, 2015 at 11:02:04 AM UTC, Alla _ wrote:
> This looks so much better )
> // Program to computed elapsed time between two times
> #include <stdio.h>
> 
> struct time
> {
>     int hour;
>     int minutes;
>     int seconds;
> };
> 
> struct time elapsed_time (struct time time_1, struct time time_2);
> 
> int main (void)
> {
>     struct time time_one, time_two, elapsed;
>     
>     printf("Please, enter two times (hh:mm:ss):\n");
>     scanf ("%d:%d:%d  %d:%d:%d", &time_one.hour, &time_one.minutes, &time_one.seconds, &time_two.hour, &time_two.minutes, &time_two.seconds);
>     
>     elapsed = elapsed_time (time_one, time_two);
>     
>     printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
>     
>     return 0;
> }
> 
> // Function to compute elapsed time
> 
> struct time elapsed_time (struct time time_1, struct time time_2)
> {
>     struct time result;
>     
>     result.seconds = time_2.seconds - time_1.seconds;
>     
>     if (result.seconds < 0)
>     {
>         result.seconds += 60;
>         time_2.minutes--;
>     }
>     
>         
>     result.minutes = time_2.minutes - time_1.minutes;
>         
>     if (result.minutes < 0)
>     {
>         result.minutes += 60;
>         time_2.hour --;
>     }
>     
>     result.hour = time_2.hour - time_1.hour;
>     
>     if (result.hour < 0)
>         
>         result.hour += 24;
>         
>         
>     return result;
> }

It's not bad.

I'd add a time_valid function. If the user tries to enter negative seconds, 60 or more seconds, 
and so on, throw them out.

Then consider what is supposed to happen if the "tock" is before the "tick". Unless your users
are neutrinos, usually it doesn't make sense to have negative elapsed time.

[toc] | [prev] | [next] | [standalone]


#74975

FromAlla _ <modelling.data@gmail.com>
Date2015-11-05 06:55 -0800
Message-ID<8018fc9a-fba6-4025-bc07-ceef9a23f8b9@googlegroups.com>
In reply to#74949
On Thursday, November 5, 2015 at 3:42:27 PM UTC+4, Malcolm McLean wrote:
> On Thursday, November 5, 2015 at 11:02:04 AM UTC, Alla _ wrote:
> > This looks so much better )
> > // Program to computed elapsed time between two times
> > #include <stdio.h>
> > 
> > struct time
> > {
> >     int hour;
> >     int minutes;
> >     int seconds;
> > };
> > 
> > struct time elapsed_time (struct time time_1, struct time time_2);
> > 
> > int main (void)
> > {
> >     struct time time_one, time_two, elapsed;
> >     
> >     printf("Please, enter two times (hh:mm:ss):\n");
> >     scanf ("%d:%d:%d  %d:%d:%d", &time_one.hour, &time_one.minutes, &time_one.seconds, &time_two.hour, &time_two.minutes, &time_two.seconds);
> >     
> >     elapsed = elapsed_time (time_one, time_two);
> >     
> >     printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
> >     
> >     return 0;
> > }
> > 
> > // Function to compute elapsed time
> > 
> > struct time elapsed_time (struct time time_1, struct time time_2)
> > {
> >     struct time result;
> >     
> >     result.seconds = time_2.seconds - time_1.seconds;
> >     
> >     if (result.seconds < 0)
> >     {
> >         result.seconds += 60;
> >         time_2.minutes--;
> >     }
> >     
> >         
> >     result.minutes = time_2.minutes - time_1.minutes;
> >         
> >     if (result.minutes < 0)
> >     {
> >         result.minutes += 60;
> >         time_2.hour --;
> >     }
> >     
> >     result.hour = time_2.hour - time_1.hour;
> >     
> >     if (result.hour < 0)
> >         
> >         result.hour += 24;
> >         
> >         
> >     return result;
> > }
> 
> It's not bad.
> 
> I'd add a time_valid function. If the user tries to enter negative seconds, 60 or more seconds, 
> and so on, throw them out.
> 
> Then consider what is supposed to happen if the "tock" is before the "tick". Unless your users
> are neutrinos, usually it doesn't make sense to have negative elapsed time.

Thank you. Indeed, these 'features' would make the program
better.

[toc] | [prev] | [next] | [standalone]


#74948

FromAlla _ <modelling.data@gmail.com>
Date2015-11-05 03:03 -0800
Message-ID<fde899dd-7817-4e28-8819-ba4ca0f51fb3@googlegroups.com>
In reply to#74941
>     
> >    elapsed = elapsed_time (time_one, time_two);
> >    
> >    printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
> 
> There is no distinction on output.
>     
I am sorry - I didn't understand this last sentence. What do you
by 'no distinction on output'?

[toc] | [prev] | [next] | [standalone]


#74961

FromRichard Heathfield <rjh@cpax.org.uk>
Date2015-11-05 13:27 +0000
Message-ID<n1flbl$nvj$1@dont-email.me>
In reply to#74948
On 05/11/15 11:03, Alla _ wrote:
>
>>
>>>     elapsed = elapsed_time (time_one, time_two);
>>>
>>>     printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
>>
>> There is no distinction on output.
>>
> I am sorry - I didn't understand this last sentence. What do you
> by 'no distinction on output'?
>

He meant that, although scanf behaves differently for %i than it does 
for %d on some inputs, printf doesn't behave differently. From printf's 
point of view, they both mean the same thing.

Most people use %d unless they have a pressing reason to use %i.

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

[toc] | [prev] | [next] | [standalone]


#74974

FromAlla _ <modelling.data@gmail.com>
Date2015-11-05 06:54 -0800
Message-ID<da6bac54-ebd4-42bc-84e4-c9dd78fc06e5@googlegroups.com>
In reply to#74961
On Thursday, November 5, 2015 at 5:27:35 PM UTC+4, Richard Heathfield wrote:
> On 05/11/15 11:03, Alla _ wrote:
> >
> >>
> >>>     elapsed = elapsed_time (time_one, time_two);
> >>>
> >>>     printf("The elapsed time is %.2i:%.2i:%.2i\n", elapsed.hour, elapsed.minutes, elapsed.seconds);
> >>
> >> There is no distinction on output.
> >>
> > I am sorry - I didn't understand this last sentence. What do you
> > by 'no distinction on output'?
> >
> 
> He meant that, although scanf behaves differently for %i than it does 
> for %d on some inputs, printf doesn't behave differently. From printf's 
> point of view, they both mean the same thing.
> 
> Most people use %d unless they have a pressing reason to use %i.
> 
> -- 
> Richard Heathfield
> Email: rjh at cpax dot org dot uk
> "Usenet is a strange place" - dmr 29 July 1999
> Sig line 4 vacant - apply within

Thank you for the explanation :)

[toc] | [prev] | [next] | [standalone]


#75013

FromKeith Thompson <kst-u@mib.org>
Date2015-11-05 12:42 -0800
Message-ID<ln7flw5gbb.fsf@kst-u.example.com>
In reply to#74933
Alla _ <modelling.data@gmail.com> writes:
> Long time after I created this thread, I have corrected the 
> program, and it works now. Thank you for your help!
>
> // Program to computed elapsed time between two times
> #include <stdio.h>
>
> struct time
> {
>     int hour;
>     int minutes;
>     int seconds;
> };
>
> struct time elapsed_time (struct time time_1, struct time time_2);
[snip]

You're doing something to handle wraparound from one day to the next,
but I don't think you're doing it consistently.  This also interacts
oddly with out-of-order input.  For example:

 1:00:00  3:00:00 -->  02:00:00
 3:00:00  1:00:00 -->  22:00:00
49:00:00 00:00:00 --> -25:00:00
00:00:00 49:00:00 -->  49:00:00

You need a consistent definition of how the program should behave in
these corner cases -- which includes defining just what you mean by
"elapsed time".  Restricting the permitted input values (and aborting
with an error message when the restrictions are violated) is a
reasonable approach if you don't want to handle bizarre inputs.

You have a great deal of code in your elapsed_time function that
deals with the hour, minutes, and seconds fields separately.
An alternative approach (one that I'd probably use) is to convert
the "struct time" value to a raw number of seconds, compute the
difference, and then convert back to a "struct time" result.

Encapsulating the struct time <--> raw seconds conversions into their
own functions would IMHO make the code easier to read and understand.

There might be some concern for overflow, if
    3600*hour + 60*minute + seconds
might exceed INT_MAX, but storing raw seconds in a long or even
a long long would avoid that for any "reasonable" time intervals.
Storing a number of seconds in a "long" covers an interval of at
least 68 years.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#75031

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2015-11-05 15:50 -0800
Message-ID<96fc2202-7f5f-4c78-bad8-996dfb4e6c8e@googlegroups.com>
In reply to#75013
On Thursday, November 5, 2015 at 8:42:31 PM UTC, Keith Thompson wrote:
> Alla _ <modelling.data@gmail.com> writes:
> > Long time after I created this thread, I have corrected the 
> > program, and it works now. Thank you for your help!
> >
> > // Program to computed elapsed time between two times
> > #include <stdio.h>
> >
> > struct time
> > {
> >     int hour;
> >     int minutes;
> >     int seconds;
> > };
> >
> > struct time elapsed_time (struct time time_1, struct time time_2);
> [snip]
> 
> You're doing something to handle wraparound from one day to the next,
> but I don't think you're doing it consistently.  This also interacts
> oddly with out-of-order input.  For example:
> 
>  1:00:00  3:00:00 -->  02:00:00
>  3:00:00  1:00:00 -->  22:00:00
> 49:00:00 00:00:00 --> -25:00:00
> 00:00:00 49:00:00 -->  49:00:00
> 
> You need a consistent definition of how the program should behave in
> these corner cases -- which includes defining just what you mean by
> "elapsed time".  Restricting the permitted input values (and aborting
> with an error message when the restrictions are violated) is a
> reasonable approach if you don't want to handle bizarre inputs.
> 
> You have a great deal of code in your elapsed_time function that
> deals with the hour, minutes, and seconds fields separately.
> An alternative approach (one that I'd probably use) is to convert
> the "struct time" value to a raw number of seconds, compute the
> difference, and then convert back to a "struct time" result.
> 
> Encapsulating the struct time <--> raw seconds conversions into their
> own functions would IMHO make the code easier to read and understand.
> 
> There might be some concern for overflow, if
>     3600*hour + 60*minute + seconds
> might exceed INT_MAX, but storing raw seconds in a long or even
> a long long would avoid that for any "reasonable" time intervals.
> Storing a number of seconds in a "long" covers an interval of at
> least 68 years.
> 
If you write a function to convert from a timestruct to seconds, and
back, and express seconds as a double, you get 2^53 seconds represented
to second precision. The system then degrades gracefully if you attempt
longer intervals - over 285,616,414 years I make it.

Whilst intervals of 68 years come up in human affairs quite often -
it's not unusual to span such an interval in your personal life -
the higher limit is much less likely to be reached, except in geology
or astronomy, where you won't need second precision anyway.

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

Back to top | Article view | comp.lang.c


csiph-web