Module 2 (9 hrs)
ALGORITHMS - The Notion of Algorithm: Reasons for Algorithm, Steps Involved in Algorithm
Development, Characteristics of Algorithm, Representation of Algorithms, Representative Algorithms for
Simple Problems, Measuring Efficiency of Algorithms, Advantages and Disadvantages of Algorithms
ALGORITHM AND PSEUDOCODE REPRESENTATION:- Meaning and Definition of Pseudocode, Reasons for
using pseudocode, The main constructs of pseudocode - Sequencing, selection (if-else structure, case
structure) and repetition (for, while, repeat-until loops), Sample problems*
FLOWCHARTS** :- Symbols used in creating a Flowchart - start and end, arithmetic calculations,
input/output operation, decision (selection), module name (call), for loop (Hexagon), flow-lines, on-page
connector, off-page connector.
Module 2.1 (2 hrs)
Algorithms
The word “algorithm” relates to the name of the mathematician Al-khowarizmi, which means a
procedure or a technique.
Eg: Algorithm for addition of two numbers
Step-1 Start
Step-2 Input first numbers say A
Step-3 Input second number say B
Step-4 SUM = A + B
Step-5 Display SUM
Step-6 Stop
The Notion of Algorithm
Definition: An algorithm is a clearly specified, finite set of instructions describing the solution to a
specific problem.
It takes an input and transforms it to an adequate output.
It must be independent from any programming language.
It has to be designed such that it can be reused and understood by others.
1
Reasons for Algorithm
1. To understand the basic idea of the problem.
2. To find an approach to solve the problem.
3. To improve the efficiency of existing techniques.
4. To understand the basic principles of designing the algorithms.
5. To compare the performance of the algorithm with respect to other techniques.
6. The Algorithm gives a clear description of requirements and goal of the problem to the designer.
7. To understand the flow of the problem.
8. To measure the behavior (or performance) of the methods in all cases (best cases, worst cases,
average cases)
Characteristics of Algorithm
1. Well-described steps: Each step has to be well-defined, there is no ambiguity.
2. Input and output: Algorithms take inputs, and produce outputs, which are the results or
solutions generated by using the set of rules after processing the inputs.
3. Finiteness: Algorithms must have a well-defined termination condition.
4. Determinism: Given the same inputs and achieved below the same conditions, they may
continually produce the identical outputs.
5. Efficiency: Algorithms attempt to be efficient in phrases of time and sources.
6. Correctness: Algorithms must be designed to produce correct results for all legitimate inputs
inside their domain.
7. Modularity and reusability: Algorithms may be modular, meaning they may be divided into
smaller subproblems and support reusability.
8. Understandability: Well-documented and readable code can enhance the understandability of
an algorithm.
The algorithm and flowchart include following three types of control structures:
2
1. Sequence: In the sequence structure, statements are placed one after the other and the execution
takes place starting from up to down.
2. Branching (Selection): In branch control, there is a condition and according to a condition, a decision
of either TRUE or FALSE is achieved. In the case of TRUE, one of the two branches is explored; but in the
case of FALSE condition, the other alternative is taken. Generally, the ‘IF-THEN’ is used to represent
branch control.
3. Loop (Repetition): The Loop or Repetition allows a statement(s) to be executed repeatedly based on
certain loop condition e.g. WHILE, FOR loops.
Advantages of algorithm
• It is a step-wise representation of a solution to a given problem, which makes it easy to understand.
• An algorithm uses a definite procedure
. • It is not dependent on any programming language, so it is easy to understand for anyone even
without programming knowledge.
• Every step in an algorithm has its own logical sequence so it is easy to debug.
Disdvantages of Algorithms:
1. Alogorithms are Time consuming.
2. Difficult to show Branching and Looping in Algorithms.
3. Big tasks are difficult to put in Algorithms.
Steps Involved in Algorithm Development
Step 1: Define the algorithms input: Many algorithms take in data to be processed.
E.g. to calculate the area of rectangle, input may be the rectangle height and rectangle width.
Step 2: Define the variables: Meaningful variable names for using it for more than one place.
Eg. Define two variables for rectangle height and rectangle width as HEIGHT and WIDTH
Step 3: Outline the algorithm's operations: An algorithm's operations can take the form of multiple steps
and even branch, depending on the value of the input variables.
3
Step 4: Output the results of the algorithm's operations: In case of area of rectangle output will be the
value stored in variable AREA.
Measuring Efficiency of Algorithms
An algorithm’s efficiency is referred to as the number of computational resources used by the
algorithm.
There are two main measures for the efficiency of an algorithm.
Time complexity: The total time needed to complete the execution of an program. measured as
the number of steps, provided each step consumes constant time.
Space complexity: It is the amount of memory required during the program execution until its
completion.
There are three cases for the time complexity of an algorithm:
Worst-case time complexity
Average case time complexity
Best case time complexity
Module 2.2 (4 hrs)
PSEUDOCODE - Meaning and Definition of Pseudocode, Reasons for using pseudocode, The main
constructs of pseudocode - Sequencing, selection (if-else structure, case structure) and repetition (for,
while, repeat-until loops), Sample problems*
PSEUDOCODE
Pseudocode literally means ‘false code’. It’s simply an implementation of an algorithm in the form of
annotations and informative text written in plain English.
It is a syntax-free description of an algorithm, it must provide a full description of the algorithm’s logic.
Reasons for using Pseudocode
1. Better readability:Using pseudocode to explain the mechanics of the code will make the
communication between the different backgrounds easier and more efficient.
4
2. Ease up code construction: When the programmer goes through the process of
developing and generating pseudocode, the process of converting that into real code
written in any programming language will become much easier and faster as well.
3. A good middle point between flowchart and code: Pseudocode presents a way to
make the transition between the flowchart to the code smoother.
4. Act as a start point for documentation: Documentation is an essential aspect of building
a good project. Pseudocode can represent a good starting point for what the
documentation should include.
5. Easier bug detection and fixing: Since pseudocode is written in a human-readable
format, it is easier to edit and discover bugs before actually writing a single line of code.
The main constructs of pseudocode
The core of pseudocode is the ability to represent 6 programming constructs (always written in
uppercase): SEQUENCE, CASE, WHILE, REPEAT-UNTIL, FOR, and IF-THEN-ELSE. These constructs — also
called keywords —are used to describe the control flow of the algorithm.
Sequencing
1. SEQUENCE: represents linear tasks sequentially performed one after the other.
Eg: READ, WRITE, SET
Repetition
2. WHILE: a loop with a condition at its beginning.
Eg: WHILE (x < 20)
WRITE x
ADD 1 to x
3. REPEAT-UNTIL: a loop with a condition at the bottom.
REPEAT
ADD 1 to x
UNTIL(x<20)
4. FOR: another way of looping.
FOR count FROM 6 TO 8 DO
5
Selection
5. IF-THEN-ELSE a conditional statement changing the flow of the algorithm.
IF (n1 < n2)
SET max to n2
ELSE
SET max to n1
6. CASE: the generalization form of IF-THEN-ELSE.
2 most needed commands:
1. Invoking classes or calling functions (using the CALL keyword).
2. Handling exceptions (using EXCEPTION, WHEN keywords).
6
Sample Problems:
1. Evaluate an expression: d=a+b×c
READ a, b, c
SET d TO a + (b * c)
WRITE d
2. Find simple interest
READ Principal, Rate, Time
SET SimpleInterest TO (Principal * Rate * Time) / 100
WRITE SimpleInterest
3. Determine the larger of two numbers
READ num1, num2
IF (num1 > num2) THEN
WRITE num1, "is larger"
ELSE IF (num2 > num1) THEN
WRITE num2, "is larger"
7
ELSE
WRITE "Both numbers are equal"
END IF
4. Determine the smallest of three numbers
READ num1, num2, num3
SET smallest TO num1
IF (num2 < smallest) THEN
SET smallest TO num2
END IF
IF (num3 < smallest) THEN
SET smallest TO num3
END IF
WRITE "The smallest number is", smallest
5. Determine the grade earned by a student based on KTU grade scale (using if-else)
READ marks
IF (marks >= 90) THEN
WRITE "Grade: A+"
ELSE IF (marks >= 80) THEN
WRITE "Grade: A"
ELSE IF (marks >= 70) THEN
WRITE "Grade: B+"
ELSE IF (marks >= 60) THEN
WRITE "Grade: B"
ELSE IF (marks >= 50) THEN
WRITE "Grade: C"
8
ELSE IF (marks >= 40) THEN
WRITE "Grade: D"
ELSE
WRITE "Grade: F"
END IF
6. Determine the grade earned by a student based on KTU grade scale (using case structure)
READ marks
CASE marks OF
90 TO 100:
WRITE "Grade: A+"
80 TO 89:
WRITE "Grade: A"
70 TO 79:
WRITE "Grade: B+"
60 TO 69:
WRITE "Grade: B"
50 TO 59:
WRITE "Grade: C"
40 TO 49:
WRITE "Grade: D"
0 TO 39:
WRITE "Grade: F"
OTHERWISE:
WRITE "Invalid marks"
9
END CASE
7. Print the numbers from 1 to 50 in descending order
FOR i FROM 50 DOWN TO 1 DO
WRITE i
END FOR
8. Find the sum of n numbers input by the user (using all three loop variants)
i)Using FOR loop
READ n
SET sum TO 0
FOR i FROM 1 TO n DO
READ number
SET sum TO sum + number
END FOR
WRITE "Sum is", sum
ii) Using WHILE loop
READ n
SET sum TO 0
SET i TO 1
WHILE (i <= n) DO
READ number
SET sum TO sum + number
SET i TO i + 1
END WHILE
WRITE "Sum is", sum
10
iii) Using REPEAT-UNTIL loop
SET sum TO 0
SET i TO 1
REPEAT
READ number
SET sum TO sum + number
SET i TO i + 1
UNTIL (i > n)
WRITE "Sum is", sum
9. Factorial of a number
READ n
SET factorial TO 1
FOR i FROM 1 TO n DO
SET factorial TO factorial * i
END FOR
WRITE "Factorial of", n, "is", factorial
10. Largest of n numbers
READ n
READ first_number
SET largest TO first_number
FOR i FROM 2 TO n DO
READ number
IF (number > largest) THEN
SET largest TO number
END IF
11
END FOR
WRITE "The largest number is", largest
Module 2.2 (3 hrs)
FLOWCHARTS** :- Symbols used in creating a Flowchart - start and end, arithmetic calculations,
input/output operation, decision (selection), module name (call), for loop (Hexagon), flow-lines, on-
page connector, off-page connector.
FLOWCHARTS
A flowchart is a visual representation of an algorithm.
It is a diagram made up of boxes, diamonds and other shapes, connected by arrows. Each shape
represents a step of the solution process and the arrow represents the order or link among the steps. It
is a diagram that shows each step or progression through a process.
The flowchart was first designed in 1945 by John Von Neumann.
12
Algorithm
Step 1: Start
Step 2: Input the values of A, B Compare A and B.
Step 3: If A > B then go to step 5
Step 4: Print “B is largest” go to Step 6
Step 5: Print “A is largest”
Step 6: Stop
13
Basic_Symbols
14