Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Intro 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

ALGORITHMS AND

FLOWCHARTS

A typical programming task can be divided


into two phases:
 Problem solving phase
 produce an ordered sequence of steps that describe
solution of problem
 this sequence of steps is called an algorithm

 Implementation phase
 implement the program in some programming
language
STEPS IN PROBLEM SOLVING
 First produce a general algorithm

 Refinethe algorithm successively to get


step by step detailed algorithm that is
very close to a computer language.

 Writea procedure to make a TEA in


form of steps.
ALGORITHM
 Example 1: Write an algorithm to determine a
student’s final grade and indicate whether it is
passing or failing. The final grade is calculated as
the average of four marks.
General Steps
 We need 4 marks
 Add all 4 marks
 Divide it by 4
 Check whether grade is less then 50
 If less say he/she has failed
 Else say he/she is passed
DETAILED ALGORITHM

Step 1: Input M1,M2,M3,M4


Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
THE FLOWCHART
A Flowchart

 shows logic of an algorithm


 emphasizes individual steps and their
interconnections
 e.g. control flow from one action to the next
EXAMPLE 1

START

Step 1: Input M1,M2,M3,M4


Input
M1,M2,M3,M4 Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print “FAIL”
GRADE(M1+M2+M3+M4)/
else
Print “PASS”
N Y endif
IS
GRADE<
50

Print Print
“PASS” “FAIL”

STOP
EXAMPLE 2
 Example 2: Write an algorithm and draw a
flowchart to convert the length in feet to
centimeter.

General Steps
 We need length in feet
 Multiply it with 30
Algorithm Flowchart
 Step 1: Input Lfeet START

 Step 2: Lcent  Lfeet x 30


 Step 3: Print Lcent Input
Lfeet

Lcent  Lfeet x 30

Print
Lcent

STOP
EXAMPLE 3
Write an algorithm and draw a flowchart that
will read the three numbers and perform sum

Write an algorithm and draw a flowchart that


will read the two sides of a rectangle and
calculate its area.
Algorithm START
 Step 1: Input W,L
 Step 2: AL x W Input
 Step 3: Print A W, L

ALxW

Print
A

STOP
EXAMPLE 5
 Write an algorithm that reads two values,
determines the largest value and prints the
largest value with an identifying message.

ALGORITHM
Step 1: Input VALUE1, VALUE2
Step 2: if (VALUE1 > VALUE2) then
MAX  VALUE1
else
MAX  VALUE2
endif
Step 3: Print “The largest value is”, MAX
START

Input
VALUE1,VALUE2

Y N
is
VALUE1>
VALUE2

MAX  VALUE1 MAX  VALUE2

Print
“The largest value is”,
MAX

STOP
Write an algorithm and draw a
flowchart that will calculate the
roots of a quadratic equation
Algorithm: START
 Step 1: Input a, b, c
 Step 2: d  sqrt ( b^2 – 4 ac) Input
 Step 3: x1  (–b + d) / (2 x a) a, b, c
 Step 4: x2  (–b – d) / (2 x a)
 Step 5: Print x1, x2 d  sqrt(b x b – 4 x a x c)

x1 (–b + d) / (2 x a)

X2  (–b – d) / (2 x a)

Print
x1,x2

STOP
DECISION STRUCTURES
 The expression A>B is a logical expression

 it describes a condition we want to test

 if A>B is true (if A is greater than B)
we take the action on left

 print the value of A

 if A>B is false (if A is not greater than
B) we take the action on right

 print the value of B
IF–THEN–ELSE STRUCTURE
 The structure is as follows:

If (condition) then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE
 The algorithm for the flowchart is as follows:

If (A>B) then
print A
else
print B Y is N
endif A>B

Print Print
A B
SAMPLE QUES ON ALGORITHMS/FLOWCHART
ASSIGN -1

 Write an Algorithm and Flowchart to find Area of


Circumference.
 Write an Algorithm and Flowchart to find sum of
3 numbers.
 Write an Algorithm and Flowchart to evaluate
product of 6 numbers
 Write an Algorithm and Flowchart to swap 2
numbers.
 Write an Algorithm and Flowchart to evaluate
expression:
x= a - b + c / d + 2 – e
THANK YOU …………

You might also like