CSC 280 · Introduction to Computer Programming
Our notes
2 min readFrom problem to program
- Understand the problem: what are the inputs, the outputs and the rules?
- Plan a solution: write an algorithm (steps), draw a flowchart or write pseudocode.
- Code it in a programming language.
- Test and debug with sample data whose answers you know.
- 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:
| Structure | Meaning | Example |
|---|---|---|
| Sequence | steps one after another | read, compute, print |
| Selection (decision) | choose a path by a condition | if score ≥ 40 then "pass" |
| Iteration (loop) | repeat steps | add 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
-
Best pick Introduction to Computer Science, 2.1 Computational Thinking
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 2.1 Computational Thinking, CC BY-NC-SA 4.0 · licence
-
Introduction to Computer Science, 3.2 Algorithm Design and Discovery
(opens openstax.org in a new tab)
OpenStax, Introduction to Computer Science, 3.2 Algorithm Design and Discovery, CC BY-NC-SA 4.0 · licence
-
Programming Basics/Introduction/What is an algorithm?
(opens en.wikibooks.org in a new tab)
Wikibooks, Programming Basics/Introduction/What is an algorithm?, CC BY-SA 4.0 · licence
-
Algorithm
(opens en.wikipedia.org in a new tab)
Wikipedia, Algorithm, CC BY-SA 4.0 · licence
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.