Lecture 5 - Pseudo Code & Desk Checking


Home


Objectives

Contents


This lecture focuses on step 5 of the Steps in Program Development - Desk Checking

1. Desk Checking

A desk check is a manual (non computerised) technique for checking the logic of an algorithm. The person performing the desk check effectively acts as the computer, using pen and paper to record results. The desk checker carefully follows the algorithm being careful to rigidly adhere to the logic specified. The desk check can expose problems with the algorithm.

Desk checks are useful to check an algorithm (before coding) thereby confirming that the algorithm works as expected. This can save time as the programmer doesn't waste time writing a program that doesn't do what is intended.

A desk check is done as a table, with columns in the order shown below:

  1. A pseudo code line number column. Pseudo code doesn't normally have lines numbers, but these are necessary in a desk check to specify the line being executed.
  2. One column per variable used. The variables should be in alphabetic order. State the variable name at the top of the column. As the algorithm is executed, the new values of the variables are put in the appropriate column. Show the working for all calculations.
  3. A condition column. Show the working for the evaluation of any condition in this column. The result of the condition will be True (T) or False (F).
  4. An Input/Output column is used to show what is input by the user and displayed by the program. Show inputs with: the variable name, followed by a "?" and the value input e.g. number1 ? 10. Show outputs with the variable name, an =, and the value displayed e.g. total = 13

Pseudo Code (with line numbers shown)

1 addTwoNumbers()
2     Input number1, number2
3     total = number1 + number2
4     Display total
5 STOP

Test data: number1 = 10, number2 = 3, total = 13

Line Number number1 number2 total Input/Output
1        
2 10 3   Number 1 ? 10
Number 2 ? 3
3     10 + 3 =13  
4       total = 13
5        

A more compact version of the above is normally used and is shown below. As line 1 doesn't perform any action (it would be a blank row in the desk check), it is combined with next line of code to be executed and shown in a single row in the desk check.

Line Number number1 number2 total Input/Output
1, 2 10 3   Number 1 ? 10
Number 2 ? 3
3     10 + 3 =13  
4       total = 13
5        

Example Problems

It is important to have a go at problems. Attempting and solving problems is essential in learning to program. With time you will begin to develop a knowledge base of problem types to draw on. These can be used to help solve problems.

Don't expect to get it right the first time. Look at your solution - does it do what is required? A Desk Check is a useful tool for checking that the algorithm performs as required. Modify your solution as necessary. Learn from your successes and your mistakes.

Problem 1: Hello user-name

Write a program that allows a user to input their name, then displays Hello student-name.

Example Runs

Run
Name ? Jane
Hello Jane
Run
Name ? Peter Smith
Hello Peter Smith

1. Understand the Problem

  1. Read the Problem
  2. Check understanding

name = "Jane"
Display "Hello Jane"

2. Specify a High Level Solution (Defining Diagram)

  1. Determine the output(s) (result) required:
  2. What processing is required to get to obtain the result:
  3. What inputs are needed:
  4. Develop the Defining Diagram
Inputs Processing Output
name Input name
Display name
name

3. Develop the Outline

This is a simple problem and requires no more detail.

4. Develop the Algorithm (Pseudo Code and Data Dictionary)

hello()
    Input name
    Display "Hello ", name
STOP

Data Dictionary

Name Data Type Description
name String The name of a person

5. Test the Algorithm (Desk Checking)

1 hello()
2     Input name
3     Display "Hello ", name
4 STOP

Inputs: name = Jane;
Correct results: Hello Jane

Line Number name Input/Output
1, 2 Jane Name ? Jane
3   Hello Jane
4    

Inputs: name = Peter Smith;
Correct results: Hello Peter Smith

Line Number name Input/Output
1, 2 Peter Smith Name ? Peter Smith
3   Hello Peter Smith
4    

Problem 2: Square and Cube of x

A program is required to display the square and cube of a number input by a user..

Example Runs

Run
x ? 2
x squared = 4
x cubed = 8
Run
x ? 5
x squared = 25
x cubed = 125

1. Check understanding

 
 

2. Specify a High Level Solution (Defining Diagram)

Inputs Processing Outputs
   




 

3. Develop the Outline

 

4. Algorithm (Pseudo Code & Data Dictionary)

 
 
 
 
 
 
 
 

Data Dictionary

Name Data Type Description
     
     
     

5. Test the Algorithm (Desk Checking)

 
 
 
 
 
 
 

Inputs: x = 2;
Correct results: xSquared = 4, xCubed = 8

Line Number x      xCubed      xSquared      Input/Output        
         
         
         
         
         

Inputs: x = 5;
Correct results: xSquared = 25, xCubed = 125

Line Number x      xCubed      xSquared      Input/Output        
         
         
         
         
         

Problem 3: Supermarket Item Value

It can be difficult to compare the price of items in a supermarket when the items are different sizes. In some countries, supermarkets display a price per 100 grams for items to make comparisons easier. For example, which is the better value, 250g of honey at a cost of $5.00, or 500g for $8.00, or 375g for $5.95? Write a program to allow the user to input the weight in grams and the cost of an item, and display the cost per 100g. Hint: the weight needs to be converted to lots of 100g (e.g. divide the weight in grams by 100).

Example Runs

Run
Weight (g) ? 250
Cost ($) ? 5.00
Cost per 100g = $2.00
Run
Weight (g) ? 500 
Cost ($) ? 8.00
Cost per 100g = $1.60

1. Check understanding

weight = 250, cost = $5.00.
weight in lots of 100 grams = 250 / 100 = 2.5.
cost per 100g  = cost / weight in lots of 100 grams = 5.0 / 2.5 = $2.00

weight = 500, cost = $8.00:
weight in lots of 100 grams = 500 / 100 = 5.0.
cost per 100g  = cost / weight in lots of 100 grams = 8.00 / 5.0 = $1.60

2. Specify a High Level Solution (Defining Diagram)

Inputs Processing Outputs
weight
cost
Input weight, cost
Calculate weightInLotsOf100Grams
Calculate costPer100Grams
Display costPer100Grams
costPer100Grams

3. Develop the Outline

This is a simple problem and requires no more detail.

4. Algorithm (Pseudo Code & Data Dictionary)

 
 
 
 
 
 

Data Dictionary

Name Data Type Description
cost Double The cost of the item in $.
costPer100Grams Double The cost in $ of 100 grams of the item.
weight Integer The weight of the item in whole grams.
weightInLotsOf100Grams Double The weight of the item in 100 grams lots. e.g. an item weighing 325grams is 3.25 x 100g

5. Test the Algorithm (Desk Checking)

 
 
 
 
 
 

Inputs: weight = 250, cost = 5
Correct results: costPer100Grams = 2.0

Line Number cost costPer100Grams weight weightInLotsOf100Grams Input/Output
           
           
           
           
           

Inputs: weight = 500, cost = 8
Correct results: costPer100Grams = 1.6

Line Number cost costPer100Grams weight weightInLotsOf100Grams Input/Output
           
           
           
           
           

What's Next

In the next lecture we will look at converting an algorithm into a program and introduce programming in Visual Basic.

Key Points

Further Reading


Written by Tim Whitfort.