UNIHIVE

Study Hub UNIPORT ENG 213.1

ENG 213.1 · Computer Programming for Engineers

Algorithms, flowcharts and pseudocode

Planning a program before coding: the properties of an algorithm, flowchart symbols, pseudocode and the three control structures.

Our notes

1 min read

Algorithm

An algorithm is a finite list of clear steps that solves a problem. A good algorithm is:

  • Finite: it stops.
  • Definite: each step has one meaning.
  • Has input and output.
  • Effective: each step can be carried out.
  • General: works for a whole class of inputs, not one case.

Steps in solving a problem by computer

  1. Define the problem: inputs, outputs, formulas, units.
  2. Design the algorithm (flowchart or pseudocode).
  3. Code it in a language.
  4. Compile and test with known answers.
  5. Debug and document.

Flowchart symbols

SymbolUse
Ovalstart / stop
Parallelograminput / output
Rectangleprocess (calculation)
Diamonddecision (yes / no)
Arrowflow direction
Circleconnector

The three control structures

  1. Sequence: one step after another.
  2. Selection: IF condition THEN ... ELSE ...
  3. Repetition: repeat a block a fixed number of times or while a condition holds.

Worked example: roots of a quadratic

Pseudocode:

  1. Input a, b, c.
  2. d = b² − 4ac.
  3. If d > 0: x1 = (−b + √d)/(2a), x2 = (−b − √d)/(2a); print both.
  4. Else if d = 0: x = −b/(2a); print "equal roots", x.
  5. Else: print "complex roots".
  6. Stop.

Test: a = 1, b = −3, c = 2: d = 9 − 8 = 1, x1 = (3 + 1)/2 = 2, x2 = (3 − 1)/2 = 1. Check: 2² − 3(2) + 2 = 0 ✓.

Worked example: largest of N readings

  1. Input N and the first reading; set MAX = first reading.
  2. Repeat N − 1 times: input the next reading R; if R > MAX then MAX = R.
  3. Print MAX.

Trace with 4.2, 6.1, 5.0, 6.8, 3.9: MAX goes 4.2 → 6.1 → 6.1 → 6.8 → 6.8. Output 6.8.

Dry run (desk check)

Before coding, trace the algorithm by hand with a table of variables. It catches logic errors cheaply.

Exams usually ask

  • Define an algorithm and give its properties.
  • Draw a flowchart or write pseudocode for a stated engineering calculation.

Common mistake

Forgetting the case a = 0 in the quadratic (division by zero). Good algorithms check for impossible inputs.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All ENG 213.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.