GE8151-Notes - by WWW - EasyEngineering.net 5
GE8151-Notes - by WWW - EasyEngineering.net 5
net
ww
w.E
a syE
ngi
nee
rin
g.n
et
1.PROBLEM SOLVING
Problem solving is the systematic approach to define the problem and creating
number of solutions.
The problem solving process starts with the problem specifications and ends with a
Correct program.
ww
1.1 PROBLEM SOLVING TECHNIQUES
w.E
Problem solving technique is a set of techniques that helps in providing logic for solving
a problem.
Problem Solving Techniques:
asy
Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts. En
3. Pseudo codes.
4. programs gin
1.2.ALGORITHM
eer
It is defined as a sequence of instructions that describe a method for solving a
ing
problem. In other words it is a step by step procedure for solving a problem.
Properties of Algorithms
Should be written in simple English
.ne
Each and every instruction should be precise and unambiguous.
Instructions in an algorithm should not be repeated infinitely.
Algorithm should conclude after a finite number of steps.
t
Should have an end point
Derived results should be obtained only after the algorithm terminates.
Qualities of a good algorithm
The following are the primary factors that are often used to judge the quality of the
algorithms.
Time – To execute a program, the computer system takes some amount of time. The
lesser is the time required, the better is the algorithm.
Memory – To execute a program, computer system takes some amount of memory
space. The lesser is the memory required, the better is the algorithm.
Accuracy – Multiple algorithms may provide suitable or correct solutions to a given
problem, some of these may provide more accurate results than others, and such
algorithms may be suitable.
ww
process data-perform operation on a given input
output data-processed result
2.2.State: w.E
Transition from one process to another process under specified condition with in a
time is called state. asy
2.3.Control flow: En
gin
The process of executing the individual statements in a given order is called control
flow.
The control can be executed in three ways
1. sequence eer
2. selection
3. iteration ing
Sequence:
.ne
All the instructions are executed one after another is called sequence execution. t
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Display c
Step 5: Stop
Selection:
A selection statement causes the program control to be transferred to a specific
part of the program based upon the condition.
If the conditional test is true, one part of the program will be executed, otherwise
it will execute the other part of the program.
Download From : www.EasyEngineering.net
Unit 1: Algorithmic problem solving 2
Download From : www.EasyEngineering.net
Example
ww
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
w.E
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote”
asy
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration: En
gin
In some programs, certain set of statements are executed again and again based
upon conditional test. i.e. executed more than one time. This type of execution is called
looping or iteration. eer
Example ing
Write an algorithm to print all natural numbers up to n .ne
Step 1: Start
Step 2: get n value.
t
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
2.4.Functions:
Function is a sub program which consists of block of code(set of instructions)
that performs a particular task.
For complex problems, the problem is been divided into smaller and simpler
tasks during algorithm design.
ww
sub function add()
Step 1: Function start
w.E
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
Step 5: Return asy
En
gin
eer
ing
.ne
t
3.NOTATIONS
3.1.FLOW CHART
Flow chart is defined as graphical representation of the logic for problem solving.
The purpose of flowchart is making the logic of the program clear in a visual
representation.
ww
w.E
Rules for drawing a flowchart
asy
1. The flowchart should be clear, neat and easy to follow.
En
2. The flowchart must have a logical start and finish.
3. Only one flow line should come out from a process symbol.
gin
eer
4. Only one flow line should enter a decision symbol. However, two or three flow
lines may leave the decision symbol. ing
.ne
t
5. Only one flow line is used with a terminal symbol.
Advantages of flowchart:
1. Communication: - Flowcharts are better way of communicating the logic of a
system to all concerned.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in more
effective way.
ww
4. Cost: For large application the time and cost of flowchart drawing becomes
costly.
w.E
3.2.PSEUDO CODE:
Pseudo code consists of short, readable and formally styled English languages
used for explain an algorithm.
asy
It does not include details like variable declaration, subroutines.
It is easier to understand for the programmer or non programmer to understand
En
the general working of the program, because it is not based on any programming
language.
gin
It gives us the sketch of the program before actual coding.
It is not a machine readable
Pseudo code can’t be compiled and executed. eer
There is no standard syntax for pseudo code.
Guidelines for writing pseudo code: ing
Write one statement per line
Capitalize initial keyword
.ne
Indent to hierarchy
End multiline structure
t
Keep statements language independent
Common keywords used in pseudocode
The following gives common keywords used in pseudocodes.
1. //: This keyword used to represent a comment.
2. BEGIN,END: Begin is the first statement and end is the last statement.
3. INPUT, GET, READ: The keyword is used to inputting data.
4. COMPUTE, CALCULATE: used for calculation of the result of the given expression.
5. ADD, SUBTRACT, INITIALIZE used for addition, subtraction and initialization.
6. OUTPUT, PRINT, DISPLAY: It is used to display the output of the program.
7. IF, ELSE, ENDIF: used to make decision.
8. WHILE, ENDWHILE: used for iterative statements.
9. FOR, ENDFOR: Another iterative incremented/decremented tested automatically.
w.E
Syntax for While:
END
Example: Print n natural numbers
WHILE (condition) DO
statement asy BEGIN
GET n
...
ENDWHILE En
INITIALIZE i=1
WHILE(i<=n) DO
ginPRINT i
i=i+1
ENDWHILE
END eer
Advantages: ing
Pseudo is independent of any language; it can be used by most programmers.
It is easy to translate pseudo code into a programming language.
.ne
It can be easily modified as compared to flowchart.
Converting a pseudo code to programming language is very easy as compared
with converting a flowchart to programming language.
t
Disadvantages:
It does not provide visual representation of the program’s logic.
There are no accepted standards for writing pseudo codes.
It cannot be compiled nor executed.
For a beginner, It is more difficult to follow the logic or write pseudo code as
compared to flowchart.
Example:
Addition of two numbers:
BEGIN
GET a,b
ADD c=a+b
PRINT c
END
Download From : www.EasyEngineering.net
Unit 1: Algorithmic problem solving 7
Download From : www.EasyEngineering.net
Algorithm Flowchart Pseudo code
An algorithm is a sequence It is a graphical It is a language
of instructions used to representation of algorithm representation of
solve a problem algorithm.
User needs knowledge to not need knowledge of Not need knowledge of
write algorithm. program to draw or program language to
understand flowchart understand or write a
pseudo code.
3.3.PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer
to perform specific tasks. The programmers have to follow all the specified rules before
writing program using programming language. The user has to communicate with the
ww
computer using language which it can understand.
Types of programming language
w.E
1. Machine language
2. Assembly language
3. High level language
Machine language: asy
En
The computer can understand only machine language which uses 0’s and 1’s. In
gin
machine language the different instructions are formed by taking different
combinations of 0’s and 1’s.
Advantages:
Translation free:
eer
ing
Machine language is the only language which the computer understands. For
executing any program written in any programming language, the conversion to
machine language is necessary. The program written in machine language can be .ne
executed directly on computer. In this case any conversion process is not required.
High speed
t
The machine language program is translation free. Since the conversion time is
saved, the execution of machine language program is extremely fast.
Disadvantage:
It is hard to find errors in a program written in the machine language.
Writhing program in machine language is a time consuming process.
Machine dependent: According to architecture used, the computer differs from each
other. So machine language differs from computer to computer. So a program
developed for a particular type of computer may not run on other type of computer.
Assembly language:
To overcome the issues in programming language and make the programming
process easier, an assembly language is developed which is logically equivalent to
machine language but it is easier for people to read, write and understand.
Download From : www.EasyEngineering.net
Unit 1: Algorithmic problem solving 8
Download From : www.EasyEngineering.net
Assembly language is symbolic representation of machine language. Assembly
languages are symbolic programming language that uses symbolic notation to
represent machine language instructions. They are called low level language
because they are so closely related to the machines.
Ex: ADD a, b
Assembler:
Assembler is the program which translates assembly language instruction in to a
machine language.
Advantage:
Easy to understand and use.
It is easy to locate and correct errors.
Disadvantage
Machine dependent
ww
The assembly language program which can be executed on the machine depends
on the architecture of that computer.
w.E
Hard to learn
It is machine dependent, so the programmer should have the hardware
asy
knowledge to create applications using assembly language.
Less efficient
En
Execution time of assembly language program is more than machine language
program.
gin
Because assembler is needed to convert from assembly language to machine
language.
eer
High level language
ing
High level language contains English words and symbols. The specified rules are
.ne
to be followed while writing program in high level language. The interpreter or
compilers are used for converting these programs in to machine readable form.
Translating high level language to machine language t
The programs that translate high level language in to machine language are called
interpreter or compiler.
Compiler:
A compiler is a program which translates the source code written in a high level
language in to object code which is in machine language program. Compiler reads the
whole program written in high level language and translates it to machine language. If
any error is found it display error message on the screen.
Interpreter
Interpreter translates the high level language program in line by line manner. The
interpreter translates a high level language statement in a source program to a machine
ww
Less efficient
The translation process increases the execution time of the program. Programs in
w.E
high level language require more memory and take more execution time to execute.
ww
structured and easily traceable for program flow.
Examples:
Hyper talk
MATLAB w.E
Scripting language: asy
Scripting language are programming languages that control an application.
En
Scripts can execute independent of any other application. They are mostly embedded in
gin
the application that they control and are used to automate frequently executed tasks
like communicating with external program.
Examples: eer
Apple script
VB script ing
Markup languages:
.ne
A markup language is an artificial language that uses annotations to text that
define hoe the text is to be displayed.
t
Examples:
HTML
XML
Concurrent programming language:
Concurrent programming is a computer programming technique that provides
for the execution of operation concurrently, either with in a single computer or across a
number of systems.
Examples:
Joule
Limbo
Object oriented programming language:
Object oriented programming is a programming paradigm based on the concept
of objects which may contain data in the form of procedures often known as methods.
Download From : www.EasyEngineering.net
Unit 1: Algorithmic problem solving 11
Download From : www.EasyEngineering.net
Examples:
Lava
Moto
ww
w.E
asy
En
gin
eer
ing
.ne
t
Understanding the Problem
It is the process of finding the input of the problem that the algorithm solves.
It is very important to specify exactly the set of inputs the algorithm needs to
handle.
A correct algorithm is not one that works most of the time, but one that works
correctly for all legitimate inputs.
Ascertaining the Capabilities of the Computational Device
ww
approach to solving problems algorithmically that is applicable to a variety of
problems from different areas of computing.
w.E
Learning these techniques is of utmost importance for the following reasons.
First, they provide guidance for designing algorithms for new problems,
Second, algorithms are the cornerstone of computer science
gin
constructs. Pseudocode is usually more precise than natural language, and its
usage often yields more succinct algorithm descriptions.
eer
In the earlier days of computing, the dominant vehicle for specifying algorithms
ing
was a flowchart, a method of expressing an algorithm by a collection of
connected geometric shapes containing descriptions of the algorithm’s steps.
.ne
Programming language can be fed into an electronic computer directly. Instead,
it needs to be converted into a computer program written in a particular
computer language. We can look at such a program as yet another way of
t
specifying the algorithm, although it is preferable to consider it as the algorithm’s
implementation.
Proving an Algorithm’s Correctness
Once an algorithm has been specified, you have to prove its correctness. That is,
you have to prove that the algorithm yields a required result for every legitimate
input in a finite amount of time.
A common technique for proving correctness is to use mathematical induction
because an algorithm’s iterations provide a natural sequence of steps needed for
such proofs.
It might be worth mentioning that although tracing the algorithm’s performance
for a few specific inputs can be a very worthwhile activity, it cannot prove the
algorithm’s correctness conclusively. But in order to show that an algorithm is
incorrect, you need just one instance of its input for which the algorithm fails.
Download From : www.EasyEngineering.net
Unit 1: Algorithmic problem solving 13
Download From : www.EasyEngineering.net
Analysing an Algorithm
1. Efficiency.
Time efficiency, indicating how fast the algorithm runs,
Space efficiency, indicating how much extra memory it uses.
2. simplicity.
An algorithm should be precisely defined and investigated with mathematical
expressions.
Simpler algorithms are easier to understand and easier to program.
Simple algorithms usually contain fewer bugs.
Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer
programs. Programming an algorithm presents both a peril and an opportunity.
ww
A working program provides an additional opportunity in allowing an empirical
analysis of the underlying algorithm. Such an analysis is based on timing the
w.E
program on several inputs and then analysing the results obtained.
ENDFOR
INITIALIZE i=1
FOR (i<=n) DO
PRINT i
t
i=i+1
ENDFOR
END
Syntax for While: Example: Print n natural numbers
BEGIN
WHILE (condition) DO GET n
statement INITIALIZE i=1
... WHILE(i<=n) DO
ENDWHILE PRINT i
i=i+1
ENDWHILE
END
ww
w.E
asy
5.2.Recursions:
En
A function that calls itself is known as recursion.
gin
Recursion is a process by which a function calls itself repeatedly until some
specified condition has been satisfied.
eer
Algorithm for factorial of n numbers using recursion:
ing
Main function:
Step1: Start .ne
Step2: Get n
Step3: call factorial(n)
Step4: print fact
t
Step5: Stop
ww
w.E
Pseudo code for factorial using recursion:
Main function:
asy
En
BEGIN
GET n gin
CALL factorial(n)
PRINT fact eer
BIN
ing
Sub function factorial(n):
.ne
IF(n==1) THEN
fact=1
RETURN fact
t
ELSE
RETURN fact=n*factorial(n-1)
ww
w.E
asy
Write an algorithm for Calculating area and circumference of circle
En
Step 1: Start
Step 2: get r value
gin BEGIN
READ r
Step 3: Calculate A=3.14*r*r eer CALCULATE A and C
Step 4: Calculate C=2.3.14*r
Step 5: Display A,C ing A=3.14*r*r
C=2*3.14*r
Step 6: Stop DISPLAY A
.ne
END
t
ww
w.E
asy
Write an algorithm for Calculating engineering cutoff
Step 1: Start
Step2: get P,C,M value En BEGIN
Step3:calculate
gin READ P,C,M
Cutoff= (P/4+C/4+M/2)
Step 4: Display Cutoff eer CALCULATE
Cutoff= (P/4+C/4+M/2)
Step 5: Stop
ing DISPLAY Cutoff
END
.ne
t
ww
w.E
asy
En
To check leap year or not
Step 1: Start gin
Step 2: get y
Step 3: if(y%4==0) print leap year eer
Step 4: else print not leap year
Step 5: Stop
ing
BEGIN .ne
READ y
IF (y%4==0) THEN t
DISPLAY leap year
ELSE
DISPLAY not leap year
END IF
END
ww
Step 2: get num
Step 3: check if(num>0) print a is positive
w.E
Step 4: else num is negative
Step 5: Stop
BEGIN
asy
READ num En
IF (num>0) THEN
DISPLAY num is positive gin
ELSE eer
DISPLAY num is negative
END IF ing
END
.ne
t
ww
END IF
END IF
END
w.E
asy
En
gin
eer
ing
.ne
t
Write an algorithm to check whether given number is +ve, -ve or zero.
Step 1: Start
Step 2: Get n value.
Step 3: if (n ==0) print “Given number is Zero” Else goto step4
Step 4: if (n > 0) then Print “Given number is +ve”
Step 5: else Print “Given number is -ve”
Step 6: Stop
ww
w.E
asy
En
gin
eer
ing
.ne
t
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) go to step 5 else go to step 8
Step 5: Print i value
step 6 : increment i value by 1
Step 7: go to step 4
Step 8: Stop
BEGIN
GET n
INITIALIZE i=1
ww
WHILE(i<=n) DO
PRINT i
i=i+1 w.E
ENDWHILE
END asy
En
gin
eer
ing
.ne
t
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check if(i<=n) goto step 5 else goto step 8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
BEGIN
GET n
ww
INITIALIZE i=1
w.E
WHILE(i<=n) DO
PRINT i
i=i+2
ENDWHILE asy
END
En
gin
eer
ing
.ne
t
Step 1: start
step 2: get n value
step 3: set initial value i=2
step 4: check if(i<=n) goto step 5 else goto step8
step 5: print i value
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
BEGIN
GET n
ww
INITIALIZE i=2
w.E
WHILE(i<=n) DO
PRINT i
i=i+2
ENDWHILE
asy
END En
gin
eer
ing
.ne
t
BEGIN
GET n
INITIALIZE i=1
ww
WHILE(i<=n) DO
PRINT i*i
i=i+2
ENDWHILE
w.E
END
asy
En
gin
eer
ing
.ne
t
ww
PRINT i*i*i
i=i+2
ENDWHILE
END
w.E
asy
En
gin
eer
ing
.ne
t
Step 1: start
step 2: get n value
step 3: set initial value i=1, sum=0
Step 4: check i value if(i<=n) goto step 5 else goto step8
step 5: calculate sum=sum+i
step 6: increment i value by 1
step 7: goto step 4
step 8: print sum value
step 9: stop
BEGIN
GET n
ww
INITIALIZE i=1,sum=0
w.E
WHILE(i<=n) DO
sum=sum+i
i=i+1
ENDWHILE
asy
PRINT sum En
END
gin
eer
ing
.ne
t
Step 1: start
step 2: get n value
step 3: set initial value i=1, fact=1
Step 4: check i value if(i<=n) goto step 5 else goto step8
step 5: calculate fact=fact*i
step 6: increment i value by 1
step 7: goto step 4
step 8: print fact value
step 9: stop
BEGIN
GET n
ww
INITIALIZE i=1,fact=1
w.E
WHILE(i<=n) DO
fact=fact*i
i=i+1
ENDWHILE
asy
PRINT fact En
END
gin
eer
ing
.ne
t
ww
Area & circumference of circle
r=eval(input(“enter the radius of circle”))
output
enter the radius of circle4
a=3.14*r*r
c=2*3.14*r
w.E the area of circle 50.24
the circumference of circle
print(“the area of circle”,a)
print(“the circumference of circle”,c)
asy 25.12
.ne
Calculate engineering cutoff
p=eval(input(“enter physics marks”))
c=eval(input(“enter chemistry marks”))
Output
enter physics marks 100
enter chemistry marks 99
t
m=eval(input(“enter maths marks”)) enter maths marks 96
cutoff=(p/4+c/4+m/2) cutoff = 97.75
print(“cutoff =”,cutoff)
for i in range(1,5,1):
asy 1234
print(i)
En
Print n odd numbers
for i in range(1,10,2): gin Output
print(i) eer
1 3 5 79
ing
Print n even numbers
for i in range(2,10,2):
Output
.ne
print(i)
Print squares of numbers
2 4 6 8
Output
t
for i in range(1,5,1): 1 4 9 16
print(i*i)
for i in range(1,5,1): 1 8 27 64
print(i*i*i)
i=i+2
w.E
print(i) 5
7
asy 9
Print n squares of numbers
i=1 En Output
1
while(i<=5): gin 4
print(i*i)
i=i+1 eer 9
16
ing25
add()
ww
print(“the sum is”,c) the sum is 10
w.E
area of rectangle using function Output
def area(): asy enter the length of
En
l=eval(input(“enter the length of rectangle”))
b=eval(input(“enter the breath of rectangle”))
rectangle 20
enter the breath of
a=l*b gin rectangle 5
print(“the area of rectangle is”,a)
area() eer the area of rectangle is
100
ing
swap two values of variables
def swap():
Output
enter a value3.ne
a=eval(input("enter a value"))
b=eval(input("enter b value"))
enter b value5
a= 5 b= 3
t
c=a
a=b
b=c
print("a=",a,"b=",b)
swap()
En
18. Mention the advantages of using pseudo code?
19. Mention the disadvantages of using pseudo code?
gin
20. What are the ways available to represent algorithm?
21. Differentiate flowchart and pseudo code?
22. Differentiate algorithm and pseudo code? eer
23. What is programming language?
24. Mention the types of programming language?
ing
25. What is mean by machine level language? .ne
26. What are the advantages and disadvantages of machine level language?
27. What is high level programming language and mention its advantages? t
28. What are the steps in algorithmic problem solving?
29. Write the algorithm for any example?
30. Draw the flow chart for any example?
31. Write pseudo code for any example?
Part B:
1. Explain in detail about problem solving techniques?
2. Explain in detail about building blocks of algorithm?
3. Discuss the symbols and rules for drawing flowchart with the example?
4. Explain in detail about programming language?
5. Discuss briefly about algorithmic problem solving?
6. Write algorithm, pseudo code and flow chart for any example?
7. Explain in detail about simple strategies for developing algorithms?
Download From : www.EasyEngineering.net
Unit 1: Algorithmic problem solving 37
Download From : www.EasyEngineering.net
ww
w.E
a syE
ngi
nee
rin
g.n
et