# BMI program help

November 12 2010 at 9:46 AM

This BMI program is running well, but if anyone has any ideas or input please do so.
Thanks from a new software writing student using qbasic.
This is formated from our teacher to give the corrected output.

REM Body Mass Index Calculator
REM Variables
DIM measureSystem AS STRING
DIM bodyMassIndex AS SINGLE
DIM weightKilograms AS SINGLE
DIM weightPounds AS SINGLE
DIM heightMeters AS SINGLE
DIM heightInches AS SINGLE
CLS

REM output the opening message
PRINT "Body Mass Index Calculator"

REM prompt/input for which measuring system
INPUT "Do you want to use English or Metric units? (m/e) ", measureSystem

REM prompt/input the english/metric value for height
IF measureSystem = "m" THEN INPUT "Enter your height in Meters ", heightMeters
IF measureSystem = "e" THEN INPUT "Enter your height in Inches ", heightInches
REM prompt/input the english/metric value for weight
IF measureSystem = "m" THEN INPUT "Enter your weight in Kilograms ", weightKilograms
IF measureSystem = "e" THEN INPUT "Enter your weight in Pounds ", weightPounds
REM Calculate bodyMassIndex
IF measureSystem = "m" THEN bodyMassIndex = weightKilograms / (heightMeters * heightMeters)
IF measureSystem = "e" THEN bodyMassIndex = 703 * (weightPounds / (heightInches * heightInches))

REM display bodyMassIndex
PRINT "At ";
IF measureSystem = "m" THEN PRINT USING "#.#"; heightMeters;
IF measureSystem = "m" THEN PRINT "m";
IF measureSystem = "e" THEN PRINT USING "##"; heightInches;
IF measureSystem = "e" THEN PRINT " inches";
PRINT " and ";
IF measureSystem = "m" THEN PRINT USING "##.#"; weightKilograms;
IF measureSystem = "m" THEN PRINT "kg.";
IF measureSystem = "e" THEN PRINT USING "###"; weightPounds;
IF measureSystem = "e" THEN PRINT " lb.";
PRINT " your BMI is ";

REM ouput BMI
PRINT USING "##.#"; bodyMassIndex

REM display W.H.O. categories
PRINT "Based on W.H.O. data, at a BMI of ";
PRINT USING "##.#"; bodyMassIndex;
PRINT " you are ";

REM BMI categories
IF bodyMassIndex <= 16.49 THEN PRINT "Severely underweight, or anorexic"
IF (bodyMassIndex >= 16.5) AND (bodyMassIndex <= 18.49) THEN PRINT "Underweight"
IF (bodyMassIndex >= 18.5) AND (bodyMassIndex <= 24.99) THEN PRINT "normal"
IF (bodyMassIndex >= 25) AND (bodyMassIndex <= 29.99) THEN PRINT "Overweight"
IF (bodyMassIndex >= 30) AND (bodyMassIndex <= 34.99) THEN PRINT "Obese"
IF (bodyMassIndex >= 35) AND (bodyMassIndex <= 39.99) THEN PRINT "Clinically Obese"
IF bodyMassIndex >= 40 THEN PRINT "Morbidly Obese"

REM prompt user to do another
INPUT "Do you Want to Do another calculation? (y/n) ", reply
LOOP

REM dispaly ending message
PRINT "Goodbye"

 Respond to this message

R

# A problem

