Algorithmic Thinking with Python - Module 2
Algorithmic Thinking with Python - Module 2
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.
ALGORITHM
An algorithm describes a systematic way of solving a problem. It is a step-by- step procedure that produces
an output when given the necessary inputs. An algorithm uses pure English phrases or sentences to describe
the solution to a problem.
PSEUDOCODE
A Pseudocode is a high-level representation of an algorithm that uses a mixture of natural language and
programming language-like syntax. It is more structured than an algorithm in that it uses mathematical
expressions with English phrases to capture the essence of a solution concisely.
Evaluate-Algo Evaluate-Pseudo
1. Start Start
2. Read the values of a, b and c. Read(a, b, c)
3. Find the product of b and c. d=a+b∗c
4. Store the product in a temporary variable temp. Print(d)
5. Find the sum of a and temp. Stop
6. Store the sum in d.
7. Print the value of d.
8. Stop.
Evaluate-Algo Evaluate-Pseudo
1. Start START
2. Read the values of a, b, and c. READ(a, b, c)
3. Find the sum of b and c and store it in a temporary temp = b + c
variable temp. d = a / temp
4. Divide a by temp. PRINT(d)
5. Store the result in d. STOP
6. Print the value of d.
7. Stop.
Evaluate-Algo Evaluate-Pseudo
1. Start START
2. Read the radius rr. READ(r)
3. Calculate the area using the formula Area=PI*r^2 Area = PI * r * r
4. Print the area. PRINT(Area)
5. Stop. STOP
Why pseudocodes?
Sequence Algorithm
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.
S1
S2
S3
.
.
Sn
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.
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, as discussed below:
if structure
Syntax:
if (condition) :
true_instructions
endif
If the test condition is evaluated to True, the statements denoted by true_instructions are executed.
Otherwise, those statements are skipped.
CheckPositive(x)
1 if(x>0):
2 Print(x,“ is positive”)
3 endif
if else structure
if (condition) :
true_instructions
else:
false_instructions
endif
Algorithm Pseudocode
1. Start START
2. Read the value of age READ(age)
3. If age is greater than or equal to 18 IF age >= 18 THEN
4. Print "You are a major"
5. Else PRINT("You are a major")
6. Print "You are a minor" ELSE
7. End PRINT("You are a minor")
ENDIF
STOP
Program:
age = int(input("Enter your age: "))
if age >= 18:
print("You are eligible to vote.")
else:
print("You are not eligible to vote.")
Algorithm Pseudocode
START
1. Start
READ(number)
2. Read the number
3. If the number is divisible by 2 (i.e., IF number % 2 == 0 THEN
number % 2 == 0) PRINT("The number is even.")
o Print "The number is even." ELSE
4. Else PRINT("The number is odd.")
o Print "The number is odd." ENDIF
5. End STOP
Program:
number = int(input("Enter a number: "))
if number % 2 == 0:
print("The number is even.")
else:
print("The number is odd.")
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:
if (condition 1) :
True_instructions 1
else if (condition 2):
True_instructions 2
else:
False_instruction
endif
Algorithm Pseudocode
1. Start START
2. Read the values of x and y READ(x, y)
3. If x is greater than y IF x > y THEN
o Print "x is greater than y" PRINT(x + " is greater than " + y)
4. Else if x is less than y ELSE IF x < y THEN
o Print "x is less than y" PRINT(x + " is less than " + y)
5. Else ELSE
o Print "x is equal to y" PRINT(x + " is equal to " + y)
6. End ENDIF
STOP
Program
x = int(input("Enter the value of x: "))
y = int(input("Enter the value of y: "))
if x > y:
print(x, "is greater than", y)
elif x < y:
print(x, "is less than", y)
else:
Algorithm Pseudocode
1. Start
2. Read the value of score START
3. If score is greater than or equal to 90
READ(score)
Print "Grade: A"
IF score >= 90 THEN
4. Else if score is greater than or equal to 80
PRINT("Grade: A")
Print "Grade: B"
ELSE IF score >= 80 THEN
5. Else if score is greater than or equal to 70
PRINT("Grade: B")
Print "Grade: C"
ELSE IF score >= 70 THEN
6. Else if score is greater than or equal to 60
PRINT("Grade: C")
Print "Grade: D"
ELSE IF score >= 60 THEN
7. Else
PRINT("Grade: D")
Print "Grade: F"
ELSE
8. End
PRINT("Grade: F")
ENDIF
STOP
The case structure is a refined alternative to if else if else structure. The pseudocode representation of the case
structure is given below.
caseof (expression)
case 1 value1:
block1
case 2 value2:
block2
default:
default_block
endcase
Python program, algorithm and pseudocode that prompts the user to enter a number between 1
and 7 and prints the corresponding day of the week.
Algorithm Pseudocode
1. Start.
START
2. Display: "Enter a number (1-7) for
the day of the week:" DISPLAY "Enter a number (1-7) for the day of the week:"
3. Input: Read day_number from user
INPUT day_number
input.
4. Begin caseof structure
on day_number: caseof (day_number)
o Case 1:
§ Print "Monday". case 1:
o Case 2:
PRINT "Monday"
§ Print "Tuesday".
o Case 3: case 2:
§ Print "Wednesday".
PRINT "Tuesday"
o Case 4:
§ Print "Thursday". case 3:
o Case 5:
PRINT "Wednesday"
§ Print "Friday".
o Case 6: case 4:
§ Print "Saturday".
PRINT "Thursday"
o Case 7:
§ Print "Sunday". case 5:
o Default case:
PRINT "Friday"
§ Print "Invalid input!
Please enter a case 6:
number between 1 PRINT "Saturday"
and 7."
5. End caseof structure. case 7:
6. End. PRINT "Sunday"
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. If the number of iterations (how many times
the block is to be executed) is known in advance, it is called definite iteration. Otherwise, it is called
indefinite or conditional iteration. The block that is repeatedly executed is called the loop body. There
are three types of loop constructs as they are,
1) while loop
2) repeat-until loop
3) for loop
A while loop is generally used to implement indefinite iteration. The general form of the while loop is as follows:
while (condition) :
(True_instructions)
endwhile
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.
Write a program that prompts the user to enter a positive integer nn and then calculates and displays the
sum of the first n natural numbers using a while loop.
2) 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. The
pseudocode form of repeat-until loop is shown below.
repeat
False_instructions
until (condition)
May not execute if the condition is Executes at least once regardless of the
Execution Guarantee
initially False condition
3) for loop
The for loop implements definite iteration. There are three variants of the for loop. All three 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.
Iterating Over a Sequence
Example Output
range(5) 0, 1, 2, 3, 4
range(2, 8) 2, 3, 4, 5, 6, 7
range(0, 10, 2) 0, 2, 4, 6, 8
range(5, 0, -1) 5, 4, 3, 2, 1
list(range(2, 21, 2)) [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
►On page and Off Page References: Symbols used when the entire
flowchart cannot fit on the same page fully.
To find the average height of boys and average height of girls in a class of n students.