Algorithms and Flowcharts
Algorithms and Flowcharts
Learning Objectives
❖ To demonstrate an algorithm using a real world
paradigm
❖ To solve computational problems using algorithms
and flowcharts
❖ To demonstrate the use the three constructs for
developing algorithms: sequence, decision, and
repetition.
❖ To understand the finite- and infinite- loop.
STEPS FOR PROBLEM SOLVING
ALGORITHM
Algorithm is a step-by-step process of solving a well-defined
computational problem.
following:
result
WRITE AN ALGORITHM TO PRINT
“GOOD MORNING”.
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
• Algorithms have a definite beginning and a definite end,
and a finite number of steps.
• A good algorithm, which is precise, unique and finite,
receives input and produces an output.
• There can be more than one approach to solve a
problem and hence we can have more than one
algorithm for a particular problem.
Objectives
❖solve FLOWCHART
problems by
using flowchart
❖ A flowchart is a type of diagram that represents the algorithm
❖Define and
use the three graphically using boxes of various kinds, in an order connected by
constructs for
arrows.
developing
flowchart:
❖Each shape represents a step of the solution process and the arrow
sequence,
decision, and
represents the order or link among the steps.
repetition.
❖understand
the finite- and
infinite- loop.
Objectives FLOWCHART SYMBOLS
❖solve Symbols Functions
problems by
Start/stop
using flowchart
❖Define and
Input/output
use the three
constructs for
developing Processing
flowchart:
sequence,
decision, and Decision Box
repetition.
❖understand
the finite- and
infinite- loop. Flow of control
Connector
PSEUDOCODE
A pseudocode is another way of representing an algorithm.
It is a detailed description of instructions that a computer must follow
in a particular order.
It is intended for human reading and cannot be executed directly by
the computer.
The word “pseudo” means “not real,” so “pseudocode” means “not
real code”.
PSEUDOCODE
Some frequently used keywords while writing Pseudocode are:
• INPUT
• COMPUTE
• PRINT
• INCREMENT
• DECREMENT
• IF/ELSE
• WHILE
• TRUE/FALSE
WRITE AN ALGORITHM , FLOWCHART AN PSEUDOCODE
TO FIND THE SQUARE OF A NUMBER
Step 1: Get a number and store it to num
Step 2: Compute num * num and store it in square
Step 3: Display square
INPUT num
COMPUTE square = num*num
PRINT square
WRITE AN ALGORITHM TO DISPLAY THE SUM OF TWO
NUMBERS ENTERED BY USER, USING BOTH PSEUDOCODE
AND FLOWCHART.
Step 1: Get two numbers and store it to num1 &num2
Step 2: Compute num1 + num2 and store it in Result
Step 3: Display Result
INPUT num1
INPUT num2
COMPUTE Result = num1 + num2
PRINT Result
WRITE AN ALGORITHM TO CALCULATE AREA AND PERIMETER
OF A RECTANGLE, USING BOTH PSEUDOCODE AND
FLOWCHART.
Step 1: Get length and breadth and store
Step 2: Compute Area=length * breadth
Step3: Compute Perimeter= 2 * (length + breadth)
Step 4: Display Area and Perimeter
INPUT length
INPUT breadth
COMPUTE Area = length * breadth
PRINT Area
COMPUTE Perim = 2 * (length + breadth)
print Perim
FLOW OF CONTROL
❖The flow of control depicts the flow of events.
❖The events can flow in a sequence, or on branch
based on a decision or even repeat some part for a
finite number of times.
❖The program shall be categorized into three ways:
(i) sequence
(ii) selection
(iii) iteration or looping statements.
SEQUENCE STATEMENTS:
In this program,
all the instructions
are executed one
after another.
WRITE AN ALGORITHM TO FIND AREA OF A
RECTANGLE.
Step 1: Start
Step 2: Take length and breadth of rectangle and
store them as Length and Breadth
Step 3: Multiply by Length and Breadth and store
it in area
Step 4: Print area
Step 5: Stop
Excercise 1
©Brooks/Cole, 2003
Excercise 4
©Brooks/Cole, 2003
Objectives DRAW A FLOWCHART TO FIND THE SIMPLE
❖solve
problems by
INTEREST. (SEQUENCE)
using flowchart
❖Define and
use the three
constructs for
developing
flowchart:
sequence,
decision, and
repetition.
❖understand
the finite- and
infinite- loop.
Objectives
❖understand
the concept of
DRAW A FLOWCHART TO FIND BIGGER
Algorithm NUMBER AMONG TWO NUMBERS (SELECTIVE)
❖solve
problems by
using algorithms
❖Define and
use the three
constructs for
developing
algorithms:
sequence,
decision, and
repetition.
❖understand
the finite- and
infinite- loop.
Objectives
❖understand DRAW A FLOW CHART TO FIND FACTORIAL
the concept of
Algorithm
OF ANY NUMBER(ITERATIVE).
❖solve
problems by
using algorithms
❖Define and
use the three
constructs for
developing
algorithms:
sequence,
decision, and
repetition.
❖understand
the finite- and
infinite- loop.
Objectives
❖understand DRAW A FLOW CHART TO FIND BIGGEST
the concept of
Algorithm
NUMBER AMONG “N” NUMBERS.
❖solve
problems by
using algorithms
❖Define and
use the three
constructs for
developing
algorithms:
sequence,
decision, and
repetition.
❖understand
the finite- and
infinite- loop.
Objectives
❖understand
the concept of
FINITE AND INFINITE LOOP
Algorithm In looping statements, if some set of statements are executed “n”
times (fixed number of times), then it is called “finite loop”.
❖solve
problems by At the same time, if some set of statements are executed again and
using algorithms
again without any end (infinite times), then it is called “infinite
loop”.
❖Define and
use the three For example if we are not incrementing “I‟ (index) value, then we
will get endless (infinite) loop. The following is an example of
constructs for infinite loop.
developing
Step 1: Start
algorithms:
sequence, Step 2: Take any number and store it in n.
decision, and Step 3: Store 1 in I
repetition. Step 4: Check I value, if I<=n then go to step 5 else go to step 6
❖understand
the finite- and
Step 5: Print I and Go to step 4
infinite- loop. Step 6: Stop
WRITE A PSEUDOCODE TO PRINT WHETHER A PERSON IS A
CHILD,TEENAGER OR ADULT BASED ON THE CRITERIA GIVEN
BELOW:
CHILD (<13) ,TEENAGER (>13 BUT <20), ADULT (>=20)
INPUT Age
IF Age < 13 THEN
PRINT "Child"
ELSE IF Age < 20 THEN
PRINT "Teenager"
ELSE
PRINT "Adult"
WRITE PSEUDOCODE TO ACCEPT 5 NUMBERS AND PRINT
THEIR AVERAGE
Step1: SET count = 0, sum = 0
Step 2: WHILE count < 5 , REPEAT steps 3 to 5
Step 3: INPUT num
Step 4: COMPUTE sum = sum + num
Step 5: INCREMENT count by 1
Step 6: COMPUTE average = sum/5
Step 7: PRINT average
WRITE PSEUDOCODE TO INPUT N NUMBERS AND
PRINT THE LARGEST NUMBER
Step 1: INPUT n
Step 2: INPUT A
Step 3: SET Count=1, Big=A
Step 4: WHILE Count<n REPEAT Steps 5 to 9
Step 5: INPUT A
Step 6: IF Big<A THEN
Step 7 : SET Big=A
Step 8: INCREMENT count by 1
Step 9: ELSE INCREMENT count by 1
Step 10: PRINT Big
WRITE PSEUDOCODE TO ACCEPT NUMBERS TILL THE USER ENTERS 0
AND THEN FIND THEIR AVERAGE.
Step 1: SET count = 0, sum = 0
Step 2: INPUT num
Step 3: WHILE num is not equal to 0, REPEAT Steps 4 to 6
Step 4: sum = sum + num
Step 5: count = count + 1
Step 6: INPUT num
Step 7: COMPUTE average = sum/count
Step 8: PRINT average
BENEFITS OF PSEUDOCODE
Pseudocode of a program helps in representing the basic functionality of
the intended program.
Pseudocode helps to review the steps to confirm that the proposed
implementation is going to achieve the desire output.
VERIFYING ALGORITHM
Verification means to ensure that the algorithm is working as intended.
The software designer should make sure that the functioning of all the components are
defined correctly, checked and verified in every possible way.
We can check this for small numbers , for which we can manually calculate and verify
the output.
The mechanism of verifying algorithm is called Dry-Run.
A dry run is the process of a programmer manually working through their code to
trace the value of variables. (with pen and paper)
CHARACTERISTICS OF A DRY RUN
It is carried out during design, implementation, testing or maintenance.
It is used to identify logical errors in a code.
TRACE TABLES
Dry running an algorithm is carried out using trace
tables where the impact of each line of the code is
seen on the values of the variables of the algorithm.
Example
Step1: INPUT num num count num<500
Step 2: SET count=0 16 0 True
32 1 True
Step3: WHILE num<500 REPEAT steps 4 to5
64 2 True
Step 4: COMPUTE num= num*2 128 3 True
Step 5: INCREMENT count 256 4 True
Step 6: PRINT num, count 512 5 False
WRITE PSEUDOCODE TO PRINT EVEN NUMBERS FROM 1
TO N AND DRY RUN THE CODE USING TRACE TABLES
Step 1:Print “ enter n” n Num If num<=n num%2==0 Res
ult
Step 2: Input n
6 1 True False
Step 3: SET num=1 2 True True 2
Step 4: While num<=n Repeat steps 5 to 7 3 True False
Step 5:If num Mod2==0 then step6 else step7 4 True True 4
5 True False
Step 6: Print num
6 True True 6
Step 7: Increment num
COMPARISON OF ALGORITHM
There can be more than one approach to solve a problem
using computer and hence we can have more than one
algorithm.
Algorithms can be compared and analysed on the basis of
Time-wise efficiency- the amount of processing time they
need to run
Space-wise efficiency - the amount of memory that is
needed to execute.
COMPARISON OF ALGORITHM
There can be different ways to write algorithms to check whether a given number is
prime or not as shown below:
(i) Starting with divisor 2, divide the given number (dividend) and check if there are
any factors. Increase the divisor in each iteration and repeat the previous steps as
long as divisor < dividend. If there is a factor, then the given number is not prime
(ii) In (i), instead of testing all the numbers till the dividend, only test up to half of the
given value (dividend) because the divisor can not be more than half of the dividend
(iii) In method (i), only test up to the square root of the dividend (numbers)
iv) Given a prior list of prime number till 100, divide the given number by each
number in the list. If not divisible by any number, then the number is a prime else it is
not prime
COMPARISON OF ALGORITHM
i) More processing time. If given no is large it takes more
time to give the output.
ii) is more efficient than i) as it checks for the divisibility till
half the number and thus it reduces the time for
computation.
iii) is even more efficient as it checks for divisibility till the
square root of the number.
iv) use only prime number which is smaller than the given
number for divisibility, it further reduces the calculation.
CODING
Once an algorithm is finalized, it should be coded in a high-level
programming language as selected by the programmer.
Programs written using binary digits are directly understood by the
computer hardware, but they are difficult to deal with and
comprehend by humans. This led to the invention of high-level
languages which are close to natural languages and are easier to
read, write, and maintain, but are not directly understood by the
computer hardware
Example for High Level Languages are
Python, C, C++, Java, Fortran
A program written in a high-level language is called source code. We need to
translate the source code into machine language using a compiler or an
interpreter, so that it can be understood by the computer.
CHARACTERISTICS OF A GOOD PROGRAM
❖Precision — the steps are precisely stated or defined.