Module 2 ATP
Module 2 ATP
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.
Algorithms
The word “algorithm” relates to the name of the mathematician Al-khowarizmi, which means a
procedure or a technique.
Step-1 Start
Step-4 SUM = A + B
Step-6 Stop
Definition: An algorithm is a clearly specified, finite set of instructions describing the solution to a
specific problem.
1
Reasons for Algorithm
6. The Algorithm gives a clear description of requirements and goal of the problem to the designer.
8. To measure the behavior (or performance) of the methods in all cases (best cases, worst cases,
average cases)
Characteristics of Algorithm
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.
4. Determinism: Given the same inputs and achieved below the same conditions, they may
continually produce the identical outputs.
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.
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 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:
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.
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.
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.
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.
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.
Repetition
WRITE x
ADD 1 to x
REPEAT
ADD 1 to x
UNTIL(x<20)
5
Selection
SET max to n2
ELSE
SET max to n1
6
Sample Problems:
READ a, b, c
SET d TO a + (b * c)
WRITE d
WRITE SimpleInterest
7
ELSE
END IF
END IF
END IF
5. Determine the grade earned by a student based on KTU grade scale (using if-else)
READ marks
8
ELSE IF (marks >= 40) THEN
ELSE
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:
80 TO 89:
70 TO 79:
60 TO 69:
50 TO 59:
40 TO 49:
0 TO 39:
OTHERWISE:
9
END CASE
WRITE i
END FOR
8. Find the sum of n numbers input by the user (using all three loop variants)
READ n
SET sum TO 0
FOR i FROM 1 TO n DO
READ number
END FOR
READ n
SET sum TO 0
SET i TO 1
WHILE (i <= n) DO
READ number
SET i TO i + 1
END WHILE
10
iii) Using REPEAT-UNTIL loop
SET sum TO 0
SET i TO 1
REPEAT
READ number
SET i TO i + 1
UNTIL (i > n)
9. Factorial of a number
READ n
SET factorial TO 1
FOR i FROM 1 TO n DO
END FOR
READ n
READ first_number
FOR i FROM 2 TO n DO
READ number
END IF
11
END FOR
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
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.
12
Algorithm
Step 1: Start
Step 6: Stop
13
Basic_Symbols
14