UNIHIVE

Study Hub UNIPORT MTH 280

MTH 280 · Introduction to Computer programming

Problem solving and algorithms

The steps from a problem to a working program, what an algorithm is, its properties, and writing simple algorithms in steps.

Our notes

2 min read

From problem to program

  1. Understand the problem: what are the inputs, the outputs and the rules?
  2. Plan a solution: write an algorithm (steps), draw a flowchart or write pseudocode.
  3. Code it in a programming language.
  4. Test and debug with sample data whose answers you know.
  5. Document and maintain it (comments, user notes, later fixes).

What an algorithm is

An algorithm is a finite, ordered set of clear steps that solves a problem. A cooking recipe and the steps for registering courses on the portal are everyday algorithms.

Properties of a good algorithm

  • Finite: it must stop after a number of steps.
  • Definite: each step is clear and has only one meaning.
  • Input: zero or more inputs.
  • Output: at least one result.
  • Effective: each step can actually be carried out.
  • General: it works for every valid input of that kind, not just one case.

Writing an algorithm in steps

Example 1: average of three numbers.
Step 1: Start.
Step 2: Read A, B, C.
Step 3: SUM ← A + B + C.
Step 4: AVG ← SUM / 3.
Step 5: Print AVG.
Step 6: Stop.

Example 2: the larger of two numbers.
Step 1: Start. Step 2: Read X, Y. Step 3: If X > Y, print X; otherwise print Y. Step 4: Stop.

Example 3: sum of the first N natural numbers (with a loop).
Step 1: Start. Step 2: Read N. Step 3: SUM ← 0, I ← 1. Step 4: While I ≤ N: SUM ← SUM + I; I ← I + 1. Step 5: Print SUM. Step 6: Stop.
Trace for N = 4: SUM goes 1, 3, 6, 10.

The three building blocks

Every algorithm can be built from three structures:

StructureMeaningExample
Sequencesteps one after anotherread, compute, print
Selection (decision)choose a path by a conditionif score ≥ 40 then "pass"
Iteration (loop)repeat stepsadd up 100 numbers

Top-down design

Break a big problem into smaller parts (modules), and those into smaller ones, until each part is easy to code. A result-processing program might split into: read scores, compute grades, compute GPA, print the result sheet.

Exams usually ask: define an algorithm and list its properties; write an algorithm for a simple task (area of a circle, largest of three numbers, sum or average of N numbers, roots of a quadratic); name the three control structures.

Common mistake: writing steps that never end (a loop with no way out), or steps that depend on something never read or set.

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

Read

Watch

Practise

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 MTH 280 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.