Programming Algorithms
Senior 4 · ICT
Programming Algorithms
Lesson 2 of 5
25 min
At break time the school canteen sells mandazi, juice and bread. For each customer the cashier needs two answers: how much to charge, and how much change to give. The calculation is the same every time; only the numbers change. An algorithm for the till therefore needs places to store numbers that change from customer to customer, operations to calculate with them, a way to take numbers in, and a way to give the answers out. These four ideas are the building blocks of every algorithm you will write.
Definition
Variable: a named storage location whose value can change while the algorithm runs. Identifier: the name given to a variable or constant, such as total.
Constant: a named value that does not change while the algorithm runs, such as MINUTES_PER_HOUR ← 60.
Data type: the kind of value a variable holds, which decides what can be done with it.
Assignment: storing a value in a variable, written variable ← expression and read "becomes".
Input: data the algorithm receives from outside (a keyboard, a sensor, a file). Output: results it gives out (a screen, a printer, a file).
| Type | Holds | Examples |
|---|---|---|
| INTEGER | Whole numbers, positive or negative | 25, −3, 1500 |
| REAL (float) | Numbers with a decimal part | 3.75, −0.5, 98.6 |
| STRING | Text, in quotation marks | "Musanze", "S4B" |
| CHARACTER | One symbol | 'A', '7', '?' |
| BOOLEAN | TRUE or FALSE | passed ← TRUE |
The type matters: "12" + "3" joins two strings into "123", while 12 + 3 adds two integers to give 15.
Naming rules. An identifier starts with a letter, contains only letters, digits and underscores, has no spaces, is not a keyword such as IF or WHILE, and says what it holds: total_cost or totalCost, not x or tc.
An assignment works right to left: first the expression on the right is calculated using the current values, then the result is stored in the variable on the left, replacing what was there.
So count ← count + 1 is not an impossible equation: it means "take the value of count, add 1, and store the result back in count". If count was 6, it becomes 7.
| Kind | Operator | Meaning | Example | Result |
|---|---|---|---|---|
| Arithmetic | + − | add, subtract | 1500 − 1200 | 300 |
| * | multiply | 300 × 4 | 1200 | |
| / | divide | 17 / 5 | 3.4 | |
| DIV | whole-number division | 17 DIV 5 | 3 | |
| MOD | remainder | 17 MOD 5 | 2 | |
| ^ | power | 2 ^ 3 | 8 | |
| Relational | = ≠ < > ≤ ≥ | compare two values | 17 ≥ 20 | FALSE |
| Logical | AND OR NOT | combine conditions | (5 > 3) AND (2 > 4) | FALSE |
DIV and MOD go together: , so and . The logical operators are the AND, OR and NOT gates of Unit 2.
Order of operations: brackets; then ^; then *, /, DIV, MOD; then + and −; then the relational operators; then NOT, AND, OR.
Worked example 1: evaluate expressions
(a)
(b)
(c) 200 minutes in hours and minutes: and , so 3 h 20 min, since .
(d) Is 45 even? , not 0, so no. Its last digit is .
INPUT variable waits for a value and stores it in the variable. OUTPUT (or PRINT, DISPLAY) shows text and values. A good algorithm outputs a prompt before each input so the user knows what to type, and labels each result.
The canteen till in pseudocode: three inputs, two calculations, two outputs, in sequence from top to bottom.
Worked example 2: trace the till
Test data: price 300, quantity 4, paid 1500.
| Line | price | quantity | paid | total | change | Output |
|---|---|---|---|---|---|---|
| INPUT price | 300 | |||||
| INPUT quantity | 300 | 4 | ||||
| INPUT paid | 300 | 4 | 1500 | |||
| total ← … | 300 | 4 | 1500 | 1200 | ||
| change ← … | 300 | 4 | 1500 | 1200 | 300 | |
| OUTPUTs | Total: 1200 Frw; Change: 300 Frw |
A trace table has one column per variable and one row per step. It shows exactly when each value changes.
Python writes ← as =, INPUT as input() (with int() to turn the typed text into a whole number) and OUTPUT as print(). Run it with the test data 300, 4 and 1500 and compare with the trace table. Then try paying too little (for example 1000) and see what the change becomes; lesson 4 shows how to handle that case.
Worked example 3: swap two values
Swap the values of a and b. The obvious attempt fails:
| Step | a | b |
|---|---|---|
| start | 5 | 9 |
| a ← b | 9 | 9 |
| b ← a | 9 | 9 |
The 5 was overwritten before it was saved. Use a temporary variable:
| Step | a | b | temp |
|---|---|---|---|
| start | 5 | 9 | |
| temp ← a | 5 | 9 | 5 |
| a ← b | 9 | 9 | 5 |
| b ← temp | 9 | 5 | 5 |
Stuff Made Easy: Variables, Constants, Assignments and Data Types. Watch how a variable is declared and assigned, how a constant differs from a variable, and how each data type is used.
Warning
← is not =. In pseudocode, x ← 5 stores 5 in x, while x = 5 asks whether x equals 5 and gives TRUE or FALSE. (Python uses = for assignment and == for comparison.)
Give a variable a value before you use it. total ← total + mark is meaningless unless total was first set, usually to 0.
/ and DIV differ. 7 / 2 gives 3.5, but 7 DIV 2 gives 3.
Tip
Trace tables catch overwriting. Whenever a variable is assigned, cross out its old value in your trace table. If you see a value disappear before it was used, as in the failed swap, you have found the bug.
Practice
total marks, mark2, 2ndMark, IF, average_mark.x ← 4, y ← x * 3, x ← y - x, y ← x + y, what are x and y? Use a trace table.p and q, and explain why a third variable is needed.average ← a + b + c / 3. Evaluate it for a = 60, b = 70, c = 80, explain the mistake and correct it.total marks (contains a space), 2ndMark (starts with a digit), IF (a keyword). The others are valid.average ← (a + b + c) / 3, which gives 70.Key points
Sources and credits
input(), int() and print(): Built-in Functions, Python documentation, Python Software Foundation (linked for reference).This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.
BEGIN
OUTPUT "Price of one item (Frw):"
INPUT price
OUTPUT "How many items?"
INPUT quantity
OUTPUT "Amount paid (Frw):"
INPUT paid
total ← price * quantity
change ← paid - total
OUTPUT "Total: ", total, " Frw"
OUTPUT "Change: ", change, " Frw"
END# The canteen till in Python. Press Run and type a number after each question.
price = int(input("Price of one item (Frw): "))
quantity = int(input("How many items? "))
paid = int(input("Amount paid (Frw): "))
total = price * quantity
change = paid - total
print("Total:", total, "Frw")
print("Change:", change, "Frw")Quick check