Board index » delphi » Newbie Problem

Newbie Problem

Hi

I have just started learning TP7 but am having problems completing the
procedure below. The objective is to convert seconds to Hours Minutes
and Seconds but I am unsure how to use MOD and DIV within the
procedure. Any help will be very much appreciated

Here's the sod that is giving me grief :-)

Procedure   ConvertSecondsToHoursMinutesAndSeconds
VAR         Hours,Minutes,Seconds,TimeInSeconds : LONGINT
begin
        WriteLn;
        WriteLn;
        Write ('Please Enter The Number Of Seconds');
        ReadLn (Seconds)
        WriteLn;
        WriteLn;
        Write ('You have entered' ,seconds, ' seconds');
        Minutes := Seconds DIV 60;
        Hours := Seconds DIV 3600;
        TimeInSeconds := {???? Using DIV and MOD ????}
        WriteLn;
        WriteLn;
        Write ('Time Converts To' ,Hours, ' hours' ,Minutes, ' Minutes and'
        ,seconds, ' Seconds');
end;

I know this will probably take most of you 3 seconds to sort out, I'm
on my 3rd Day :-) As I say, if anyone would be kind enough to point me
in the right direction it would be very much appreciated

Regards

George Johnson | Email : geo...@gjohnson.demon.co.uk
<a href="http://metro.turnpike.net/J/Jelec/index.html">The Newbies Net Guide</a>
PGP Public Key Available
FingerPrint : 7D A4 66 2C 68 AE 15 E4 E4 27 88 25 5A F6 CA 2C

 

Re:Newbie Problem


In article 29...@gjohnson.demon.co.uk, geo...@gjohnson.demon.co.uk (George Johnson) writes:

Quote
> Hi

> I have just started learning TP7 but am having problems completing the
> procedure below. The objective is to convert seconds to Hours Minutes
> and Seconds but I am unsure how to use MOD and DIV within the
> procedure. Any help will be very much appreciated

Hi there,

The most difficult part seems to be the number of minutes.
So, here it is :

Procedure   ConvertSecondsToHoursMinutesAndSeconds;
VAR         Hours,Minutes,Seconds,TimeInSeconds : LONGINT;
begin
        WriteLn;
        WriteLn;
        Write ('Please Enter The Number Of Seconds');
        ReadLn (TimeInSeconds)
        WriteLn;
        WriteLn;
        Write ('You have entered' ,TimeInSeconds, ' seconds');
        Hours := TimeInSeconds DIV 3600;
        Minutes := (TimeInSeconds DIV 60) MOD 60;
        Seconds := TimeInSeconds MOD 60;
        WriteLn;
        WriteLn;
        Write ('Time Converts To' ,Hours, ' hours' ,Minutes, ' Minutes and'
        ,seconds, ' Seconds');
end;

Explanation :

* An hour is completed after 3600 seconds. (Obvious)
* The number of seconds is obtained by a MOD 60. (Obvious too)
* To extract the number of minutes, first convert time in seconds to
  time in minutes (DIV 60). [This drops out the seconds.] Then perform
  a MOD 60 to retrieve the number of minutes. So this explain the formula.
  {You can also write  Minutes := (TimeInSeconds MOD 3600) DIV 60;
   if you prefer. It's how you like it.  }

Hope this will help.

Christophe.

Re:Newbie Problem


In article <816536412.29...@gjohnson.demon.co.uk>, geo...@gjohnson.demon.co.uk (George Johnson) writes:

Quote
>Hi

>I have just started learning TP7 but am having problems completing the
>procedure below. The objective is to convert seconds to Hours Minutes
>and Seconds but I am unsure how to use MOD and DIV within the
>procedure. Any help will be very much appreciated

>Here's the sod that is giving me grief :-)

>Procedure   ConvertSecondsToHoursMinutesAndSeconds
>VAR     Hours,Minutes,Seconds,TimeInSeconds : LONGINT
>begin
>    WriteLn;
>    WriteLn;
>    Write ('Please Enter The Number Of Seconds');
>    ReadLn (Seconds)
>    WriteLn;
>    WriteLn;
>    Write ('You have entered' ,seconds, ' seconds');
>    Minutes := Seconds DIV 60;
>    Hours := Seconds DIV 3600;

Your problem is that you're not removing your hours and minutes from the
total after figuring them; try something like:

Hours := Seconds div 3600 ;
Minutes := ( Seconds - ( Hours * 3600 )) div 60 ;
SecondsLeft := ( Seconds - ( Hours * 3600 ) - ( Minutes * 60 )) ;

- Show quoted text -

Quote
>    TimeInSeconds := {???? Using DIV and MOD ????}
>    WriteLn;
>    WriteLn;
>    Write ('Time Converts To' ,Hours, ' hours' ,Minutes, ' Minutes and'
>    ,seconds, ' Seconds');
>end;

>I know this will probably take most of you 3 seconds to sort out, I'm
>on my 3rd Day :-) As I say, if anyone would be kind enough to point me
>in the right direction it would be very much appreciated

>Regards

>George Johnson | Email : geo...@gjohnson.demon.co.uk
><a href="http://metro.turnpike.net/J/Jelec/index.html">The Newbies Net Guide</a>
>PGP Public Key Available
>FingerPrint : 7D A4 66 2C 68 AE 15 E4 E4 27 88 25 5A F6 CA 2C

HTH

-* Stephen *-
Stephen Posey
University of New Orleans
Email  : S...@uno.edu
WWW    : http://www.uno.edu/~slp

Other Threads