You are on page 1of 52

ALGORITHMS & 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.

While writing an algorithm, it is required to clearly identify the

following:

• The input to be taken from the user

• Processing or computation to be performed to get the desired

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

1. Write an algorithm, pseudocode and flowchart that


finds the average of two numbers
SELECTIVE STATEMENTS
In this, some portion of the
program is executed
based upon the conditional
test. If the conditional test
is true, compiler will
execute some part of the
program, otherwise it will
execute the other part of
the program.
WRITE AN ALGORITHM TO CHECK
WHETHER PERSON IS ELIGIBLE TO VOTE?
Step 1: Start
Step 2: Take age of the person and
store it in age
Step 3: Check age value, if age >=
18 then go to step 4 else step 5
Step 4: Print “Eligible to vote” and go
to step 6
Step 5: Print “Not eligible to vote”
Step 6: Stop
Excercise 2

1. Write an algorithm to get total marks and display the


Student’s result whether passed / Failed.
2. Write an algorithm to check whether a number is odd
or even
WRITE A PSEUDOCODE TO CHECK
WHETHER A NUMBER IS ODD OR EVEN
PRINT "Enter the Number"
INPUT number
IF number MOD 2 == 0 THEN
PRINT "Number is Even"
ELSE
PRINT "Number is Odd"
Excercise 3

Write an algorithm to check whether given


number is +ve, -ve or zero.
WRITE AN ALGORITHM TO CHECK WHETHER GIVEN
NUMBER IS +VE, -VE OR ZERO.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Check n value, if n > 0 then go to step 5 else go to step 4
Step 4: Check n value, if n < 0 then go to step 6 else go to step 7
Step 5: Print “Given number is +ve” and go to step 8
Step 6: Print “Given number is -ve” and go to step 8
Step 7: Print “Given number is zero”
Step 8: Stop
REPETITION/ITERATIVE
STATEMENTS
In some programs, certain set
of statements are executed
again and again based upon
conditional test. i.e. executed
more than one time. This
type of execution is called
“looping or iteration‟.
WRITE AN ALGORITHM TO PRINT ALL
NATURAL NUMBERS UP TO “N”.
Step 1: Start
Step 2: Take any number and store it in n.
Step 3: Store 1 in I
Step 4: Check I value, if I<=n then go to step 5 else go to step 7
Step 5: Print I
Step 6: Increment I value by 1 and go to step 4
Step 7: Stop
Excercise 4

1. Write an algorithm to accept 5 numbers and find their


sum and average
2. Write an algorithm to accept numbers till the user
enters 0 and then find their average.
Excercise 4
1. Write an algorithm to accept numbers till the user enters 0 and then
find their average.
Step 1: Start
Step 2: Take a number and store it in n.
Step 3: Store 0 in Count and sum=0
Step 4: Check n value, if n<>0 then go to step 5 else go to step 9
Step 5: sum=sum+n
Step 6: Increment Count value by 1
Step 7 : Take a number and store it in n.
Step 8: Go to step 4
Step 9: Store Average=sum/Count
Step 10: Print Average
Step 11: Stop
Excercise 4

1. Write an algorithm to determine a student’s final


grade and indicate whether he/she is passing or failing.
The final grade is calculated as the average of four
marks.
2. Write an algorithm to find the largest and sum of n
numbers.
3. Write an algorithm to convert the length in feet to
centimeter
Excercise 4
1. Write an algorithm to find the largest of n numbers
Step 1: Start
Step 2: Take a no of numbers to enter and store in n.
Step 3: Store 1 in I and large=0
Step 4: Check I value, if I<=n then go to step 5 else go to step 10
Step 5: Take a number and store it in num
Step 6: Increment I value by 1
Step 7 : check if large<num the go to step 8 else go to step 9
Step 8: store large=num
Step 9: Go to step 4
Step 10: Print large
Step 11: Stop

©Brooks/Cole, 2003
Excercise 4

Develop an algorithm to enter a mark scored by a student


in a particular exam into grades (A,B,C,D) as given
Numerical Letter Grade
condition
<40 E
>=40 and <=54 D
>=55 and <=69 C
>=70 and <=85 B
>85 A
Algorithm
Step 1: Start
Step 2: Take a marks of a student out of 100 and store in marks.
Step 3: Check if marks>85 then go to step 11 else goto step 4
Step 4: Check if marks>=70 then go to step 10 else goto step 5
Step 5: Check if marks>=55 then go to step 9 else goto step 6
Step 6: Check if marks>=40 then go to step 8 else goto step 7
Step 7: print “Grade E” and go to step 12
Step 8 : print “Grade D” and go to step 12
Step 9: print “Grade C” and go to step 12
Step 10: print “Grade B” and go to step 12
Step 11: print “Grade A”
Step 12: Stop

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

❖Uniqueness — results of each step are uniquely defined and only


depend on the input and the result of the preceding steps.

❖Finiteness — the algorithm always stops after a finite number of steps.

❖Input — the algorithm receives some input.

❖Output — the algorithm produces some output.


DECOMPOSITION
Sometimes a problem may be complex, that is, its solution is not directly
derivable. In such cases, we need to decompose it into simpler parts.
The basic idea of solving a complex problem by decomposition is to
'decompose' or break down a complex problem into smaller sub
problems
These sub problems are relatively easier to solve than the original
problem. Finally, the subproblems are combined in a logical way to
obtain the solution for the bigger, main problem.
Each sub problem can be solved independently and by different persons
(or teams). Having different teams working on different sub problems
can also be advantageous because specific sub problems can be
assigned to teams who are experts in solving such problems.
QUESTIONS
1. Write an algorithm, flowchart and pseudocode to find the area of a
triangle.
3. Write an algorithm, flowchart and pseudocode to find the sum of all
even number up to given number.
4. Write an algorithm, flowchart and pseudocode to find the area of a
circle.
5. Write an algorithm, flowchart and pseudocode to find the smallest
number among n numbers.
6. Write an algorithm, flowchart and pseudocode to find the sum of all
multiples of 5 up to given number.
7. Write an algorithm, flowchart and pseudocode to find sum of n numbers.

You might also like