You are on page 1of 27

PART I

INTRODUCTION
TO PROGRAMMING
CHAPTER 2
ALGORITHM AND FLOW CHARTS
Algorithms?
An algorithm is procedure consisting of a finite set of
unambiguous rules (instructions) which specify a finite
sequence of operations that provides the solution to a
problem, or to a specific class of problems for any
allowable set of input quantities (if there are inputs). In
other word, an algorithm is a step-by-step procedure to
solve a given problem.
We solve many such trivial problems every day without even
thinking about it, for example making breakfast, travelling
to the workplace etc. But the solution to such problems
requires little intellectual effort and is relatively unimportant.
However, the solution of a more interesting problem of more
importance usually involves stating the problem in an
understandable form and communicating the solution to
others. In the case where a computer is part of the means of
solving the problem, a procedure, explicitly stating the steps
leading to the solution, must be transmitted to the computer.
This concept of problem solution and communication makes
the study of algorithms important to computer science.
Example 1.
S1: Start Problem is the
S2: Input x and Input y summation of the x and
S3: Set z = x+y y values.
S4: Output z
S5: End
Example 2.
S1: Start
Problem is the summation of the
S2: Input x and Input y x and y values and times h and k
values. Then summation of the
S3: Input h and Input k two process results.
S4: Set z = x+y
S5: Set g = h*k
S6: Set n= z+g
S7: Output n
S8: End
Example 3.
S1: Start The given unknown
number is equal or not the
S2: Input N
variable M value according
S3: Set M = 1 and F = 1 to F*M process. This
S4: Set F = F*M program must continue
until M=N.
S5: If M = N GOTO S7
S6: Set M = M + 1 and GOTO S4
S7: Output F
S8: End
Example 4.
S1: Start Write an algorithm to
determine a student’s final
S2: Input M1,M2,M3,M4 grade and indicate whether it
S3: Set N=(M1+M2+M3+M4)/4 is passing or failing. The final
S4: if (N< 60) then grade is calculated as the
average of four marks.
S5: Output “FAIL”
S6: if (N=> 60)
S7: Output “PASS”
S8: End
Example 5.
S1: Start Write an algorithm that will
read the two sides of a
S2: Input W,L rectangle and calculate its
S3: Set k= L x W area.

S4: Print k
S5: End
Example 6.
S1: Start Write an algorithm that will
calculate the roots of a quadratic
S2: Input the coefficients equation
(a, b, c) of the quadratic
ax 2  bx  c  0
equation
S3: Set d Hint: d = sqrt ( b 2  4ac ), and the
roots are:
S4: Set x1
S5: Set x2 x1 = (–b + d)/2a and
S6: Output x1 and x2 x2 = (–b – d)/2a
S7: End
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
DECISION STRUCTURES
Y N
is
A>B

Print A Print B
Decision Structure
IF
Switch
For Loop
While Loops
If control structure has three different types.

IF Condition IF Condition IF Condition


1. Process 1. Process; …
2. Process ELSE ELSEIF …
3. Process 2. Process …
: END ELSE
END …
END
IF–THEN–ELSE STRUCTURE
The structure is as follows

If condition then
true alternative
else
false alternative
end
IF–THEN–ELSE STRUCTURE
The algorithm for the flowchart is as follows:
S1: Start
S2: Input A,B
S3: If A>B Y N
is
S4: Output A A>B
S5: If A<B
S6: Output B Print Print
A B
S7: End
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
 Greater than or equal to
 Less than or equal to
 Not equal to
Example 7.
S1: Start
Write an algorithm that
S2: Input VALUE1, VALUE2 reads three values,
S3: if (VALUE1 > VALUE2) determines the largest value
and prints the largest value
S4: Set MAX =VALUE1 with an identifying message.

S5: if (VALUE1 > VALUE2)


S6: Set MAX = VALUE2
S7: Output MAX
S8: End
Flow Charts
Example 8.
Problem is the
summation of the x and START
y values.
Input
X,y

S1: Start z= X x Y
S2: Input X and Input Y
S3: Set z = X+Y Outout
z
S4: Output z
S5: End
STOP
Example 9. START
Write an algorithm to
determine a student’s final Input
M1,M2,M3,M4
grade and indicate whether it
is passing or failing. The final
grade is calculated as the N=(M1+M2+M3+M4)/4

average of four marks.


S1: Start N
IS Y
N<60
S2: Input M1,M2,M3,M4
S3: Set N=(M1+M2+M3+M4)/4
Output Output
S4: if (N< 60) then “PASS” “FAIL”

S5: Output “FAIL”


S6: if (N=> 60)
S7: Output “PASS” STOP

S8: End
Example 10.
Write an algorithm that will START
read the two sides of a
rectangle and calculate its
Input
area. W, L

k= L x W

S1: Start
S2: Input W,L Output
S3: Set k= L x W k

S4: Print k
S5: End STOP
Write an algorithm that will
calculate the roots of a quadratic
equation Example 11.
ax 2  bx  c  0 START

Hint: d = sqrt ( b  4ac ), and the


2

roots are: Input


a, b, c

x1 = (–b + d)/2a and


d  sqrt(b x b – 4 x a x c)
x2 = (–b – d)/2a
S1: Start x1 (–b + d) / (2 x a)

S2: Input the coefficients


(a, b, c) of the quadratic equation X2  (–b – d) / (2 x a)

S3: Set d
Output
S4: Set x1 x1 ,x2
S5: Set x2
S6: Output x1 and x2
STOP
S7: End
Example 12. START

Input
Fınd maximum one to VALUE1,VALUE2
the given 2 different number

Y is
N
S1: Start VALUE1>VALUE2

S2: Input VALUE1, VALUE2


S3: if (VALUE1 > VALUE2)
S4: Set MAX =VALUE1 MAX  VALUE1 MAX  VALUE2
S5: if (VALUE1 > VALUE2)
S6: Set MAX = VALUE2
S7: Output MAX
S8: End Print
“The largest value is”, MAX

STOP
Example 13.
F(x) function value is given
according to X value. X is
changing between o and 20
with 0.5 increment. Draw a
flow chart for this calculation.

0< X < 2 f(x)= X


2< X < 3 f(x)=X -X3 -22
3< X <4 f(x)=X2 -2X+13
4< X f(x)=X4 -3X2 -43
Example 14.
Write a program to control the given
temperature

S1: Start
S2: Input temperature
S3: if temperature<32
S4: set cover tomatoes
S5: if temperature=>32
S6: set uncover tomatoes
S7: End
Example 15.
Find the summation of the numbers
between 1 and 5

S1: Start
S2: Set i=0
S3: while i<5 go to S4
S4: Set i=i+1
S5: while i=>5 go to S6
S6: Output i
S7: End

You might also like