0% found this document useful (0 votes)
9 views

Python Module2 Complete

make it precise and easier to visualise

Uploaded by

adhinarayan206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Module2 Complete

make it precise and easier to visualise

Uploaded by

adhinarayan206
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

Module 2

Page 2
Module 2

► 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.

Page 3
Algorithms and pseudocodes

Page 4
Algorithms and pseudocodes
► Algorithm
► A step-by-step procedure for solving a problem.
► Uses pure English phrases or sentences.
► Pseudocode
► A high-level representation of an algorithm.
► Uses a mix of natural language and programming-like syntax.
► More structured than algorithms.

Page 5
Pseudocodes

► Confused between algorithms and pseudocodes? Let us take an example.


We will now write an algorithm and a pseudocode to evaluate an
expression, say d = a + b ∗ c.

Pseudocode
Algorithm

Page 6
Why pseudocodes?

Wondering why pseudocodes are important? Here are a few motivating reasons:
► Ease of understanding: Since the pseudocode is programming language independent,
novice developers can also understand it very easily.
► Focus on logic: A pseudocode allows you to focus on the algorithm’s logic without
bothering about the syntax of a specific programming language.
► More legible: Combining programming constructs with English phrases makes
pseudocode more legible and conveys the logic precisely.
► Consistent: As the constructs used in pseudocode are standardized, it is useful in sharing
ideas among developers from various domains.
► Easy translation to a program: Using programming constructs makes mapping the
pseudocode to a program straightforward.
► Identification of flaws: A pseudocode helps identify flaws in the solution logic before
implementation.

7
Difference between Algorithm and Pseudocode

Page 8
Constructs of a pseudocode

Page 9
Constructs of a pseudocode

► A good pseudocode should follow the structured programming approach.


► Structured coding aims to improve the readability of pseudocode by
ensuring that the execution sequence follows the order in which the code
is written.
► The main constructs are
► Sequencing
► Selection
► Repetition (loop)

Page 10
Constructs of a pseudocode - Sequence
► This is the most elementary construct where the instructions of the
algorithm are executed in the order listed.
► It is the logical equivalent of a straight line.
► Consider the code below.

► The statement S1 is executed first, which is then


followed by statement S2, so on and so forth, Sn until all
the instructions are executed.
► No instruction is skipped and every instruction is
executed only once.

Page 11
Constructs of a pseudocode - Sequence
► Example: Write a pseudocode to find the area of a square

1. Start
2. Read the side length of the square
3. area: area = side_length * side_length
4. Print the area
5. End

► All the statements in the given pseudocode will execute in sequential


order, without skipping any line.

Page 12
Constructs of a pseudocode - Decision or Selection

► A selection structure consists of a test condition together with one or


more blocks of statements.
► The result of the test determines which of these blocks is executed.
► There are mainly two types of selection structures
► if structure
► Simple if structure
► If…else structure
► if ..else if.. else structure
► Case Structure

Page 13
Constructs of a pseudocode - Decision or Selection

► Simple If
► The general form of this structure is

The pseudocode CheckPositive(x) checks if an input value x is positive.

Page 14
Constructs of a pseudocode - Decision or Selection

► if .. else structure
► The general form of this structure is
► This structure contains two blocks of statements.
► If the test condition is met, the first block (denoted by
true_instructions) is executed and the algorithm skips over the
second block (denoted by false_instructions).
► If the test condition is not met, the first block is skipped and only
the second block is executed.

The pseudocode PersonType(age) checks if a person is a major or not.

Page 15
Constructs of a pseudocode - Decision or Selection

► if ..else if .. else structure


► When a selection is to be made out of a set of more than two possibilities,
you need to use the if else if else structure, whose general form is given
below: ► Here, if condition1 is met, TRUE_INSTRUCTIONS1 will be executed.

► Else condition2 is checked. If it evaluates to True,


TRUE_INSTRUCTIONS2 will be selected.
► There is no limit to the number of else if statements, but in the
end, there has to be an else statement.
► The conditions are tested one by one starting from the top,
proceeding downwards.
► Once a condition is evaluated to be True, the corresponding block is
executed, and the rest of the structure is skipped.
► If none of the conditions are met, the final else part is
executed.
Page 16
Constructs of a pseudocode - Decision or Selection

► if ..else if .. else structure


► Example

The pseudocode CompareVars(x, y) compares two variables x


and y and prints the relation between them.

Page 17
Constructs of a pseudocode - Case Structure

