This lecture focuses on step 4 of the Steps in Program Development - Specifying the Algorithm
An algorithm is a set of instructions or steps to accomplish a task.
Algorithms are used in many activities and come in many forms. Instructions for assembling kits (e.g. furniture, toys), recipes, steps for processing credit card approvals, directions to a destination, all involve algorithms.
It is important that algorithms are unambiguous and precise as possible. To make an algorithm easier to follow conventions are used, such as similar layout and terminology. For example recipes usually have two main sections: ingredients and method. Poorly written instructions are a source of frustrations - missing steps, ambiguous statements etc all contribute to wasted time, poor or incorrect results....
Algorithms for making things will often be divided into sections; the parts/components/ingredients (inputs) required to accomplish the task and actions/steps/methods (processing) to produce the required outcome (output). For example to build a model car, the parts (inputs) are needed plus instructions on how to assemble the car (processing) and the result is the car (output).
Different problem types have differing conventions, for programming problems we will be using Defining Diagrams, Pseudo Code and Data Dictionaries.
| Oven temperature: 180 C Cooking time: 10 minutes
Number: 24 Ingredients
1 cup s.r. flour |
Small Sponge Cakes
Method
Egg Sponge Method
Cooking the Australian Way, p262-3 |
|
The above recipe shows some similarities to algorithms used for programming:
Steps 1 to 6 shows a sequence: a series of steps performed one after the
other.
Step 2 shows a reference to a commonly used task that is specified in detail elsewhere: Follow
Method for Egg Sponge from Step a to Step d.
Step c shows repetition: "Beat egg white until stiff" -
something is repeated until some condition is met.
Step 6 shows selection: "Ice with glace or melted butter icing" -
One of two possible actions is taken.
Pseudo Code (which means fake code, because its not really programming code) specifies the steps required to accomplish the task. Pseudo Code is a type of structured English that is used to specify an algorithm.
Some advantages of using pseudo code to specify an algorithm rather than immediately writing program code are:
Pseudo Code is developed from the defining diagram and the outline of the solution . The Pseudo Code specifies the algorithm in detail and the Data Dictionary describes the data used in the algorithm.
A Pseudo Code Guide is available under Other Resources. This covers the style of pseudo code that will be used in this subject. Initially only a small part of the guide will be applicable. Over the next few weeks it will be discussed in more detail.
At the moment we will be looking at three main statements (operations): Assignment, Input and Display:
Assignment is used to store a value. This might involve either (a) simply storing a value or (b) calculating the answer to an arithmetic problem and then storing the result. The equals sign (=) is usually used to indicate assignment.
(a) Storing a Value
Examples
total = 0
The first example means: store zero in the variable named total.
(b) Arithmetic Calculations
Arithmetic operators are used in calculations. The operators are:
| Operator | Meaning | Example |
| () | Brackets. Grouping | y = (a + b) * (c + d) |
| * | Multiply | x = a * b |
| / | Divide | average = total / count |
| + | Add | i = i + 1 |
| - | Subtract | y = x - 0.5 |
Examples
area = length * width
The first example means: multiply the value stored in length by the value stored in width and store the result in area. The third example adds 1 to the value already in count.
Display a message asking the user for a value and store the value typed by the user in a variable.
Examples
Input custName
The first example means display a message asking the user to input a customers name and store the value typed by the user in the variable called custName.
Displays data on the computer screen (monitor).
Examples
Display "Width = ", width Display "Hello World" Display grossIncome, taxPayable
Values in quotation marks are displayed exactly as stated (minus the quotation
marks)
The values held in variables are displayed rather than the variable name.
For example, if the variable width held the value 72, the first Display statement above would display the following:
Width = 72
The pseudo code by itself doesn't provide enough information to be able to write program code. Data Dictionaries are used to describe the data used in the Pseudo Code. Data Dictionaries accompany the Pseudo Code and state what things have been used - for programs this is the data that has been used to solve the problem.
The data dictionary has 3 columns: Name, Data Type and Description (in that order). The data recorded is the identifier name (e.g. variable name), data type and a description of the identifier. The table is sorted into alphabetical order on the identifier name. The standard data types used in Pseudo Code are Integer, Double, String, Char and Boolean. The identifier descriptions should be brief but meaningful. Try to avoid just restating the name in the description. Specify units of measure where appropriate e.g. cm, minutes, dollars. Provide example data where it helps the description.
| Name | Data Type | Description |
| number1 | Integer | The first number to be added |
| number2 | Integer | The second number to be added |
| total | Integer | The total of number1 and number2 added together |
The following example is used to demonstrate the steps involved in Program Development and focuses on Step 4 Developing the Algorithm. More detail on steps 5 through 7 will be given in later lectures.
Input two numbers, add the two numbers together and display their total.
Run Number 1 ? 10 Number 2 ? 3 Total = 13
Note: This is a simple problem, however it allows the concepts and techniques used to be demonstrated. Each of the steps below will be revisited in more detail later in the subject. This is just to give an overview of the processes that will be discussed in class. Steps 1 & 2 were covered in the previous lecture. Steps 5 and 6 will be covered in later lectures.
Step 1a: Carefully read the problem, a number of times if necessary, writing down
any key information, highlighting keywords etc.
Step 1b: Check your understanding of the problem.
Study the problem identifying the inputs, processing and outputs.
Step 2a: Determine the outputs (result) required
Step 2b: What processing is required to get to obtain the result
Step 2c: What inputs are needed
Step 2d: Develop the Defining Diagram
| Inputs | Processing | Outputs |
| number1, number2 | Input two numbers. Add the numbers together Display the total |
total |
Where problems are more complex further refinement of the problem may be required e.g. breaking it into smaller parts, providing more details for some steps etc. This is a simple problem and requires no more detail.
| Name | Data Type | Description |
| number1 | Integer | The first number to be added |
| number2 | Integer | The second number to be added |
| total | Integer | The total of number1 and number2 added together |
A Desk Check is a manual check of the algorithm, to ensure that it is correct.
Pseudo Code (with line numbers shown)
Inputs: number1 = 10, number2 = 3
Correct result: total = 13
' Name: addTwoNumbers ' Purpose: To add 2 number together and display the result ' Author: Tim Whitfort
Option Compare Database Option Explicit
Sub Main()
' Variable declarations
Dim number1 As Integer
Dim number2 As Integer
Dim total As Integer
' Input the numbers
number1 = InputBox("Number 1 ? ")
number2 = InputBox("Number 2 ? ")
' Add the numbers together
total = number1 + number2
' Display the total
Debug.Print "Total = "; total
End Sub
A Test Plan is devised to specify test cases that will help determine if the program works correctly. Test Runs are performed using the test plan to see if any errors are found in the program.
| Test Number | Reasons | Inputs | Expected Outputs | Actual Outputs | Correct? |
| 1 | Use the data provided. Two positive numbers | number1 = 10 number2 = 3 |
total = 13 | total = 13 | Yes |
| 2 | Two zero numbers | number1 = 0 number2 = 0 |
total = 0 | total = 0 | Yes |
| 3 | A negative and positive number | number1 = 3 number2 = -7 |
total = -4 | total = -4 | Yes |
This is a very simple problem. Only a few tests are required to test the program. Correct results were obtained.
Locate and fix the errors detected during testing.
Nothing required at the moment for the program, however...
Programs (systems) may be used for many years, with 10+ years not being uncommon, and some systems may last over 20 years. Over this time it is likely that programs will need to be changed to: fix errors, meet new requirements, add new functionality, store different data, improve the user interface etc. It is important that the program and its documentation are easy to understand, and that the program was well designed and written. Spending the time to do things right in the first place saves time and money in the short, medium and long term.
Written by Tim Whitfort.