hanegreefs.com/Business Information SystemsBIS
Lecture 3 · unit 2 of 16

Algorithms

The eight steps of programming, what an algorithm is, flow charts, Nassi-Schneiderman diagrams and pseudo code, and the three building blocks: sequence, selection and iteration.

Based on: lecture 3 (algorithms) and the algorithm exercise sessions · About 60 minutes

Eight steps from problem to programImportant

Solving a problem with a computer follows a fixed route. Learn the steps in order: exam questions ask for them, and the algorithm lecture keeps referring to them by number.

  1. Problem definition: describe exactly the starting situation, the result you need and the link between both.
  2. Solution strategy: choose a method and the data you need; split the problem into smaller sub-problems (modules) and agree what each one does.
  3. Representation of the algorithm: write the solution as a flow chart, a Nassi-Schneiderman diagram or pseudo code.
  4. Coding: translate the finished algorithm into a programming language (Python, Java, C++ and many more).
  5. Compiling: detect syntax errors and translate into machine language (ones and zeros), with a compiler or an interpreter.
  6. Testing: the results must also be logically correct; compare with results obtained another way and test every different case.
  7. Documenting: everything that helps someone else understand, change, improve or extend the program.
  8. Maintenance: after a while the program has to be updated, and every change goes into the documentation.
Common trapCompiling only catches syntax errors. A program can compile perfectly and still give wrong answers: that is what testing is for.

What an algorithm isImportant

The word comes from the name of al-Khwarizmi, a Persian mathematician of the ninth century whose book on solving equations also gave us the word algebra. In the course an algorithm is a method to solve a problem with four characteristics:

  • a sequence of actions, commands or rules,
  • that starts from a given initial situation,
  • and reaches a stated goal,
  • and stops after a finite time.

On top of that, every step has to be unambiguous: everyone (and every computer) must understand them the same way. A recipe for pancakes or the steps to change a flat tyre are everyday algorithms; computers use them for routing network traffic, cryptography, planning, image and text processing.

Common trap"It has arrows connecting the figures" is not a characteristic of an algorithm: that is a property of one way to draw it (the flow chart).

Three ways to represent an algorithmImportant

RepresentationHow it looksStrong point
Flow chartbasic figures with the instructions inside, connected by arrowseasy to read, shows the flow visually
Nassi-Schneiderman diagram (block diagram, structogram)nested rectangles without arrows; a selection is a split block, a loop a block that wraps its bodyforces a clean structure: no jumping around
Pseudo codestructured text with keywords such as Read, Write, If, While, EndDoclosest to real code; easy to translate into Python

Flow chart symbols used in the course: an oval for begin and end, a rectangle for a process step (an assignment), a parallelogram for input and output (Read, Write), a diamond for a choice with yes and no branches. The course adds one rule for clarity: every rectangle gets exactly one arrow in and one arrow out.

In pseudo code the assignment is written with an arrow: km ← mile × 1.609 means "km gets the value of mile times 1.609". The left-hand side receives the value of the right-hand side. A user-friendly algorithm first writes a message (Write 'Give distance in miles') so the user knows what to type.

Worked example

Miles to kilometres in pseudo code

Problem definition: convert a distance in miles into kilometres; 1 mile = 1.609 km.

1

Solution strategy

ask for the distance, multiply by 1.609, write the result
2

Pseudo code

Begin / Write 'Distance in miles?' / Read miles / km ← miles × 1.609 / Write miles, ' mi is ', km, ' km' / End
3

Test with 10 miles

10 × 1.609 = 16.09 km
Answer

This uses only three primitives: input (Read), assignment and output (Write).

Sequence, selection and iterationImportant

Every algorithm is built from three building blocks.

BlockVariantsIn words
Sequenceone step after the otherread, compute, write
Selection (conditional)If Then; If Then Else; Case (several options, built from nested Ifs)do something only when a condition is True
Iteration (loop, repetition)While; Repeat (Until); Forrepeat a block of actions
LoopCondition testedRuns at least once?Stops whenBest for
Whilebefore the bodyno: can run 0 timesthe condition becomes Falseunknown number of repetitions
Repeat ... Untilafter the bodyyes: at least oncethe condition becomes Trueactions that must happen once before you can test
For X from E1 to E2 [Step E3]counter compared with E2no (if E1 is past E2)the counter passes E2a known number of repetitions; step is 1 by default

Every Repeat loop can be rewritten as a While loop (run the body once before the While, or use a flag), and every For loop can be built with a While and a counter. The reverse is not always possible without extra tests, because a While may run zero times.

Worked example

Sum of a series closed by zero

Read numbers until the user types 0, then write the sum. Series 7, 4, 3, 0.

1

Initialisation