November 13 2010, 6:56 PM
 In the following block of code: IF bodyMassIndex <= 16.49 THEN PRINT "Severely underweight, or anorexic" IF (bodyMassIndex >= 16.5) AND (bodyMassIndex <= 18.49) THEN PRINT "Underweight" IF (bodyMassIndex >= 18.5) AND (bodyMassIndex <= 24.99) THEN PRINT "normal" IF (bodyMassIndex >= 25) AND (bodyMassIndex <= 29.99) THEN PRINT "Overweight" IF (bodyMassIndex >= 30) AND (bodyMassIndex <= 34.99) THEN PRINT "Obese" IF (bodyMassIndex >= 35) AND (bodyMassIndex <= 39.99) THEN PRINT "Clinically Obese" IF bodyMassIndex >= 40 THEN PRINT "Morbidly Obese" What happens if bodyMassIndex is exactly 16.495? No message gets printed. One solution is to take advantage of the difference between < and <=, like so: IF bodyMassIndex < 16.5 THEN PRINT "Severely underweight, or anorexic" IF bodyMassIndex >= 16.5 AND bodyMassIndex < 18.5 THEN PRINT "Underweight" IF bodyMassIndex >= 18.5 AND bodyMassIndex < 25 THEN PRINT "normal" 'etc... A neater way to solve the problem would be to use ELSEIFs, like this: IF bodyMassIndex < 16.5 THEN     PRINT "Severely underweight, or anorexic" ELSEIF bodyMassIndex < 18.5 THEN     PRINT "Underweight" ELSEIF bodyMassIndex < 25 THEN     PRINT "normal" ELSEIF bodyMassIndex < 30 THEN     PRINT "Overweight" ELSEIF bodyMassIndex < 35 THEN     PRINT "Obese" ELSEIF bodyMassIndex < 40 THEN     PRINT "Clinically Obese" ELSE     PRINT "Morbidly Obese" END IF
 Respond to this message
unseen machine

# Select case would be faster

December 5 2010, 1:07 PM
 in terms of computing speed select case is faster than if's
 Respond to this message
Ben

# you sure?

December 5 2010, 10:24 PM
 Respond to this message
Unseen Machine

# Yes

December 7 2010, 9:09 AM
 It takes less computing time, i dont understand it, but if you want more information on it, ask DarthWho Or Codeguy about it at QB64 and they will be happy to create a thread 500000000 pages long about optimisation in qb. They know some really funky stuff!
 Respond to this message

R

# SELECT CASE is a compiler hint

December 29 2010, 4:58 PM
 Obviously, any SELECT CASE (or 'switch' in C/C++) can be rewritten as a functionally equivalent IF-ELSEIF-ELSEIF. But not the other way round. So SELECT CASE is more restricted than IF-ELSEIF. And hence it's easier to optimise. A common technique is to calculate or look up the address to JMP to based on the test expression. But (you may argue), a compiler could recognise one of the "restricted" IF-ELSEIF-ELSEIFs that can be turned into a SELECT CASE and optimise it the same way. However adding such a feature to a compiler may not be an easy task and increases the complexity of the compiler. Furthermore, the use of SELECT CASE can be treated as a compiler hint that it's a good idea to apply a case-based optimisation. (Such an optimisation works best when there are many cases of roughly equal probability.)
 Respond to this message
AlGoreIthm

# Variation without conditionals

January 16 2011, 3:11 PM
 Using an index tables for all messages and calculating the index according to the ranges in the program without using IF-THEN-ELSE or SELECT CASE is elegant and circumvents the possibility of no message being printed. conclusion\$(1) = "Severely underweight or anorexic" conclusion\$(2) = "Underweight" conclusion\$(3) = "normal" conclusion\$(4) = "Overweight" conclusion\$(5) = "Obese" conclusion\$(6) = "Clinically Obese" conclusion\$(7) = "Morbidly Obese" calculation% = 7 + (bmi < 40) + (bmi < 35) + (bmi < 30) + (bmi < 25) + (bmi < 18.5) + (bmi < 16.5) PRINT "This patient is "; conclusion\$(calculation%); ".";
 Respond to this message
Solitaire
S

# * Brilliant!

January 26 2011, 3:42 PM
 Respond to this message
AlGoreIthm

# I like it too - but ... brilliant?

January 29 2011, 5:27 PM
 Respond to this message
AlGoreIthm

February 5 2011, 5:28 AM
 Updated and simplified version of the calculation Using no conditionals IF-THEN-ELSE or SELECT CASE the program sets the grades A B C D and F to points ranging from 0 to 100, by steps of 0.5 SCREEN 12 REM INPUT "What is the student's grade"; grade FOR p = 0 TO 100 STEP .5 ' Rem-ove this line when using INPUT instead grade\$ = CHR\$(65 + ABS(p < 90) + ABS(p < 77.5) + ABS(p < 65) + (2 * ABS(p < 50))) PRINT " "; p; grade\$, NEXT ' Rem-ove this line when using INPUT instead
 Respond to this message
Anonymous

# * By definition the less-than operator is conditional, but other than that...NICE! :D

February 5 2011, 8:12 PM
 Respond to this message
Anonymous