► The case structure is a refined alternative to if else if else structure.


► The pseudocode representation of the case structure is given below.

► First, the value of an expression (which could also be a single


variable) is compared to a predefined value1.
► If a match is found, the first block of code, referred to as block1, is
executed.
► Typically, each block ends with a break statement, which exits the
case structure.

Page 18
Constructs of a pseudocode - Case Structure

► The case structure is a refined alternative to if else if else structure.


► The pseudocode representation of the case structure is given below.

► If there is no match with value1, the expression or variable is then


compared with value2.
► If this comparison results in a match, block2 is executed, and the
case structure is exited with the corresponding break statement.

Page 19
Constructs of a pseudocode - Case Structure

► The case structure is a refined alternative to if else if else structure.


► The pseudocode representation of the case structure is given below.

► This comparison process continues for each case until either a


match is found, or all cases are exhausted.
► If no matches are found for any of the cases, the default_block will
be executed.
► This block handles cases where the expression doesn't match any
predefined values.

Page 20
Constructs of a pseudocode - Case Structure

► The case structure is a refined alternative to if else if else structure.


► The pseudocode representation of the case structure is given below.

► If a break statement is omitted from a block, the program will


continue executing the subsequent blocks, regardless of whether
the subsequent cases match, until a break is encountered or the
end of the case structure is reached.

Page 21
Constructs of a pseudocode - Case Structure

► Example

► The pseudocode PRINTDIRECTION(dir) prints the


direction name based on the value of a character called
dir.

Page 22
Constructs of a pseudocode - Repetition or loop

► When a certain block of instructions is to be repeatedly executed, we use


the repetition or loop construct.
► Each execution of the block is called an iteration or a pass.
► Definite iteration: If the number of iterations (how many times the block
is to be executed) is known in advance.
► Indefinite or conditional iteration: If the number of iterations is not
known in advance
► The block that is repeatedly executed is called the loop body.

Page 23
Constructs of a pseudocode - Repetition or loop
► There are three types of loop constructs as discussed below
► while loop
► repeat-until loop
► for loop

Page 24
Constructs of a pseudocode – while loop
► A while loop is generally used to implement indefinite iteration. The
general form of the while loop is as follows:

► Here, the loop body (TRUE_INSTRUCTIONS) is executed


repeatedly as long as condition evaluates to True.
► When the condition is evaluated as False, the loop body
is bypassed

Page 25
Constructs of a pseudocode – repeat-until loop

► The second type of loop structure is the repeat-until structure.


► This type of loop is also used for indefinite iteration.
► Here the set of instructions constituting the loop body is repeated as long as condition
evaluates to False.
► When the condition evaluates to True, the loop is exited

Page 26
Constructs of a pseudocode – repeat-until loop

► There are two major differences between while and repeat-until loop
constructs:
► In the while loop, the pseudocode continues to execute as long as the resultant of
the condition is True; in the repeat-until loop, the looping process stops when the
resultant of the condition becomes True.
► In the while loop, the condition is tested at the beginning; in the repeat until loop,
the condition is tested at the end.
► For this reason, the while loop is known as an entry controlled loop and the
repeat-until loop is known as an exit controlled loop.

Page 27
Constructs of a pseudocode – for loop

► The for loop implements definite iteration.


► for loop constructs use a variable (call it the loop variable) as a counter that starts
counting from a specific value called begin and updates the loop variable after each
iteration.
► The loop body repeats execution until the loop variable value reaches end.
► The condition var <= end is tested first.

► If the condition is True, the loop body is executed.

► After the first iteration, the loop variable is incremented


(increased by 1).
► The condition var <= end is tested again with the
updated value of var.
► If the condition remains True, the loop body is executed
again.The loop continues: updating the loop variable
after each iteration and testing the condition.When the
loop variable becomes greater than end, the condition
Page 28
evaluates to False.At this point, the loop execution STOPS
Constructs of a pseudocode – for loop

► In the second for loop variant, whose pseudocode


syntax is given
► The loop variable is decremented (decreased by 1) after
every iteration
► The condition being tested is var >= end.
► Here, begin should be greater than or equal to end,
and the loop exits when this condition is violated.

Page 29
Constructs of a pseudocode – for loop

► It is also possible to update the loop variable by an


amount other than 1 after every iteration.
► The value by which the loop variable is increased or
decreased is known as step.
► In the pseudocode shown below, the step value is
specified using the keyword by .

Page 30
Constructs of a pseudocode – for loop

