Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

Chapter Two

Problem solution and algorithms

 Computers are used to solve users


problems of different type
 Computers are instructed to solve these
problems
 Program: A set of directions (instructions)
telling a computer exactly what to do.
 programmer or developer: A person who
write/develop program
 follow series of steps and cycles starting
from collecting user specifications until the
software is ready for delivery. 1
MTU, Electrical & Computer Engineering 2012
 User: uses developed /programmed
software
 Programming Languages: Languages
for specifying sequences of directions
to a computer.

MTU, Electrical & Computer Engineering 2012 2


2.1. Procedure for Problem Solving

 For effectively solving problems by


computer techniques, a few steps
should be understood and followed
 There are basically five steps in
solving a problem.
1. First, spend some time in
understanding the problem
thoroughly
 Read each statement to answer “what is
expected by solving the problem”

MTU, Electrical & Computer Engineering 2012 3


2. Now construct a list of variables that
are needed to solve the problem.
3. Decide the output of your program
4. Program development
 At this stage algorithms are designed
 Decide what type of steps you must follow
(depending on input to find desired output)
5. Test your program

MTU, Electrical & Computer Engineering 2012 4


Algorithm
 A sequence of language independent
steps which may be followed to solve a
problem.
 The sequence of instructions for solving
a particular problem.
 There are three basic logic structures to
write algorithm:
 sequence
 selection
 iteration

MTU, Electrical & Computer Engineering 2012 5


Sequence logic
 used for performing instructions one
after another in sequence.
 The logic flow of an algorithm is from
the top to the bottom

 Example:
 Design an algorithm that adds two numbers
and display the result

MTU, Electrical & Computer Engineering 2012 6


Solution
 Step 1: read the first number
 Step 2: read the second number
 Step 3: add the numbers
 Step 4: display the result

 In the above problem the instructions


are executed in the order they are
from top to bottom.

MTU, Electrical & Computer Engineering 2012 7


Example:
Design an algorithm which calculates area of
a circle

Solution
1. Start.
2. Input radius in cm.
3. Calculate, area = 3.14 * (radius * radius)
4. Display area.
5. Stop

MTU, Electrical & Computer Engineering 2012 8


Selection logic
 Also known as decision logic
 Used for making decision
 Selecting the proper path out of two or
more alternative paths in the program logic
 If condition is true do process 1, else do process
2

 Example 1
 Design an algorithm which calculates a
quadratic equation
Ax2 + BX + C = 0
MTU, Electrical & Computer Engineering 2012 9
solution
 Step 1: Read the value of A, B and C
and store them in the memory.
 Step 2: If A = 0 and B = 0 then print
"No root exists" and stop. Else continue.
 Step 3: If A = 0 and B is not equal to 0,
then r1 = - C/B and output the
root. Else continue.
 Step 4: Compute D = (B2- 4AC)
 Step 5: If D = 0, then compute
r1 = r2 =( -B/2A), output r1 and r2 .
Else continue.
MTU, Electrical & Computer Engineering 2012 10
 Step 6: If D < 0, then write "Roots are
not real" and stop. Else continue.
 Step 7: If D > 0, then calculate
r1 = ( -B + (B2- 4AC)/(2A))
r2 = ( -B -(B2- 4AC)/(2A)) and
display r1 and r2

MTU, Electrical & Computer Engineering 2012 11


Example 2: Algorithm for student status

1. Start.
2. Accept five different subjects’ marks, i.e.
m1, m2, m3, m4, m5.
3. Calculate average ,
AV = (m1+m2+m3+m4+m5) / 5
4. If AV >= 85 then display “Excellent”
elseIf AV >= 75 then display “Very good”
elseIf AV >= 60 then display “Good”
elseIf AV >= 40 then display “Satisfactory”
else Display “Fail”.
5. Stop.
MTU, Electrical & Computer Engineering 2012 12
Iteration
 Used when one or more instructions
may be executed several times
depending on some condition.
 Single instructions used repeatedly

 Example. Design an algorithm that adds


100 numbers and display the result

MTU, Electrical & Computer Engineering 2012 13


Solution
 Step1 : Set i =1
Set sum=0
 Step 2: Read no(i)
 Step 3: Add sum and no(i)
Increment i
 Step 4: If i<=100 go to step 2
else continue
 Step 5: Write sum

MTU, Electrical & Computer Engineering 2012 14


Characteristics of the Instructions in an
Algorithm

 The sequence of instructions must


possess the following characteristics for
qualifying as an algorithm.
1. Each and every instruction should b e
precise and unambiguous.
2. Each instruction should be such that
it can be performed in a finite time.

MTU, Electrical & Computer Engineering 2012 15


3. One or more instructions should not
be repeated indefinitely.
4. This ensures that the algorithm will
ultimately terminate.
5. After performing the instructions,
that is, after the algorithm
terminates, the desired results must
be obtained

MTU, Electrical & Computer Engineering 2012 16


