Qbasic, Quickbasic/QBASIC PROGRAMMING

Advertisement


Question
How to convert a large number of seconds to hours: minutes: seconds through QBASIC program.

Answer
Hi Arindam Sahoo,

Well, this is less of a QBasic program and more of a math problem.

Let's sort out what we have.

Let's say we have 1430 seconds.  We know there is 60 seconds in a minute, and 60 minutes in an hour.

So, let's try converting to minutes

REM Notice that I'm using the ! (single percision) suffix.  this is so our numbers will have decimals
const SECONDS_IN_ONE_MINUTE! = 60.0

DIM total_seconds%

total_seconds% = 1430

minutes% = INT(total_seconds! / SECONDS_IN_ONE_MINUTE!)
REM minutes% should now equal 23.83333, but since we're using INT (converts the formula into an integer, and truncates the decimal), it will trim down to 23


So `minutes%` really isn't helpful on it's own, because now we want to know how many seconds are in that 0.5 that INT() removed for us, so the easiest way to deal with this is the MOD (modulo) function

seconds_remaining% = total_seconds! MOD SECONDS_IN_ONE_MINUTE!
REM seconds_remaining% should now equal 50


Now we know that 1430 seconds is 23 minutes and 50 seconds.

The same idea applies to converting minutes to hours

Let's say we have 147 minutes, let's do the math

const MINUTES_IN_ONE_HOUR! = 60.0

DIM total_minutes%

total_minutes% = 147

hours% = INT(total_minutes% / MINUTES_IN_ONE_HOUR!)
minutes_remaining% = total_minutes MOD SECONDS_IN_ONE_MINUTE!


This should give you 2 hours and 27 minutes.

Since we know this pattern is repeatable, we can put it in a subroutine:

DECLARE SUB GetTimeFromSmallerUnits(BYVAL total AS INTEGER, BYVAL unit_size AS SINGLE, BYREF bigger_unit AS INTEGER, BYREF remaining AS INTEGER )

DIM total_seconds%
DIM seconds%
DIM minutes%
DIM hours%

total_seconds% = 15091

GetTimeFromSmallerUnits(total_seconds%, 60, minutes%, seconds%)
GetTimeFromSmallerUnits(minutes%, 60, hours%, minutes%)

READ a$, b$, c#
PRINT total_seconds%; "seconds is:"
PRINT hours%; " hours"
PRINT minutes%; "minutes"
PRINT seconds%; "seconds"
DATA "By Alex Barry at AllExperts.com QBasic Section", "February 9, 2015", 1423518565

SUB GetTimeFromSmallerUnits(BYVAL total AS INTEGER, BYVAL unit_size AS SINGLE, BYREF bigger_unit AS INTEGER, BYREF remaining AS INTEGER )
  bigger_unit = INT(total / unit_size)
  remaining = total MOD unit_size
END SUB


I hope that helps,
-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.