CH12伪代码
CH12伪代码
Assignme SET A TO 34 A 34
nt & INCREMENT B B B+1
Sequence
12.2 Assignments
12.2.1 Assigning a value
stores the value that is input in a variable with the identifier Number
INPUT Number
NumberOfGuesses ← 1
The following pseudocode takes the value stored in NumberOfGuesses adds 1 to that value and then stores
the new value back into the variable NumberOfGuesses
NumberOfGuesses ← NumberOfGuesses + 1
Values can be copied from one variable to another. The following pseudocode takes the value stored in
Value1 and copies it to Value2
Value2 ← Value1
The value in Value1 remains the same until it is assigned a different value.
Temp Value1
Value1 Value2
Value2 Temp
The problem to be solved: Convert a distance in miles and output the equivalent distance in km.
Solution:
Km Miles*1.61
OUTPUT “km:”,km
12.3 Logic Statements
IF A < B
THEN
<statement(s)>
ELSE
<statement(s)>
ENDIF
Operator Comparison
= Is equal to
< Is less than
> Is greater than
<= Is less than or equal to
>= Is greater than or equal to
<> Is not equal to
Conditions are either TRUE or FALSE. In pseudocode, we distinguish between the relational operator = (which
tests for equality) and the assignment symbol .
A person is classed as a child if they are under 13 and as an adult if they are over 19. If they are between 13 and 19
inclusive they are classed as teenagers. We can write these statements as logic statements.
A number-guessing game follows different steps depending on certain conditions. Here is a description of the
algorithm.
INPUT Guess
IF Guess = SecretNumber
THEN
ELSE
THEN
ELSE
ENDIF
ENDIF
the number-guessing game might allow the player multiple guesses; if the player has not guessed the secret
number after 10 guesses, a different message is output.
IF Guess = SecretNumber
THEN
ELSE
THEN
ELSE
THEN
ELSE
ENDIF
ENDIF
ENDIF
The problem to be solved: Take three numbers as input and output the largest number.
There are several different methods (algorithms) to solve this problem. Here is one method.
2 Store each of the input values in a separate variable (the identifiers are shown in Table 12.04).
3 Compare the first number with the second number and then compare the bigger one of these with the third
number.
INPUT Number1
INPUT Number2
INPUT Number3
THEN
THEN
OUTPUT Number1
ELSE
OUTPUT Number3
ENDIF
THEN
OUTPUT Number2
ELSE //Number3 is the largest
OUTPUT Number3
ENDIF
ENDIF
The problem to be solved: Take three numbers as input and output the largest number.
2 Input the second number and compare it with the value in BiggestSoFar.
4 Input the third number and compare it with the value in BiggestSoFar
INPUT BiggestSoFar
INPUT NextNumber
THEN
BiggestSoFar NextNumber
ENDIF
INPUT NextNumber
THEN
BiggestSoFar NextNumber
ENDIF
OUTPUT BiggestSoFar
12.4 Loops
The problem to be solved: Take 10 numbers as input and output the largest number.
We need one further variable to store a counter, so that we know when we have compared 10 numbers.
INPUT BiggestSoFar
REPEAT
INPUT NextNumber
Counter Counter + 1
THEN
BiggestSoFar NextNumber
ENDIF
UNTIL Counter = 10
OUTPUT BiggestSoFar
The problem to be solved: Take 10 numbers as input and output the largest number.
We can use the same identifiers as in Worked Example 12.04. Note that the purpose of Counter has changed.
INPUT BiggestSoFar
INPUT NextNumber
THEN
BiggestSoFar NextNumber
ENDIF
NEXT Counter
OUTPUT BiggestSoFar
A rogue value is a value used to terminate a sequence of values. The rogue value is of the same data type but
outside the range of normal expected values.
The problem to be solved: A sequence of non-zero numbers is terminated by 0. Take this sequence as input and
output the largest number.
Note: In this example the rogue value chosen is 0. It is very important to choose a rogue value that is of the same
data type but outside the range of normal expected values. For example, if the input might normally include 0
then a negative value, such as −1, might be chosen.
Look at Worked Example 12.05. Instead of counting the numbers input, we need to check whether the number
input is 0 to terminate the loop.
INPUT BiggestSoFar
REPEAT
INPUT NextNumber
THEN
BiggestSoFar NextNumber
ENDIF
UNTIL NextNumber = 10
OUTPUT BiggestSoFar
This algorithm works even if the sequence consists of only one non-zero input. However, it will not work if the only
input is 0. In that case, we don’t want to perform the statements within the loop at all. We can use an alternative
construct, the WHILE...ENDWHILE loop.
INPUT NextNumber
BiggestSoFar NextNumber
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar NextNumber
ENDIF
ENDWHILE
OUTPUT BiggestSoFar
Before we enter the loop, we check whether we have a non-zero number. To make this work for the first number,
we store it in NextNumber and also in BiggestSoFar. If this first number is zero, we don’t follow the instructions
within the loop. For a non-zero first number this algorithm has the same effect as the algorithm using
REPEAT...UNTIL.
Consider the number-guessing game again, this time allowing repeated guesses.
1 The player repeatedly inputs a number to guess the secret number stored.
2 If the guess is correct, the number of guesses made is output and the game stops.
3 If the number input is larger than the secret number, the player is given the message
4 If the number input is smaller than the secret number, the player is given the message to input a larger
number.
SecretNumber Random
NumberOfGuesses 0
REPEAT
INPUT Guess
NumberOfGuesses NumberOfGuesses + 1
IF Guess > SecretNumber
THEN
ENDIF
THEN
ENDIF
OUTPUT NumberOfGuesses
SecretNumber Random
INPUT Guess
NumberOfGuesses 1
THEN
ENDIF
THEN
ENDIF
INPUT Guess
NumberOfGuesses NumberOfGuesses + 1
ENDWHILE
OUTPUT NumberOfGuesses
The problem to be solved: Take 10 numbers as input and output the sum of these numbers and the average.
Sum1 0. //Sum1 is initialised to zero before we start adding the numbers being input.
INPUT NextNumber
NEXT i
Average Sum1 / 10
The problem to be solved: Take as input two numbers and a symbol. Output a grid made up entirely of the chosen
symbol, with the number of rows matching the first number input and the number of columns matching the
second number input.
For example the three input values 3, 7 and &, result in the output:
&&&&&&&
&&&&&&&
&&&&&&&
We need two variables to store the number of rows and the number of columns. We also need a variable to store
the symbol. We need a counter for the rows and a counter for the columns.
INPUT NumberOfRows
INPUT NumberOfColumns
INPUT Symbol
NEXT ColumnCounter
NEXT RowCounter
Each time round the outer loop (counting the number of rows) we complete the inner loop, outputting a symbol
for each count of the number of columns. This type of construct is called a nested loop.
The problem to be solved: Take as input a chosen symbol and an odd number. Output a pyramid shape made up
entirely of the chosen symbol, with the number of symbols in the final row matching the number input.
For example the two input values A and 9 result in the following output:
AAA
AAAAA
AAAAAAA
AAAAAAAAA
This problem is similar to Worked Example 12.09, but the number of symbols in each row starts with one and
increases by two with each row. Each row starts with a decreasing number of spaces, to create the slope effect.
We need as input:
We need to calculate how many spaces we need in the first row. So that the slope of the pyramid is symmetrical,
this number should be half of the final row’s symbols. We need to set the number of symbols to be output in the
first row to 1.
// Set Values
INPUT Symbol
REPEAT
INPUT MaxNumberOfSymbols
NumberOfSpaces (MaxNumberOfSymbols-1)/2
NumberOfSymbols 1
REPEAT
FOR i 1 TO NumberOfSpaces
NEXT i
FOR i 1 TO NumberOfSymbols
NEXT i
NumberOfSpaces NumberOfSpaces-1
NumberOfSymbols NumberOfSymbols+2
12.5 Modules
Another method of developing a solution is to decompose the problem into sub-tasks. Each sub-task can be
considered as a ‘module’ that is refined separately. Modules are procedures and functions.
Procedure: a sequence of steps that is given an identifier and can be called to perform a sub-task
Function: a sequence of steps that is given an identifier and returns a single value; function call is part of an expression
Note: Because a function returns a value, the function definition states the data type of this value.
The rules for module identifiers are the same as for variable identifiers
CALL SetValues
We can rewrite the top-level solution to our pyramid problem using a procedure for each step, as:
CALL SetValues
REPEAT
CALL OutputSpaces
CALL OutputSymbols
CALL AdjustValuesForNextRow
This top-level solution calls four procedures. This means each procedure has to be defined. The procedure
definitions are:
PROCEDURE SetValues
INPUT Symbol
NumberOfSpaces ← (MaxNumberOfSymbols - 1) / 2
NumberOfSymbols ← 1
ENDPROCEDURE
PROCEDURE InputMaxNumberOfSymbols
REPEAT
INPUT MaxNumberOfSymbols
UNTIL MaxNumberOfSymbols MOD 2 = 1
ENDPROCEDURE
PROCEDURE OutputSpaces
NEXT Count1
ENDPROCEDURE
PROCEDURE OutputSymbols
NEXT Count2
ENDPROCEDURE
PROCEDURE AdjustValuesForNextRow
NumberOfSpaces ← NumberOfSpaces – 1
NumberOfSymbols ← NumberOfSymbols + 2
ENDPROCEDURE
01 CALL SetValues
02 REPEAT
03 CALL OutputSpaces
04 CALL OutputSymbols
This top-level solution calls three procedures. It also makes use of two functions in lines 05.1 and 05.2.
PROCEDURE SetValues
INPUT Symbol
MaxNumberOfSymbols ← ValidatedMaxNumberOfSymbols
NumberOfSpaces ← (MaxNumberOfSymbols - 1) / 2
NumberOfSymbols ← 1
ENDPROCEDURE
REPEAT
INPUT MaxNumberOfSymbols
RETURN MaxNumberOfSymbols
ENDFUNCTION
PROCEDURE OutputSpaces
NEXT Count1
ENDPROCEDURE
PROCEDURE OutputSymbols
ENDPROCEDURE
NumberOfSpaces ← NumberOfSpaces – 1
RETURN NumberOfSpaces
ENDFUNCTION
NumberOfSymbols ← NumberOfSymbols + 2
RETURN NumberOfSymbols
ENDFUNCTION
One benefit of using modules is that individual modules can be reused in other solutions. Therefore, modules
should be designed to be self-contained. That means they should not rely on external variables. All variables that
are required by a module should be passed to it using parameters.
02 REPEAT
03 CALL OutputSpaces(NumberOfSpaces)
Module definitions:
MaxNumberOfSymbols ← ValidatedMaxNumberOfSymbols
NumberOfSpaces ← (MaxNumberOfSymbols - 1) / 2
NumberOfSymbols ← 1
ENDPROCEDURE
REPEAT
INPUT MaxNumberOfSymbols
RETURN MaxNumberOfSymbols
ENDFUNCTION
PROCEDURE OutputSpaces(NumberOfSpaces)
NEXT Count1
ENDPROCEDURE
NEXT Count2
ENDPROCEDURE
NumberOfSpaces ← NumberOfSpaces – 1
RETURN NumberOfSpaces
ENDFUNCTION
FUNCTION AdjustedNumbeOfSymbols(NumberOfSymbols) RETURNS INTEGER
NumberOfSymbols ← NumberOfSymbols + 2
RETURN NumberOfSymbols
ENDFUNCTION
Note that the procedure OutputSpaces uses a variable, Count1, which is used only within the module. Similarly,
OutputSymbols uses variable Count2 only within the module. We call such a variable a local variable (a variable that
is accessible only within the module in which it is declared. Tip: Good design uses local variables as it makes modules independent and re-
usable. ) A variable available to all modules is known as a global variable (a variable that is accessible from all modules)