Sum ← 0 (a variable that is read does not need initialisation, one that accumulates does)
2

Read the first number

Number = 7
3

While Number ≠ 0

Sum ← Sum + Number, then read the next number: 7, 11, 14
4

Number = 0

the condition is False, the loop ends
Answer

Write 'The sum is ', Sum gives 14. The EndDo marks exactly which actions belong to the loop; forget to read inside the loop and you get an endless loop.

Common trapTwo classic endless loops: the reading step is outside the loop (the condition never changes), or the counter is never increased. In the exam, check that every loop changes the variable in its condition.

The cash dispenser exam questionImportant

A recurring exam question asks for a flow chart, a Nassi-Schneiderman diagram or pseudo code of an ATM. Split it into two parts, exactly as the solution strategy step recommends.

  1. Part 1, checking card and code: after the card is inserted, ask for the PIN in a user-friendly way. Allow three attempts with a counter. After three wrong codes, the card is swallowed and the transaction stops.
  2. Part 2, the menu: only when the code is correct (so an If around part 2). Show four options: withdraw money, change PIN, consult balance, return card. After options 1 to 3 the menu comes back (a loop); option 4 stops. The choice itself is a Case.

Counting attempts is the counter technique: start at 0, add 1 in every pass, and test the counter in the loop condition. The final exercise session stresses that this technique comes back in the exam. You can program the PIN part yourself in the Python control flow unit.

Key takeaways

  • Eight steps: problem definition, solution strategy, representation, coding, compiling, testing, documenting, maintenance.
  • An algorithm: a sequence of unambiguous actions from a starting situation to a goal in finite time.
  • Represent it as a flow chart, a Nassi-Schneiderman diagram or pseudo code.
  • Sequence, selection (If, If Else, Case) and iteration (While, Repeat, For).
  • While tests first and can run 0 times; Repeat tests after and runs at least once.

Loading cards
Question
Click, tap or press space to flip
Answer

Multiple choice (8)

Question 1 · Lecture 3

Which is NOT a characteristic of an algorithm?

Why: Arrows belong to the flow chart, one way of drawing an algorithm.
Question 2 · Lecture 3

Which statement is correct?

Why: Repeat can always be rewritten as While; the other statements describe the loops wrongly.
Question 3 · Lecture 3

In which step are syntax errors detected?

Why: The compiler or interpreter checks the rules of the language.
Question 4 · Lecture 3

Which flow chart symbol stands for Read and Write?

Why: Input and output use the parallelogram.
Question 5 · Lecture 3

A loop must allow at most three PIN attempts. Which technique fits best?

Why: The counter technique combined with a While (or Repeat) that also checks the PIN.
Question 6 · Lecture 3

Sum ← 0; read Number; While Number ≠ 0: Sum ← Sum + Number. What is wrong?

Why: Without reading inside the loop the condition never changes: an endless loop.
Question 7 · Lecture 3

For X from 2 to 10 Step 3 runs the body for:

Why: The pseudo-code For includes the end value if it is reached; 11 is past 10.
Question 8 · Lecture 3

What is a Nassi-Schneiderman diagram also called?

Why: Its nested blocks give a clear structure, hence structogram.

Exam-style open questions (2)

Open question · Lecture 3

ATM in pseudo code

Write pseudo code for the cash dispenser: three attempts for the PIN, then a menu with withdraw, change PIN, balance and return card, where the menu returns after options 1 to 3.

Show a model answer outline
  • Counter Attempts ← 0; Repeat: Write prompt, Read code, Attempts ← Attempts + 1 Until code correct or Attempts = 3.
  • If code not correct: swallow card, stop.
  • Else Repeat: show menu, Read choice, Case choice 1, 2, 3: module Until choice = 4; return card.
  • Name the building blocks used: sequence, selection (If, Case), iteration (Repeat).
Open question · Lecture 3

While or Repeat?

Rewrite this While loop as a Repeat loop and explain when the two give a different result: z ← 0; x ← 1; While x < 6: z ← z + x; x ← x + 1.

Show a model answer outline
  • Repeat: z ← z + x; x ← x + 1 Until x ≥ 6.
  • Both give z = 15 here, because the start value 1 satisfies the condition.
  • If x started at 6 or more, While would run 0 times (z = 0) but Repeat once (z = x).
  • A For x from 1 to 5 is also suitable because the number of repetitions is known.

LabLoop tracer: While versus Repeat

Type a series of numbers that ends with 0, choose the loop type and follow the trace table. Try a series that starts with 0.

LabFor loop counter

The pseudo-code For includes the end value when the counter reaches it exactly. Compare with Python range in the Python control flow unit.

LabFlow chart symbols and loops

Two short sorting games about the representation of algorithms.