Unit 12 Algorithm Design & Problem-Solving
Unit 12 Algorithm Design & Problem-Solving
Decomposition
Decomposition means breaking tasks down into smaller parts in order to explain a process more
clearly. Decomposition is another word for step-wise refinement. This led us to structured
programming using procedures and functions with parameters.
Pattern recognition
Pattern recognition means looking for patterns or common solutions to common problems and
exploiting these to complete tasks in a more efficient and effective way.
Abstraction
Abstraction involves filtering out information that is not necessary to solving the problem.
Abstraction gives us the power to deal with complexity. An algorithm is an abstraction of a
process that takes inputs, execute-s a sequence of steps, and produces outputs. An abstract data
type defines an abstract set of values and operations for manipulating those values.
Data modelling
Data modelling involves analyzing and organizing data. Simple data types such as integer,
character and Boolean. The string data type is a composite type: a sequence of characters. When
we have groups of data items, we used one-dimensional (lD) arrays to represent linear lists and
two-dimensional (2D) arrays to represent tables or matrices. We stored data in text files. In
Chapter 10, we used data modelling to design database tables. We can set up abstract data types
to model real-world concepts, such as records, queues or stacks. When a programming language
does not have such data types built-in, we can define our own by building them from existing
data.
Algorithm design
Algorithm design involves developing step-by-step instructions to solve a problem.
Expressing Algorithms
Algorithm can be written in three ways.
1. Assigning a value
Number ← 1
2. Updating a value
Number ← Number + 1
Number ← Number – 1
3. Copying a value
Number2 ← Number1
Constant
Constants are like variables in their declarations and the ability to look at the value stored inside
them; however, you cannot change the values while the program is running, the value of a
constant remains (rather unsurprisingly) constant.
Example
Convert a distance in miles and output the equivalent distance in km (1km = 1.6 miles).
Logic Statements
A number-guessing game follows different steps depending on certain conditions. Here is a
description of the algorithm.
• The player inputs a number to guess the secret number stored.
• If the guess was correct, output a congratulations message.
• If the number input was larger than the secret number, output message “secret number is
smaller”.
• If the number input was smaller than the secret number, output message “secret number
is greater”.
We can re-write the number-guessing game steps as an algorithm in pseudocode:
SET value for secret number
INPUT Guess
IF Guess = SecretNumber
THEN
OUTPUT "Well done. You have guessed the secret number"
ELSE IF Guess > SecretNumber
THEN
OUTPUT "secret number is smaller"
ELSE
OUTPUT "secret number is greater"
ENDIF
ENDIF
More complex conditions can be formed by using the logical operators AND, OR and NOT. For
example, 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
OUTPUT "Well done. You have guessed the secret number"
ELSE IF Guess > SecretNumber AND NumberofGuesses = 10
THEN
OUTPUT "You still have not guessed the secret number"
ELSE IF Guess < SecretNumber
THEN
OUTPUT "secret number is smaller"
ELSE
OUTPUT "secret number is greater"
ENDIF
ENDIF
ENDIF
Nested IF Statement
Conditional statements within conditional statements.
This seems awfully cumbersome and we will now look a more elegant way of solving this, using
Nested IF's. First of all, nested means placing one thing inside another, so we are going to place
an IF inside another.
Case Statement
Create a program where someone types in the name of an animal and it outputs the sound the
animal makes. The animals it should handle are:
• Pig - Oink
• Cow - Moo
• Bear - Grr
• Sheep - Baa
• Tiger - Grr
• everything else - Meow
Try and complete this task by only using 5 case statements.
Declare animal: String
Input animal
Case animal Of
"Pig”: Print "Oink"
"Cow": Print "Moo"
"Bear", "Tiger": Print "Grr"
"Sheep": Print "Baa"
Otherwise: Print "Meow"
End Case
Loops/Iterations
BiggestSoFar ← -100
Counter ← 1
REPEAT
INPUT NextNumber
Counter ← Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
UNTIL Counter = 10
OUTPUT BiggestSoFar
BiggestSoFar ← -100
Counter ← 1
While Counter <> 10
Do
INPUT NextNumber
Counter ← Counter + 1
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
END While
OUTPUT BiggestSoFar
Rogue value
A value used to terminate a sequence of values.
Repetition using a rogue value
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.
BiggestSoFar ← -100
REPEAT
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
UNTIL NextNumber = 0
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
WHILE NextNumber <> 0 DO // sequence terminator not encountered
INPUT NextNumber
IF NextNumber > BiggestSoFar
THEN
BiggestSoFar ← NextNumber
ENDIF
ENDWHILE
OUTPUT BiggestSoFar
SecretNumber ← Random()
NumberOfGuesses ← 0
REPEAT
INPUT Guess
NumberOfGuesses ← NumberOfGuesses + 1
IF Guess > SecretNumber
THEN
// the player is given the message to input a smaller number
ENDIF
IF Guess < SecretNumber
THEN
// the player is given the message to input a larger number
ENDIF
UNTIL Guess = SecretNumber
OUTPUT NumberOfGuesses
SecretNumber ← Random()
INPUT Guess
NumberOfGuesses ← 1
WHILE Guess <> SecretNumber DO
IF Guess > SecretNumber
THEN
// the player is given the message to input a smaller number
ENDIF
IF Guess < SecretNumber
THEN
// the player is given the message to input a larger number
ENDIF
INPUT Guess
NumberOfGuesses ← NumberOfGuesses + 1
ENDWHILE
OUTPUT NumberOfGuesses
Calculating running totals and averages
The problem to be solved: Take 10 numbers as input and output the sum of these numbers and
the average.
RunningTotal ← 0
FOR Counter ← 1 TO 10
INPUT NextNumber
RunningTotal ← RunningTotal + NextNumber
NEXT Counter
OUTPUT RunningTotal
Average ← RunningTotal / 10
OUTPUT Average
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:
&&&&&&&
&&&&&&&
&&&&&&&
INPUT NumberOfRows
INPUT NumberOfColumns
INPUT Symbol
FOR RowCounter ← 1 TO NumberOfRows
FOR ColumnCounter ← 1 TO NumberOfColumns
OUTPUT Symbol // without moving to next line
NEXT ColumnCounter
OUTPUT Newline // move to the next line
NEXT RowCounter
Stepwise Refinement: Breaking down the steps of an outline solution into smaller and
smaller steps.
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.
Local variable: A variable that is accessible only within the module in which it is declared.
Global variable: A variable that is accessible from all modules.