2.2 Algorithms
Programming in C
2 - 6 MARKS
What is an Algorithm?
An algorithm is a finite sequence of well-defined, logical steps used to solve a given problem or to perform a specific task. Each step of an algorithm is precise, unambiguous, and executable, ensuring that the problem is solved correctly.
According to computer scientist Niklaus Wirth,
Program = Algorithm + Data,
which indicates that an algorithm forms the logical foundation of any computer program.
A well-designed algorithm must always terminate after a finite number of steps and produce at least one output for the given input. Algorithms are language-independent, meaning they can be implemented using any programming language.
Algorithms are essential because they help in systematic problem-solving, improve clarity of logic, reduce errors, and make program development easier and more efficient.
Key Features of an Algorithm:
-
Finite – Ends after a limited number of steps
-
Definite – Each step is clear and unambiguous
-
Input – Accepts zero or more inputs
-
Output – Produces at least one output
-
Effective – Steps are simple and feasible to execute
Example: Algorithm to Program Conversion
Problem
Add two integers and display the result.
Algorithm
START
PRINT “ENTER TWO NUMBERS”
INPUT A, B
R = A + B
PRINT “RESULT =”
PRINT R
STOP
C Program
16 MARKS
Algorithms
Introduction
To solve any problem systematically, a plan is required. This plan defines a procedure to solve the problem using logic and reasoning. In computer science, such a procedure-wise plan is known as an algorithm.
Computer scientist Niklaus Wirth stated:
Program = Algorithm + Data
This means that a program is formed by combining a logical procedure (algorithm) with the data on which it operates.
What is an Algorithm?
An algorithm is defined as:
“An effective procedure for solving a problem in a finite number of steps.”
Key characteristics of an algorithm:
-
It must be effective, i.e., produce a result
-
It must have a finite number of steps
-
It is guaranteed to terminate
-
It always provides an answer, even if the answer is “no solution exists”
A well-designed algorithm never runs indefinitely.
Different Ways of Representing Algorithms
Algorithms can be represented in four different ways:
-
Step-form
-
Pseudo-code
-
Flowchart
-
Nassi–Schneiderman technique
Step-form
-
Uses simple natural language
-
Each step performs a specific action
-
Steps are logically ordered
Pseudo-code
-
Written using restricted vocabulary
-
More precise than natural language
-
Easy to convert into programming code
Flowchart
-
Graphical representation
-
Uses symbols to show:
-
Sequence
-
Decision
-
Repetition
-
Nassi–Schneiderman
-
Graphical technique
-
Beyond the scope of this discussion
Key Features of an Algorithm
An algorithm consists of three basic constructs:
-
Sequence (Process)
-
Decision (Selection)
-
Repetition (Iteration / Looping)
Example: Algorithm for Making Tea (Step-form)
-
If the kettle does not contain water, fill the kettle
-
Switch on the kettle
-
If the teapot is not empty, empty it
-
Place tea leaves in the teapot
-
If water is not boiling, repeat step 5
-
Switch off the kettle
-
Pour water into the teapot
This example shows:
-
Decision (checking conditions)
-
Repetition (waiting until boiling)
-
Sequence (ordered execution)
Sequence Construct
Sequence means executing steps in the given order.
If the order is changed, the algorithm may fail.
Decision Constructs
Decision constructs work on true or false propositions.
if…then
if…then…else
A proposition is a condition that evaluates to true or false, never both.
Repetition Constructs
Repetition allows steps to be executed multiple times.
Repeat Loop
✔ Condition is checked after execution
⚠ May cause errors if condition is already satisfied
While Loop
✔ Condition is checked before execution
✔ Prevents unnecessary repetition
✔ Safer than repeat loop
if…then…goto Loop
✔ Repeats steps while condition is true
⚠ Makes logic harder to read
Termination
An algorithm must terminate after finite steps.
-
Algorithms that never terminate are called computational methods
-
Analyzing whether an algorithm will terminate is called termination analysis
-
Non-terminating algorithms are considered undecidable
Correctness of an Algorithm
Correctness refers to how well an algorithm satisfies its intended purpose.
Correctness requires:
-
Clearly defined logic
-
Traceable steps
-
Properly specified data structures
-
Correct interfaces between modules
A common measure of correctness is:
Defects per Kilo Lines of Code (KLOC)
Developing Algorithms Using Step-form
General Conventions
-
START and STOP define algorithm boundaries
-
INPUT / READ accept data
-
PRINT displays output
-
Steps execute sequentially
Arithmetic Operators Used
-
←Assignment -
+Addition -
-Subtraction -
*Multiplication -
/Division (quotient only)
Relational Operators in Algorithms
-
>Greater than -
<Less than -
<=Less than or equal to -
>=Greater than or equal to -
=Equality -
!=Not equal
⚠ ← is preferred for assignment to avoid confusion with equality =
Logical Operators
-
AND – true if both conditions are true
-
OR – true if any condition is true
-
NOT – negates the condition
Used to form composite propositions.
Strategy for Designing Algorithms
Investigation Phase
-
Identify required outputs
-
Identify input variables
-
Identify decisions and conditions
-
Identify transformation processes
-
Identify execution environment
Top-Down Development
-
Break the problem into manageable modules
-
Ensure modules cooperate logically
-
Best suited for large and complex systems
Stepwise Refinement
-
Decompose modules into smaller procedures
-
Define input-output details
-
Verify correctness
-
Group related processes and variables
-
Test each module independently
Tracing an Algorithm (Desk Check / Dry Run)
Tracing involves:
-
Executing steps one by one
-
Verifying intermediate results
-
Ensuring logic matches requirements
Used to validate correctness before coding.
Converting Algorithms into Programs
Steps involved:
-
Code the algorithm using a programming language
-
Desk-check the program
-
Modify algorithm or code if required
-
Reuse existing logic when possible
Algorithm to Program Conversion Example
Problem
Add two integers and display the result.
Algorithm
-
START
-
PRINT “ENTER TWO NUMBERS”
-
INPUT A, B
-
R = A + B
-
PRINT “RESULT =”
-
PRINT R
-
STOP
C Program
Conclusion
Algorithms form the foundation of programming. A well-designed algorithm ensures correctness, efficiency, and clarity. Understanding algorithm representation, logic constructs, and design strategies is essential before writing any program.
📖 Reference
The content for this topic is prepared by referring to the standard textbook
“Computer Fundamentals and Programming in C”
by Pradip Dey and Manas Ghosh,
Second Edition, Oxford University Press (2018).
🌱 About Aivette
Aivette-coi is created with the intention of helping college students learn smartly, revise quickly, and approach exams with confidence.
With love and care,
by Aivette 💙
Comments
Post a Comment