An algorithm is the plan behind every program. If the algorithm is wrong, no amount of careful coding will fix the program. This section defines what an algorithm is, lists the five properties it must have, and shows how tracing an algorithm by hand reveals errors, including one in the study material itself.
What an algorithm is
An algorithm is a method. A program is that method written in a particular programming language. The same algorithm can be written as numbered English steps, as pseudocode, as a flowchart, or as C or Python code.
Reading assignment: the arrow ←
Algorithms use the arrow ← for assignment: storing a value in a variable.
x ← 5 means “store 5 in x”.
i ← i + 1 means “calculate i + 1 using the current value of i, then store the result back in i”.
The right-hand side is always calculated first. The arrow is used instead of = because in mathematics i = i + 1 is a false statement, while in programming it is a normal instruction. In C, the same instruction is written i = i + 1;.
The five properties of an algorithm
Property
Meaning
Example of a violation
Input
Zero or more values are supplied
(rarely violated)
Output
At least one result is produced
Steps that calculate a value but never show it
Definiteness
Each step is clear and has only one meaning
“Multiply the price by a suitable tax rate”
Finiteness
It always stops after a finite number of steps
“k ← 10; while k > 0, k ← k + 1”
Effectiveness
Each step is basic enough to be done with pen and paper
“Find the largest prime number” (none exists)
The study material also lists “features” of an algorithm: proper understanding of the problem, use of functions for modularity, meaningful variable names and documentation. These are good programming practices. If an exam asks for features, give this list; if it asks for criteria or characteristics, give the five properties above.
Tracing an algorithm
Tracing means executing an algorithm by hand, step by step, writing down the value of every variable after each step. It is the most reliable way to find errors in an algorithm before it becomes a program.
The factorial of a whole number n is
n!=1×2×3×⋯×n
so 4!=24. The study material’s factorial algorithm (Example 1.3) repeats its loop “until i = n”. Tracing it with n = 4 shows the problem.
Key takeaways
An algorithm is a method; a program is that method in a specific language.
x ← value means store the value in x; the right-hand side is calculated first.
Every algorithm must satisfy Input, Output, Definiteness, Finiteness and Effectiveness.
Tracing with a small example catches off-by-one errors and wrong loop conditions.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 7 · short answer
Trace the corrected factorial algorithm (i ← 1, fact ← 1; while i ≤ n, fact ← fact × i and i ← i + 1) for n = 3. Show the values after each round and the output.
Show worked answer
Start: i = 1, fact = 1. Round 1: 1 ≤ 3 is true, fact = 1 × 1 = 1, i = 2. Round 2: 2 ≤ 3 is true, fact = 1 × 2 = 2, i = 3. Round 3: 3 ≤ 3 is true, fact = 2 × 3 = 6, i = 4. Test: 4 ≤ 3 is false, so the loop stops. Output: 6, which equals 3! = 1 × 2 × 3.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/programming-fundamentals/algorithms-definition-and-properties/ to read this page.