You are on page 1of 59

Algorithms and Flowchart

Dr. Odelu Vanga

Department of Computer Science and Information Systems


Birla Institute of Technology and Science Pilani
Hyderabad Campus
odelu.vanga@hyderabad.bits-pilani.ac.in

January 24, 2020

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 1 / 40


Today’s Topics

Flowchart

for problem solving

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 2 / 40


Algorithm and Flowchart

Algorithm
Sequence of steps
which will produce the desired output
for the given input

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 3 / 40


Algorithm and Flowchart

Algorithm
Sequence of steps
which will produce the desired output
for the given input

Flowchart
Graphical representation
to the logical steps to carry out a task
and show how the steps relate to each other

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 3 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 4 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 5 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 6 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 7 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 8 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 9 / 40


Flowchart symbols

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 10 / 40


Sequence Flowchart
Find the area of a circle of radius r ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 11 / 40


Sequence Flowchart
Find the area of a circle of radius r ?
Area = πr 2 , where π = 3.14.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 11 / 40


Sequence Flowchart
Find the area of a circle of radius r ?
Area = πr 2 , where π = 3.14.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 11 / 40


Sequence Flowchart
Find the area of a circle of radius r ?
Area = πr 2 , where π = 3.14.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 12 / 40


Sequence Flowchart
Find the area of a circle of radius r ?
Area = πr 2 , where π = 3.14.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 13 / 40


Sequence Flowchart
Find the area of a circle of radius r ?
Area = πr 2 , where π = 3.14.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 14 / 40


Finding Average Marks
A, B, C are the marks scored by a student in Science,
Mathematics and English

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 15 / 40


Finding Average Marks
A, B, C are the marks scored by a student in Science,
Mathematics and English

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 15 / 40


Finding Average Marks
A, B, C are the marks scored by a student in Science,
Mathematics and English

Esha, who scores 60 marks in Science,


70 marks in mathematics and 75 marks
in English. Provide the step-by-step
execution of the algorithm to calculate
the average marks of Esha.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 15 / 40


Finding Average Marks
A, B, C are the marks scored by a student in Science,
Mathematics and English

Esha, who scores 60 marks in Science,


70 marks in mathematics and 75 marks
in English. Provide the step-by-step
execution of the algorithm to calculate
the average marks of Esha.

First step: 60, 70, 75

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 15 / 40


Finding Average Marks
A, B, C are the marks scored by a student in Science,
Mathematics and English

Esha, who scores 60 marks in Science,


70 marks in mathematics and 75 marks
in English. Provide the step-by-step
execution of the algorithm to calculate
the average marks of Esha.

First step: 60, 70, 75


Second step: ———————?
Third step: ———————— ?
Forth step: ———————— ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 15 / 40


Selection flow chart

if condition is true
Then
Process step(s) 1
else
Process step(s) 2
end if

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 16 / 40


Selection flow chart

if condition is true
Then
Process step(s) 1
else
Process step(s) 2
end if

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 16 / 40


Repetition Flowchart

while condition is true


Do
Process Step(s)
Loop

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 17 / 40


Repetition Flowchart

while condition is true


Do
Process Step(s)
Loop

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 17 / 40


Flowchart of multiplication table for a given number N

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 18 / 40


Flowchart of multiplication table for a given number N

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 19 / 40


Flowchart of multiplication table for a given number N

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 20 / 40


Flowchart of multiplication table for a given number N

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 21 / 40


Flowchart of multiplication table for a given number N

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 22 / 40


Flowchart of multiplication table for a given number N

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 23 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 24 / 40


Flowchart - Snakes and Ladder game
How to play:
Player’s counter start at ’1’.
Roll the dice ⇒ Move counter.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 24 / 40


Flowchart - Snakes and Ladder game
How to play:
Player’s counter start at ’1’.
Roll the dice ⇒ Move counter.
- Counter lands at the bottom
of a ladder ⇒

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 24 / 40


Flowchart - Snakes and Ladder game
How to play:
Player’s counter start at ’1’.
Roll the dice ⇒ Move counter.
- Counter lands at the bottom
of a ladder ⇒ Move up to the
top of the ladder
- Counter lands on the head of
a snake ⇒

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 24 / 40


Flowchart - Snakes and Ladder game
How to play:
Player’s counter start at ’1’.
Roll the dice ⇒ Move counter.
- Counter lands at the bottom
of a ladder ⇒ Move up to the
top of the ladder
- Counter lands on the head of
a snake ⇒ Must slide down
to the bottom of the snake
The first player to get to the
space ’30’ is the winner.

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 24 / 40


Flowchart - Snakes and Ladder game
How to play:
Player’s counter start at ’1’.
Roll the dice ⇒ Move counter.
- Counter lands at the bottom
of a ladder ⇒ Move up to the
top of the ladder
- Counter lands on the head of
a snake ⇒ Must slide down
to the bottom of the snake
The first player to get to the
space ’30’ is the winner.

Develop Algorithm and then


Draw a flowchart to solve this
problem ?

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 24 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 25 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 26 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 27 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 28 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 29 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 30 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 31 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 32 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 33 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 34 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 35 / 40


Flowchart - Snakes and Ladder game

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 36 / 40


Switch-Case Flowchart

Note that expression must return


integer constant value

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 37 / 40


Switch-Case Flowchart

switch (expression)
{
case 1:
Statement 1;
break;

Note that expression must return


integer constant value

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 37 / 40


Switch-Case Flowchart

switch (expression)
{
case 1:
Statement 1;
break;
case 2:
Statement 2;
break;

Note that expression must return


integer constant value

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 37 / 40


Switch-Case Flowchart

switch (expression)
{
case 1:
Statement 1;
break;
case 2:
Statement 2;
break;
..
.
case n:
Statement n;
break;

Note that expression must return


integer constant value

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 37 / 40


Switch-Case Flowchart

switch (expression)
{
case 1:
Statement 1;
break;
case 2:
Statement 2;
break;
..
.
case n:
Statement n;
break;
default:
Note that expression must return Statement;
integer constant value
}

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 37 / 40


Example: switch case

Write a switch statement to print a op b, where op ∈ {+, −, ∗, invalid}

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 38 / 40


Example: switch case

Write a switch statement to print a op b, where op ∈ {+, −, ∗, invalid}


switch (op)
{
case ’+’:
printf(”%d”,a+b);
break;
case ’-’:
printf(”%d”,a-b);
break;
case ’*’:
printf(”%d”,a*b);
break;
default:
print (”invalid”);
}

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 38 / 40


Solve the Examples

1. Find profit or loss when selling a pen.


2. Find factorial of a given number N.
3. Print multiplication table for a given number N.
4. Find the binary equivalent of given number N only using loop concept.
For example, N = 372.
6. Develop algorithm and flowchart for the problem:

1
f (1) = 1+
1
1
f (2) = 1+
1 + 12
1
f (3) = 1+
1 + 1+1 1
3

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 39 / 40


.

Thank You

Dr. Odelu Vanga (BITS-Pilani Hyderabad) CS F111 January 24, 2020 40 / 40

You might also like