Flow chart and pseudo code
 Programmers use different kinds of
tools or aids which help them in
developing programs and algorithms
faster and better.
 Flowcharts and pseudo code are the
well known representations of
algorithms.

MTU, Electrical & Computer Engineering 2012 17


Flowcharts
 show the sequence of instructions in a single
program or subroutine
 representation of the sequence of operations in
an information system or program.
 boxes of different shapes to denote different
types of operations
 actual instructions are written within these
boxes
 boxes are connected by solid lines having arrow
marks to indicate the flow of operation
 Since a flowchart shows the flow of operations
in pictorial form, any error in the logic of the
procedure can be detected easily
MTU, Electrical & Computer Engineering 2012 18
Flowchart Symbols
 Only a few symbols are needed to indicate
the necessary operations in a flowchart
 These symbols have been standardized by
ANSI
Terminal Input / Output

 Begin (start) - denote any


input/output
 End (stop)
 Pause (HAULT)

MTU, Electrical & Computer Engineering 2012 19


Process
 represent arithmetic and data movement
instructions
 Denotes all arithmetic operations
 Denotes all logical process of moving data from
one location of the main memory to another.

 used to indicate the flow of operation


 Denotes the exact sequence in which the
instructions are to be executed
 The normal flow of flowchart is from top to
bottom and left to right.
MTU, Electrical & Computer Engineering 2012 20
Decision

 Used to indicate a point at which a decision


has to be made and a branch to one of two
or more alternative points is possible.
 used whenever selection logic is to be shown
 the condition upon which each of the
possible exit paths will be executed should
be identified and all the possible paths
should be accounted for. During execution,
the appropriate path is followed depending
upon the result of the decision.
MTU, Electrical & Computer Engineering 2012 21
 represents an entry from, or an exit to
another part of the flowchart
 Used in long flow charts to minimize the
complexity of flow charts due to
crisscrossing of flow lines
 If the flow chart is more than one page,
it helps to show the connection using
numbers in connectors.

MTU, Electrical & Computer Engineering 2012 22


MTU, Electrical & Computer Engineering 2012 23
23
MTU, Electrical & Computer Engineering 2012 24
Stop MTU, Electrical & Computer Engineering 2012 25
Pseudo code
 Pseudo code is another program analysis tool
that is used for planning program logic.
 "Pseudo" means imitation or false
 “Code" refers to the instructions written in a
programming language
 is an imitation of actual computer instructions
 These pseudo instructions are phrases written
in ordinary natural language
 pseudo code uses a structure that resembles
computer instructions
 pseudo code is also called Program Design
Language(PDL).b
MTU, Electrical & Computer Engineering 2012 26
Structure of Pseudo code
1. Sequence : sequence structures are written in English- like
words in sequence/step top- down
2. Selection:
IF (condition 1)
THEN process -1
ELSE IF (condition 2)
Process 2
 
ELSE IF (condition n)
Process n
ELSE
Default process
END IF
3. Iteration :

MTU, Electrical & Computer Engineering 2012 27


3. Iteration :
 Iteration logic is used when one or more
instructions may be executed several
times depending on some condition. It
uses DO...WHILE structure depicted
below.
DO WHILE (condition)
Process-1
Process-2
Process-n
END DO

MTU, Electrical & Computer Engineering 2012 28


Example: Design a flowchart and pseudo code to
compute an interest on loan given rate and balance
according to the formula
interest = rate*balance

Pseudo code
BEGIN
READ balance, rate, name
Interest=balance*rate
WRITE name, interest
END

MTU, Electrical & Computer Engineering 2012 29


Example2: Design a flowchart and pseudo code that
computes sum product and average of three numbers

Pseudo code
BEGIN
READ X, Y, Z
S=X+Y+Z
A=S/3
P=X*Y*Z
WRITE S, A, P
END

MTU, Electrical & Computer Engineering 2012 30


Programming Errors
 Debugging is the process of detecting and fixing
errors found in a program
 There are three types of errors
1.Syntax/semantic errors
Caused by giving a compiler a program it can not
recognize
2. Logic errors
Come from compilers correctly but fail to execute as
expected (e.g. c = a + b instead of c = a-b)
3. Runtime errors
occur when a program is run on the computer and
the results are not achieved due to some
misinterpretation of a particular instruction.
e.g. dividing by zero
MTU, Electrical & Computer Engineering 2012 31
Assembler, Compiler and Interpreter
 Assembler: A program which translates an assembly
language program into a machine language program
(self assembler & cross assembler)
 Compiler: A program which translates a high-level
language program into a machine language program
 A compiler is more intelligent than an assembler
 Interpreter: An interpreter is a program which
translates one statement of a high-level language
program into machine codes and executes it
 A compiler is nearly 5 to 25 times faster than an
interpreter
 An interpreter is a smaller program as compared to
the compiler. 32
MTU, Electrical & Computer Engineering 2012

You might also like