lists some examples of for loops. In these examples, var is the loop variable.

Page 31
► Problem: To find simple interest

Page 32
Solved problems - Algorithms

► Problem: To determine the larger of two numbers

LargerTwo
1 Start
2 Read(number1, number2)
3 if (number1 > number2)
4 large = number1
5 else
6 large = number2
7 endif
8 Print(large)
9 Stop.

Page 33
► Problem: To determine the smallest of three numbers.
SmallestThree
1 Start
2 Read(number1, number2, number3)
3 if (number1 < number2)
4 small = number1
5 else
6 small = number2
7 endif
8 if (number3 < small )
9 small = number3
10 endif
11 Print(small )
12 Stop.

Page 34
Solved problems - Algorithms

► Problem: To determine the entry-ticket fare in a zoo based on age.

TicketFare
1 Start
2 Read(age)
3 if (age < 10)
4 fare = 7
5 else if (age < 60)
6 fare = 10
7 else
8 fare = 5
9 endif
10 Print(fare)
11 Stop

Page 35
► Problem: To print the colour based on a code value as follows:.
PrintColour
1 Start
2 Read(code)
3 caseof (code)
4 case ‘R’:
5 Print(“Red”)
6 break
7 case ‘G’:
8 Print(“Green”)
9 break
10 case ‘B’:
11 Print(“Blue”)
12 break
13 default :
14 Print(“Wrong code”)
15 endcase
16 Stop
Page 36
Solved problems - Algorithms

► Problem: To print the numbers from 1 to 50 in descending order.

PrintDown
1 Start
2 for count = 50 downto 1
3 Print(count)
4 endfor
5 Stop

Page 37
► Problem: To find the factorial of a number.

Factorial
1 Start
2 Read(number)
3 fact = 1
4 for var = number downto 1
5 fact = fact ∗ var
6 endfor
7 Print(fact)
8 Stop

Page 38
Solved problems - Algorithms

► Problem: To determine the largest of n numbers.

Page 39
► Problem: To determine the average age of students in a class. The user will
stop giving the input by giving the age as 0.

Page 40
Solved problems - Algorithms

► Problem: To determine the average age of students in a class. The user will
stop giving the input by giving the age as 0. use repeat-until loop

Page 41
► Problem: To find the average height of boys and average height of girls in a
class of n students

Page 42
Module 2-
part 2

Page 2
Flowchart

► Flowcharts use standardized symbols to visually


represent various aspects of an algorithm or a
process.

► Terminator : A terminator symbol is used to START


represent the beginning and end of an algorithm
STOP

► Connector Lines: Connector lines are used to


connect symbols in the flowchart.
► The direction of the arrow indicates the next step.
Flowchart

► Process: A process symbol : represents an activity. Process


It represents a particular step of an algorithm.
► The symbol contains text which describes the step.
Sum = Number1+ Number2 Example

► Data: A data symbol represents data used in the


algorithm. It is also used to represent the input and
output
► The symbol contains text which describes the step.
Examples
► Multiple inputs can be read or multiple data can be
initialised in the same symbol

Page 6
Flowchart

► Decision: A symbol used to branch into different


steps based on condition
► Based on whether the condition succeeds or fails,
connector lines connect to different points in the
flowchart.
<10

► On page and Off Page References: Symbols used


when the entire flowchart cannot fit on the same P2
page fully.
On page
Off page
Reference
Reference
Flowchart

► Rectangle with vertical side-lines denotes a


module. A module is a collection of statements
written to achieve a task. It is known by the name
function in the programming domain.

► Hexagon denotes a for loop. The symbol shown


here is the representation of the loop: for count =
A to B by S.

Page 8
Flowchart
Flowchart

Page 10
Flowchart

► Problem 2.1 To find simple interest.


Flowchart

► Problem 2.2 To determine the larger of two numbers.

Page 12
Flowchart

► To determine the smallest of three numbers.


Flowchart

► To determine the entry-ticket fare in a zoo based on age as follows:

Page 14
Flowchart

► To print the colour based on a code value as follows:


Flowchart

► To find the factorial of a number


Solution: The factorial of a number n is defined as n! = n×n−1×·········· ×2×1.

Page 16
Flowchart

► To find the factorial of a number


Solution: The factorial of a number n is defined as n! = n×n−1×·········· ×2×1.
Flowchart

► To find the average height of boys and average height of girls in a class
of n students.

Page 18
Page 19

You might also like