JMA201-01
Fundamentals of Application Development

DEPARTMENT SCHEDULE SYLLABUS RESOURCES
Algorithms

An algorithm is a listing of the steps that we will need to create a program. It is, in essence, an outline for developing your program - you should think about the process your program will follow before you begin attempting to implement. Many different structures will appear in your program - different subroutines, event handlers, and so on; an algorithm allows you to prepare these elements before you write them, so you can plan effectively!

Algorithms are often written either in English, or as pseudocode - code written with some keywords to help remind yourself of the functions you might need to implement, but code that is NOT syntactically correct.

Finding My Formula

I researched the BMI formula - it is expressed in this form:

Table: BMI Formula
BMI  =  
(weight in pounds * 703 )
————————————
height in inches²

We now need to convert this to programming form, using Order Of Operations:

Next, I would convert the "friendly" value names to VB appropriate variable names:

Now, write as one expression:

Simple Algorithm

For a Body Mass Index (BMI) calculator, here is a sample algorithm we might write, in English:

  1. Elements Required:
    1. Box for Weight
    2. Box for Height
    3. Button to run command
    4. Label for weight
    5. Label for height
    6. Variable (in code) for height
    7. Variable (in code) for weight
    8. Variable (in code) for answer
  2. On Program Startup:
    1. Show form
  3. On Button Press:
    1. Check to see that a value is entered for weight
    2. Check to see that a value is entered for height
    3. Check to see that the weight is a number
    4. Check to see that the height is a number
    5. If any of these checks fail, go to step 4
    6. If all of these checks pass, go to step 5
  4. If there was a problem (a number was not entered):
    1. Display error message to the user
    2. Highlight incorrect value
    3. Wait at step 3
  5. If there was no problem
    1. Convert height to number
    2. Put into height variable
    3. Convert weight to number
    4. Put into weight variable
    5. Find answer variable according to formula

Form Elements

What form elements do I need? The values I need should suggest the textboxes I'll need on my form - I'll need:

Pseudocode

And, now we can start writing some pseudocode:

  • For the button's subroutine event handler (click):
    • declare (dim) weightInPounds as integer
    • declare (dim) heightInInches as integer
    • declare (dim) BMI as decimal (because it won't be a whole number)
    • if txtInputWeight.text and txtInputHeight.text are both numbers:
      • weightInPounds=txtInputWeight.text
      • heightInInches=txtInputHeight.text
      • BMI=(weightInPounds * 703 )/heightInInches^2
      • lblAnswer.text=BMI
    • otherwise (one of the values wasn't a number):
      • Show error message
      • Wait for user to press button again.