algorithm_٠٨٠٩٤٨
algorithm_٠٨٠٩٤٨
Computer Algorithms
Introduction:
The study of computers and the study of algorithms are closely related subjects.
The word “Algorithm” finds its origin in the name of an ozbeck Muslim author Abu Ja’far
Mohammed Ibn Musa ALKOWARIZMI.
An algorithm could be defining as “a step-by-step procedure for solving a given task”.
Notation of Algorithm:
An algorithm must satisfy the following criteria:
a) Input b) Output c) Finiteness d) Definiteness e) Effectiveness
Program Flowchart
Block diagram that uses predefined symbols and interconnecting line to represent the logic and
sequence of specific program operation.
Terminal comment
(start/end)
Input/output
operation Subprogram call
Processing
(calculation and Flow line
storing operation) (direction)
Decision connection
(conditional branching)
Statement n
Example 1:
Give a flowchart for finding the square value of a given number.
Solution:
At the beginning a value is entered to the numerical variable x. The second step involves the
calculation of the square value of x, and assigning to another variable y. the last step is to print
the values of x and y.
Steps: start
Step 1: start(begin).
Step 2: input (enter or read) value of x.
Step 3: set y = x*x.
Step 4: output (print) values of x and y. Input x
Step 5: end.
y = x*x
output x
,y
End
start
Example 2:
Give a flowchart for finding the summation of three numbers.
Solution:
Steps: Input
Step 1: start. x,y,z
Step 2: input (enter) values of x , y and z.
Step 3: set s = x + y + z . s = x+y+z
Step 4: output (print) value of s.
Step 5: end.
output s
End
Example 3:
Give a flowchart for finding the solution of the following equation y = 2x2 + 3z .
Solution:
Steps:
Step 1: start. start
Step 2: input (enter or read) values of x and z.
Step 3: set y = 2*x*x + 3 * z .
Step 4: output (print) value of y. Input x,z
Step 5: end.
output y
End
The assignment operation is used to assign a name to a value. Thus it is used whenever you
need to keep track of a value that is needed later. Some typical uses include:
The assignment operator is not commute i.e. x = e is not the same as e = x. The variable
must be declared. Variables used in the expression must be defined (have values). The type
of the expression must be compatible with the type of the variable.
The order in which assignments are performed is important for example, if the first and second
assignments in the swap sequence were interchanged, x and y would end up assigned to the
same value. The input operation and the output operation share some of the same constraints.
Example 4:
The current contents of A is 5,of B is 3, of I if 12.
In each of the update contents of the quantity on the left-hand side.
I = I +1
A=A+ B
A = A -1
A=I+B
Solution:
I=I+1 I = 12 + 1=13 I = 13
A=A+B A=5+3 A=8
A = A -1 A = 8 -1 A =7
A=I+B A= 13 + 3 A = 16
F T
Logical
Expressio Y
N n
Statement 2 Statement 1
Fig 4.3: branching flowchart
If condition is true
Then do task A
else
Do Task-B
Example 5:
Flowchart for finding the larger of two numbers.
Solution:
At the beginning, numerical values are entered for the two variables x and y. then, there values
are compared, and the larger value is printed out.
Steps: start
Step 1: start.
Step 2: input (enter) values of x and y.
Step 3: if x>y then Input x,y
Print x
Else
Print y F
End if
Step 4: end. x>y
T
Print y
Print x
End
End
Example 7:
Find the solution of the following equation
x , x >=0
y= start
-x , x< 0
Solution: Input x
Steps:
Step 1: start.
Step 2: input (enter) values of x.
F
Step 3: if x>=0 then x>=
Set y = x 0
Else T y = -x
Set y = -x
End if y=x
Step 4: print y
Step 5: end.
Print y
End
End
Looping Flowchart:
A loop represents a segment which may be repeated a definite or indefinite number of times. Fig
(4.4.a) and (4.4.b) represent the definition of two types of loops.
In fig (4.4.a), the instructions are executed one or more cycles when the logical expression is
true. When the logical expression become no longer true, the control is transferred to the
instruction outside the loop. This is known as the while loops.
Fig (4.4.b) represents another definition of a loop. Here, the instruction is executed several
times until the condition becomes true. This is known as the repeat-loop (or do-loop).
Repeat condition For condition
F Instructions
inside loop
Logical
Cond.
F
T
Logical
Cond.
Instructions
inside loop T
Instructions
Instructions outside loop
outside loop
Example 9:
Flowchart for finding the sum of 1+2+3+4+5. start
Solution:
Steps:
Step 1: start. Sum=0,i = 1
Step 2: set sum=0.
Step 3: for i= 1 to 5
Sum= sum+i F
End loop i<=5
Step 4: print sum.
Step 5: end T
i=i+1
end
start
Example 10:
Flowchart for finding the sum of 1+2+…..+n
Solution: Sum=0,i = 1
Steps:
Step 1: start.
Step 2: set sum=0. Input n
Step 3: enter n
Step 4: for i= 1 to n
Sum= sum+i F
End loop i<=n
Step 5: print sum. T
Step 6: end
sum = sum +i Print
sum
i=i+1
end
start
Example 11:
Flowchart for finding the sum of 2+4+…..+n
Solution: Sum=0,i = 2
Steps:
Step 1: start.
Step 2: set sum=0.
Input n
Step 3: enter n
Step 4: for i= 2 to n F
Sum= sum+i
Set i= i+2 i<=n
End loop T
Step 5: print sum.
Step 6: end sum = sum +i Print
sum
i=i+2
end
Start Step X Y
1 0 0
2 1 1
X=0 3 2 3
4 3 6
5 4 10
Y=0
6 5 15
X = X +1
Y=X+Y
F
X>=5
T
Print Y
End
Example 14:
Step X Y
Start 1 0 0
2 1 1
3 2 3
X=0 4 3 6
5 4 10
6 5 15
Y=0
X = X +1
Print X, Y
Y=X+Y
F
X>
=5
T
Print
X,Y
End