MIC 110

Algorithm

Objectives:

  • To introduce methods of analyzing a problem and developing a solution.
  • To develop simple algorithms using the sequence control structure.
  • To introduce methods of manually checking the developed solution

Defining the problem:

Earlier we talked about seven steps in developing a computer program. The first step, defining the problem, is the most critical step of all. It involves careful analysis of the problem in order to understand what is required to solve it.

For effective analysis, the problem could be divided into three components:

  1. Input: What are the input data? Where are the data coming from?
  2. Output: What outputs are required?
  3. Processing: What are actions to produce the required outputs?

Example: A program is required to read three numbers, add them together, and print the total.

Input
Process
Output

Num1

Num2

Num3

Read three numbers

Add numbers together

Print Total

Total

Or

Read Num1, Num2, and Num3

Total = Num1 + Num2 + Num3

Print Total

Here is another example.

Meaningful names and conventions

  • Using unique names for each memory locations, variables, and controls is essential in writing programs that is easier to understand.
  • Naming system should have some basic rules. These are conventions that are widely used, but you do not have to follow depends on the situation:
    • Put three-letters data type identification. E.g. dbl in dblAmount to indicate that it is a double data type. cur for curTotalAmt to indicate that it is a currency data type. (this convention is rarely used, but it is helpful for special situations)
    • Put three-letter control type identification. E.g. lblAmount to indicate that it is a label control. Use txt for text control. Use cmd for commend control.
    • If you use abbreviation, be consistent. E.g. use consonants to abbreviate long words. ( e.g. pmnt for payment ) If the first letter of the word is a vowel, add it to the name. (E.g. amt for amount)
    • For names of combined word,
      • use either upper-case letter for the first letter of the word. E.g. TotalAmount for total amount.
      • use an underscore between words. E.g. Total_Amount
    • Keep it short and precise. It is important to decide the names before coding a program.
    • Use verb for actions. E.g. a command button that calculates numbers could be called cmdCalc as in calculate.

Exercise: Convert the following analysis into common English (algorithm).

Input
Process
Output

max_temp

min_temp

Prompt for temperatures

Get max and min temperatures

Calculate average temperature

Display average temperature

avg_temp

Notice that all the process are written with verbs.

Your algorithm:

 

 

 

 

 

 

 

 

Designing a solution algorithm

Example: Design a program that converts temperatures in Fahrenheit to Celsius. The formula is C = (F - 32) * 5 / 9 Study the figures below. Use the Project Worksheet on the next page.

The application window:

Coding:

Private Sub cmdConvert_Click()

Dim temF As Integer

Dim temC As Integer

temF = Int(txtFah.Text)

temC = (temF - 32) * 5 / 9

txtCel.Text = Str(temC)

'the Str function converts numeric values to string. It's optional.

End Sub

Simplified but less efficient coding:

Private Sub cmdConvert_Click()

temF = txtFah.Text

temC = (temF - 32) * 5 / 9

txtCel.Text = temC

End Sub

Once you complete the Project Worksheet, check the solution algorithm with test data to verify the correctness of it. There are six simple steps for checking an algorithm.

  1. Choose simple input test cases that are valid. Usually two or three cases are sufficient.
  2. Establish what the expected results should be for the test cases by performing the calculation yourself. It is when you use a calculator. E.g. (100 - 32) * 5 / 9 is 38.
  3. Make a table of the relevant variable names within the algorithm on a piece of paper.
  4. Walk the first test case through the algorithm, keeping a step-by-step record of the contents of each variable in the table as the data passes through the logic.
  5. Repeat the walk-through process using the other test data cases.
  6. Check the results from step 4 and 5 with the expected results from step 2.

Using the example above, fill out the project worksheets.