Study guide
home lecture notes lecture notes study guide unit profile Past exams web comp staff details

[ Review questions ] [ Examples ] [ Lecture slides ]

module13

Required reading

---------------------------------------------------------
Textbook Martin 1994
Ch. 4, pp. 88-107
---------------------------------------------------------

Overview

The section on files is optional, but may be useful if you are attempting an optional advanced programming exercise in your assignment work.

In Chapter 1 of the QBasic text Martin discusses the steps of program development. He indicates that there are the following five steps to program development:

An additional step can be added to these in order to reduce logic errors in any program. The main purpose of the Program Design stage is to develop the logic of the program (steps required to solve the problem). Before the program is implemented or coded into a programming language the initial design can be checked for logic errors. This checking process is commonly known as "Desk Checking" the design and can be done on the pseudocode design of a program or the program itself.

Desk checking involves two stages. The first stage is to select a set of test values to be used as input for the program and calculate the correct output that should be obtained using the test values. The second stage is to manually step through the program line by line using the test values as input and noting the values assigned by the program as output. If the value of the output from the program is the same as the value that was calculated manually then the program logic has passed the check.

The desk checking process will not only highlight errors in calculations but also errors in the logic of looping and selection constructs. It is important to note that a desk check on a program design will not necessarily uncover all errors but should allow detection of any major errors.

We will use Program 0 form Example 13.8 to demonstrate the process of desk checking a program for logic errors.

The program is designed to calculate and print the square and cube of inputted numbers. The number 9999 is to be inputted to end the program.

Stage 1-Determine test values and their correct output.

INPUTOUTPUT
CASE No.TEST VALUESQUAREDCUBED
1246
23927
END9999

Stage 2-Step through each line of the program using the test values as input. You must step through the program as if it was being run on the computer. The easiest way to do this is to create a table with a column for each variable. Columns can also be included for DoWhile and If conditions as well as Output columns. The columns you include will depend upon the program. As each line from the program is stepped through it is entered in the first column. The changes resulting to variables from that line are entered in the appropriate column to the right.

Note: This desk check has been done on the program with an error included. The desk check for the corrected program is shown in the second table.

You will also note that the REM statements have not been included as they do not actually perform any processing.

Program StatementAnumDo While
Condition
Output from
Print command
CLS
PrintProgram 13-8-0
PrintEnter 9999 to end
INPUT"",Anum2
Do While Anum <> 9999True
Print "The Square is",Anum^24
Print"The Cube is",Anum^38
Loop
Do While Anum <> 9999True
Print "The Square is",Anum^24
Print"The Cube is",Anum^38
Loop
Do While Anum <> 9999True
Print "The Square is",Anum^24
Print"The Cube is",Anum^38
Loop
...

The desk check table above shows that the program will run in an endless loop using the first value entered into Anum for calculating the output. This is because the Do While condition is always true because Anum (which has been assigned the value 2) does not equal 9999. The desk check has been redone with the following corrected program below.

Program StatementAnumDo While
Condition
Output from
Print command
CLS
PrintProgram 13-8-0
PrintEnter 9999 to end
INPUT"",Anum2
Do While Anum <> 9999True
Print "The Square is",Anum^24
Print"The Cube is",Anum^38
INPUT"",Anum3
Loop
Do While Anum <> 9999True
Print "The Square is",Anum^29
Print"The Cube is",Anum^327
INPUT"",Anum9999
Loop
Do While Anum <> 9999False
PrintEnd Program
END...


Review questions

REVIEW QUESTION 13-1Do the chapter review exercises at the endof the chapter.
REVIEW QUESTION 13-2Try some of the example programs given below.
REVIEW QUESTION 13-3Do at least the first three programming problemsat the end of the chapter.
REVIEW QUESTION 13-4Work through the desk checking examples given below. You will possibly have exam questions like these.
top of page


Examples

EXAMPLE 13-1

REM Program name:		EXLOOP.BAS
REM Program description:	Looping examples
CLS
PRINT "25131 Example program EXLOOP"
PRINT "Very simple loops to print numbers from 1 to 10"
PRINT
PRINT "First loop - WHILE"
x = 1
DO WHILE x < 11
 PRINT x; " ";
 x = x + 1
LOOP
PRINT
PRINT "Second loop - DO UNTIL"
x = 1
DO UNTIL x > 10
 PRINT x; " ";
 x = x + 1
LOOP
PRINT
PRINT "Third loop - FOR .. NEXT"
FOR x = 1 TO 10
 PRINT x; " ";
NEXT x
PRINT
PRINT "Fourth loop - WHILE .. WEND"
x = 1
WHILE x < 11
 PRINT x; " ";
 x = x + 1
WEND
PRINT "End program"
END

EXAMPLE 13-2

Change the average temperature program in Example 12-3 so that there is some check on the data entered. Check that the maximum temperature entered is not less than the minimum temperature entered.

SOLUTION

REM ex13-12.bas Robertson p. 28
REM find average temperature
REM some check on data entry included
PRINT "Enter maximum temperature"
INPUT max.temp
min.temp = 9999
DO WHILE min.temp > max.temp
 PRINT "Enter minimum temperature"
 INPUT min.temp
 IF min.temp > max.temp THEN
	PRINT "*** Error ***"
	PRINT "*** Minimum greater than maximum temperature. Re-enter"
 END IF
LOOP
avg.temp = (max.temp + min.temp) / 2
PRINT "Average temperature = "; avg.temp
END

EXAMPLE 13-3

