12 PROBLEM SOLVING AND ALGORITHM DEVELOPMENT (
The new algorithm, which is a little more refined, can be stated as
Prompt and enter three numbers A, B, C
Calculate the total using the formula TOTAL = A + B +C
Output TOTAL
The algorithm can be further refined. For example 'Enter three
numbers' can be replaced by the word 'lnput as this is really what
Start
you want to do. The words Calculate the total using the formula
can be replaced by the word Let', and the word 'Output' can be
replaced by Print'. Our new algorithm will now be:
Prompt and INPUT A, B, C
LETTOTAL-A+B +C
PRINT TOTAL
Input A, B, C
The steps for refining an algorithm are summarised in Table 12.1
Table 12. 1
Stages of refining an algorithmn
Questions Algorithm Algorithm Algorithm
(rough) refined refined further
- narrative -pseudocode
What is Prompt and Prompt and Promptand
Let TOTAL the input enter the three enter three INPUT A, B, C
A + B+ C
instruction? numbers numbers A,
B, C
What is the Calculate the LET TOTAL=A
Calculatethe
processing Sum of the total using the +B+C
instruction? three numbers formula:
TOTAL =A+ B
PRINT What is
C
Display the sum Output TOTAL PRINT TTAL
the output of the three
instruction? numbers
The flowchart for the
example in Figure 12.6 will have the sa1
symbols as the one we have seen before, but the content or
boxes will be changed to reflect u
the more refined algoritnnl.
END
Figure 12.6 Flowchart: The sum of
three numbers
220
Refining an algorithm
Let us look at another example.
Problem 4
Convent a temperature in degrees Celsius to degrees Fahrenheit.
Possible algorithmn
The summary for the steps to writing the possible algorithm is
shown in Table 12.2.
Start
Table12.2 Converting termperature
Question Algorithm Algorithm Algorithm
- narrative
refined refined further
- pseudocode
What is Enter the Prompt and Prompt and
the input temperature in enter the INPUT C
Prompt and instruction? degrees Celsius temperature in
INPUT C
that you would degrees Celsius
like to convert. C
(The letter C
isthe variable
used to hold
the value
Let F = (32 + 9 *C/5)
of degrees
Celsius.)
What is the Convert to Convert to LETF=
processing degrees degrees (32 +9 C/5)
instruction? Fahrenheit Fahrenheit
by using the by using the
formula: formula: F =
Degrees (32+9*C/5)
PRINT C and Fahrenheit = The letter F
(32 +9 is the variable
degrees Celsius used to hold
/5 the value
of degrees
Fahrenheit
What is Display the Output C and F PRINT C and F
the output temperature
END
instruction? in degrees
Celsius and the
Converted value|
in degrees
Figure 12.7 Flowchart: Convert
Fahrenheit.
temperature
In a flowchart, the algorithm would look like this one in Figure 12.7.