0% found this document useful (0 votes)
11 views11 pages

algorithm_٠٨٠٩٤٨

Uploaded by

ali739146560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

algorithm_٠٨٠٩٤٨

Uploaded by

ali739146560
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Computer Skills 37

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

Steps of Solving A problem:


Phase 1: problem definition.
Phase 2: problem analysis.
Phase 3: algorithm develpoment.
Phase 4: program writing (program coding).
Phase 5: program transfer to the computer.
Phase 6: program testing and translation.
Phase 7: execution of program and output result.

Algorithms and Programs:


An algorithm can be described in two ways: flowchart, and/or pseudo-code.
Flowchart: represents a good, visible graph-wise way of solving algorithms.
pseudo-code: a step-by-step procedure written using a natural language in way that is very
closed to programming languages.
It is also to be mentioned, that the computer program and the algorithm are two different
representations for the same thing. The computer program is often defined to be “the expression
of an algorithm in a programming language.”

Program Flowchart
Block diagram that uses predefined symbols and interconnecting line to represent the logic and
sequence of specific program operation.

Basic program flowcharting symbols:

Terminal comment
(start/end)
Input/output
operation Subprogram call

Processing
(calculation and Flow line
storing operation) (direction)

Decision connection
(conditional branching)

Eng. Reema Faisal Ali Aljaberi


Computer Skills 38

Fig4.1: Essential symbols used with program flowcharts.


Types of Program Flowcharts:
There are three types of program flowcharts:
 Sequential flowchart
 Branching (jump) flowchart.
 Loop (repetition) flowchart.
Depending on the given problem, sequential, branching and looping parts are combined in
different ways to represent the solution of a given problem.

The Sequential Flowchart:


It is the simplest type of flowcharts, involving no decision (i.e. no branches or loops). Fig 4.3
gives an example for this type of flowchart. When such a program is executed, each statement
will be carried out respecting the order given by the flowchart.
The pseudo-code will be as follows start
Start
Get the sum
Average = sum / 6
Output the average Statement 1
Stop(end)

Statement n

End Fig 4.2: a sequential flowchart

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

Eng. Reema Faisal Ali Aljaberi


Computer Skills 39

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.

y = 2*x *x+ 3*z

output y

End

Eng. Reema Faisal Ali Aljaberi


Computer Skills 40
Assignment Statement:
The assignment operation has the form: variable:= expression. (Pascal) variable = expression.
(C/C++/Java)

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:

• initialize a variable (count = 0 )


• increment/decrement a counter (count = count + 1 )
• accumulate values ( sum = sum + item )
• capture the result of a computation ( y = 3*x + 4 )
• swap two values ( t = x; x = y; y = t )

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

Branching ( jump) Flowchart:


In this type of flowcharts, one or more decision boxes are introduced, and the action taken
depends upon whether the result of the decision test is true(yes) or false (no). this operation is
illustrated by fig(4.4).

F T
Logical
Expressio Y
N n
Statement 2 Statement 1
Fig 4.3: branching flowchart

Eng. Reema Faisal Ali Aljaberi


Computer Skills 41
In pseudocode form we get

If condition is true
Then do task A
else
Do Task-B

The logical operators used in our pseudo-code are


= is equal to
> is greater than
< is less than
>= is greater than or equal
<= is less than or equal
<> is not eaqual to

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

Eng. Reema Faisal Ali Aljaberi


Computer Skills 42
Example 6:
Find the largest of 3 numbers.
Solution: start
Steps:
Step 1: start.
Step 2: input (enter) values of x ,y and y. Input x,y
Step 3: if x>y then
If x>z then
Print x T F
else
print z x>y
end if
Else F F T
If y> z then T
Print y x>z y>z
Else
Print z
End if Print y
Print x Print y
End if
Step 4: 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

Eng. Reema Faisal Ali Aljaberi


Computer Skills 43
Example 8:
Find the solution of the quadratic equation
ax2+bx+c=0. start
Solution:
Steps:
Step 1: start. Input a,b,c
Step 2: input (enter) values of a,b,c.
Step 3: set d = b*b – 4 * a * c
Step 4: if d = 0 then
Print –b / ( 2 * a) d= b*b – 4*a*c
Elseif d> 0 then
Print (- b + √ d) / ( 2 * a), (- b - √ d) / ( 2 * a) F
Else T
Print “ no solution” d=0
End if
Step 5: end. F
d>0
T
Print "no Print -b/(2*a)
solution" Print (-b+√d)/(2*a),
(-b-√d)/(2*a)

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

D=true For i=exp1 to exp2 do


Repeat Statements
Statements Next i
Until (D=false) Endfor

Eng. Reema Faisal Ali Aljaberi


Computer Skills 44

F Instructions
inside loop
Logical
Cond.
F
T
Logical
Cond.
Instructions
inside loop T

Instructions
Instructions outside loop
outside loop

(a) While-loop (b) repeat-loop

Fig 4.4: looping flowcharts

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

sum = sum +i Print


sum

i=i+1
end

Eng. Reema Faisal Ali Aljaberi


Computer Skills 45

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

Eng. Reema Faisal Ali Aljaberi


Computer Skills 46
Example 12: start
Flowchart for finding the sum and the average of
15 numerical values.
Solution:
Explanation: Sum=0,count = 1
We need 4 variables:
X: used each time to enter one numerical value.
Sum: to find the sum of the 15 values. F
Average: to find the mean value.
Count: loop counter count<=15
Steps:
Step 1: start.
T average= sum /15
Step 2: set sum=0.
Step 3: for count= 1 to 15 Input x
Enter x
sum= sum+x Print sum,
End loop sum = sum +x average
Step 5: set average=sum/15
Step 5: print sum, average.
Step 6: end count=count+1 end

Manually Tracing Arithmetic Operations:


Example 13:

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

Eng. Reema Faisal Ali Aljaberi


Computer Skills 47

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

Eng. Reema Faisal Ali Aljaberi

You might also like