REM Program name:	EXSOUND.BAS
REM program description; Example of a loop with STEP in FOR..NEXT
REM and the SOUND statement (SOUND frequency, duration)
CLS
PRINT "A sound will continue until you press the Escape key"
DO UNTIL INKEY$ = CHR$(27) 'Character 27 is the escape key"
 FOR Frequency = 300 TO 100 STEP -1
 SOUND Frequency, .5
 NEXT Frequency
LOOP
PRINT "End program"
END

EXAMPLE 13-4

REM Program name: EXCOLOR.BAS
REM Program description: Yet another loop with 
REM the color statement
FOR Color.number = 1 TO 15
 COLOR Color.number
 PRINT "This is color.number"; Color.number
NEXT Color.number
PRINT "End program"
END

EXAMPLE 13-5

REM Program name: EXAVER.BAS
REM Program description:	To show a loop to input data
REM as in some assignments
REM The program inputs numbers until the number 9999
REM is entered. Then it prints the average of the numbers.
COLOR 15, 9 'change colour to white (15) on blue (9) just for fun
CLS
PRINT "25231 Exercise - Input numbers and find average"
PRINT
Total.Nums = 0
Counter = 0
INPUT "Enter a number (or enter 9999 to end): ", Num
DO WHILE Num <> 9999
 Total.Nums = Total.Nums + Num
 Counter = Counter + 1
 INPUT "Enter a number (or enter 9999 to end): ", Num
LOOP
PRINT
IF Counter <> 0 THEN
 PRINT "Average = "; Total.Nums / Counter
ELSE
 PRINT "No numbers entered"
END IF
PRINT "End program"
END

EXAMPLE 13-6

REM Program name:	EX13-6. BAS
REM Program to read 5 numbers and find their average
CLS
PRINT "Program EX13-6"
Total = 0
FOR x = 1 TO 5
	INPUT "Enter a number ", Anum
	Total = Total + Anum 
NEXT x
Average = Total / 5
PRINT "Average =", Average
PRINT "End program"
END

EXAMPLE 13-7

REM Program name:	EX 13-7. BAS
REM Program to read in names and account balances
REM and print total balance. Finish when name = "end"
CLS
PRINT "Program EX13-7"
Total = 0
INPUT "Enter name: ", Aname$
DO WHILE Aname$ <> "end"
	INPUT "Enter account balance : ", Amt
	Total = Total + Amt
	INPUT "Enter name: ", Aname$
LOOP
PRINT "Total balances = ", Total
PRINT "End program"
END

EXAMPLE 13-8

Desk checking problems

Each of the four programs below has something wrong with it. Either a wrong line or a missing line, or an extra line. If a line is missing or extra give three lines to show where the line should be inserted or deleted. Otherwise show how an incorrect line should be corrected.

Program 0

REM Program 13-8-0.bas
REM A program to input numbers and print their
REM square and cube.
REM Finish if 9999 entered.
CLS
PRINT "Program 13-8-0"
PRINT "(Enter 9999 to end)"
INPUT "Enter a number ", Anum
DO WHILE Anum <> 9999
 PRINT "Square is ", Anum ^ 2
 PRINT "Cube is ", Anum ^ 3
LOOP
PRINT "End program"
END

SOLUTION

 PRINT "Cube is ", Anum ^ 3
 INPUT "Enter a number ", Anum	'this line was missing
LOOP

Program 1

REM Program 13-8-1.bas
REM A program to get 5 numbers and
REM find the maximum and minimum number.
REM
CLS
PRINT "Program 13-8-1"
INPUT "Enter a number: "; Anum
Max = Anum 'set max and min to first entry
Min = Anum
FOR Counter = 1 TO 4
 INPUT "Enter a number "; Anum
 IF Anum > Max THEN
 	Max = Anum
 END IF
 IF Anum < Min THEN
 	Min = Anum
 END IF
 Counter = Counter + 1
NEXT Counter
PRINT "Maximum number is "; Max
PRINT "Minimum number is "; Min
PRINT "End program"
END

SOLUTION

END IF
 Counter = Counter + 1	'this line is not needed
NEXT Counter

Program 2

REM Program 13-8-2.bas
REM Program to calculate compound interest
REM given an amount,an interest rate and a number of years
REM Note this is not an efficient formula for compound interest
REM but will do for this exercise.
CLS
PRINT "Program 13-8-2"
INPUT "Enter $ amount ", Amount
INPUT "Enter interest rate % ", I.Rate
INPUT "Enter number of years ", Years
New.Amount = Amount
FOR x = 1 TO Years
 New.Amount = New.Amount + (Amount * I.Rate / 100)
NEXT x
PRINT "Compound interest = ", New.Amount - Amount
PRINT "End program"
END

SOLUTION

New.Amount = New.Amount + (New.Amount * I.Rate / 100) ' corrected line

Program 3

REM Program 13-8-3.bas
REM A program to print the numbers
REM from 100 to 1 in descending order
CLS
PRINT "program 13-8-3"
FOR x = 100 to 0 Step -1
 PRINT x;
NEXT x
PRINT "End program"
END

SOLUTION

FOR x = 100 to 1 Step -  'corrected line

top of page


[ Home ] [ CQU home ] [ Lecture notes ] [ Study guide ]
[ Unit profile ] [ Past exams ] [ Web comp ] [ Staff details ] [ Email list ]

For errors contact the Webmaster.
These pages were designed by BS Computer Solutions.
Copyright © 1997 Central Queensland University. All rights reserved.