4.2.3 Discuss an algorithm to solve a specific problem.
4.2.4 Analyse an algorithm presented as a flowchart.
4.2.5 Analyse an algorithm presented as pseudocode
4.2.6 Construct pseudocode to represent an algorithm.
4.2.7 Suggest suitable algorithms to solve a specific problem.
4.2.9 Determine the number of times a step in an algorithm will be performed for given input data.
A finite sequence of precise instructions for performing a computation or for solving a problem
A set of unambiguous instructions for solving a problem or subproblem in a finite amount of time using a finite amount of data.
Input -An algorithm has input values from a specified set.
Output -From each set of input values an algorithm produces output values from a specified set. The output values are the solution to the problem.
Definiteness -The steps of an algorithm must be defined precisely.
Correctness -An algorithm should produce the correct output values for each set of input values.
Finiteness -An algorithm should produce the desired output after a finite (but perhaps large) number of steps for any input in the set.
Effectiveness -It must be possible to perform each step of an algorithm exactly and in a finite amount of time.
Generality -The procedure should be applicable for all problems of the desired form, not just for a particular set of input values
a notation resembling a simplified programming language, used in program design
It uses the structural conventions of a normal programming language, but is intended for human reading rather than machine reading.
It typically omits details that are essential for machine understanding of the algorithm, such as variable declarations
The purpose of using pseudocode is that it is easier for people to understand than conventional programming language code, and that it is an efficient and environment-independent description of the key principles of an algorithm.
A flowchart is the graphical representation of an algorithm that uses different symbols, shapes and arrows in order to demonstrate a process or a program.
The main purpose of a flowchart is to analyze different processes.
Step 1: Read amount,
Step 2: Read years,
Step 3: Read rate,
Step 4: Calculate the interest with formula "Interest=Amount*Years*Rate/100
Step 5: Print interest,
Flowchart by Orsen
Step 1: Initialize X as 0,
Step 2: Increment X by 1,
Step 3: Print X,
Step 4: If X is less than 20 then go back to step 2.
Flowchart by Abraham LW
Pseudocode by Brayton
X = 0
do
X = X + 1
output X
loop while X < 20
Step 1: Read number N,
Step 2: Set remainder as N modulo 2,
Step 3: If remainder is equal to 0 then number N is even, else number N is odd,
Step 4: Print output.
prompt user for 3 numbers
calculates total of 3 numbers
calculates the average of 3 numbers
if average is >75 print pass
Flowchart by Orsen
1. Write "Enter the temperature"
2. Read temperature
3. Determine dress
input TEMPERATURE
if(TEMPERATURE > 90)
output "Texas weather: wear shorts"
else if(temperature > 70)
output "Ideal weather: short sleeves are fine"
else if(temperature > 50)
output "A little chilly: wear a light jacket"
else if(temperature > 32)
output "Philadelphia weather: wear a heavy coat"
else
output "Stay inside"
end if
Assume array ARRS[] contains 10 integers.
Search array ARRS[] for an integer input N and output the INDEX location of the number if found, -1 otherwise
The array STOCK contains a list of 1000 whole numbers (integers). The following pseudocode presents an algorithm that will count how many of these numbers are non-zero, adds up all those numbers and then prints the average of all the non-zero numbers (divides by COUNT rather than dividing by 1000).
COUNT = 0
TOTAL = 0
loop N from 0 to 999
if STOCK[N] > 0 then
COUNT = COUNT + 1
TOTAL = TOTAL + STOCK[N]
end if
end loop
if NOT COUNT = 0 then
AVERAGE = TOTAL / COUNT
output "Average = " , AVERAGE
else
output "There are no non-zero values"
end if
The following pseudocode presents an algorithm that reads all the names from a collection, NAMES, and copies them into an array, LIST, but eliminates any duplicates. That means each name is checked against the names that are already in the array. The collection and the array are passed as parameters to the method.
COUNT = 0 // number of names currently in LIST
loop while NAMES.hasNext()
DATA = NAMES.getNext()
FOUND = false
loop POS from 0 to COUNT-1
if DATA = LIST[POS] then
FOUND = true
end if
end loop
if FOUND = false then
LIST[COUNT] = DATA
COUNT = COUNT + 1
end if
end loop
The following pseudocode presents an algorithm that will print all the factors of an integer. It prints two factors at a time, stopping at the square root. It also counts and displays the total number of factors.
// recall that
// 30 div 7 = 4
// 30 mod 7 = 2
NUM = 140 // code will print all factors of this number
F = 1
FACTORS = 0
loop until F*F > NUM //code will loop until F*F is greater than NUM
if NUM mod F = 0 then
D = NUM div F
output NUM , " = " , F , "*" , D
if F = 1 then
FACTORS = FACTORS + 0
else if F = D then
FACTORS = FACTORS + 1
else
FACTORS = FACTORS + 2
end if
end if
F = F + 1
end loop
output NUM , " has " , FACTORS , " factors "
The following pseudocode presents an algorithm that will read all the names from a collection, SURVEY, and then copy these names into an array, MYARRAY, in reverse order.
// MYSTACK is a stack, initially empty
COUNT = 0 // number of names
loop while SURVEY.hasNext()
MYSTACK.push( SURVEY.getNext() )
COUNT = COUNT + 1
end loop
// Fill the array, MYARRAY, with the names in the stack
loop POS from 0 to COUNT-1
MYARRAY[POS] = MYSTACK.pop()
end loop
IB Style pseudocode and output, http://ibcomp.fis.edu/pseudocode/pcode.html