You are here:

Qbasic, Quickbasic/Basic Programming high/low

Advertisement


Question
I'm having trouble with this problem its not running I think everything is correct but must missing something.
So far I have this also how do you properly underline heading please help!
21 trip-code
CLS
READ NUM
DO WHILE NUM <> 21
IF NUM > 0 THEN
HNUM = NUM
ELSE LNUM =NUM
END IF
READ NUM
LOOP
PRINT "HIGHEST"; HNUM "LOWEST" LNUM
DATA 10, 2,0,-6,5,72,60,1,61,4,-80,-14,90,89,100,-42,17,55,48,21

Answer
Hi Ashley,

First I want to say I'm a little confused; a fellow named Brad asked a question that had nearly-identical code to what you've posted (not to mention both mention "trip code"), which is an astronomically large coincidence (or your friends/in a class together/etc.).  Regardless, I can help you out.

I see a few issues with your code, some of which are nearly identical to Brad's previous code.

The first is your DO/LOOP is constructed in an unnecessarily complex way, which forces you to perform a READ outside of your loop (which makes your code less readable).  Here is how I'd setup the loop:

DO
READ NUM
'' Your IF statements go here
LOOP UNTIL NUM = 21

Next, your code to track and highest and lowest number isn't quite right, but you have the right general idea.  You'll want to be comparing against your HNUM and LNUM values.

HNUM = 0
LNUM = 0
DO
READ NUM
IF NUM > HNUM THEN HNUM = NUM
IF NUM < LNUM THEN LNUM = NUM
LOOP UNTIL NUM = 21

The above code will work in a limited sense, but in the case that all your numbers in your data statement are less than 0, the code will not work before the initial HNUM and LNUM are set to 0, which will alter your results.  To fix this, we need to know whether or not HNUM or LNUM have been set - and it's easiest to just make sure both HNUM and LNUM are set to the value of the first READ statement, and this is how I'd do it:

SETNUMBERS = 0
HNUM = 0
LNUM = 0
DO
 READ NUM
 IF SETNUMBERS = 0 THEN
   HNUM = NUM
   LNUM = NUM
   SETNUMBERS = 1
 END IF
IF NUM > HNUM THEN HNUM = NUM
IF NUM < LNUM THEN LNUM = NUM
LOOP UNTIL NUM = 21

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.