GE3151 Problem Solving and Python Programming - 04 - by LearnEngineering - in
GE3151 Problem Solving and Python Programming - 04 - by LearnEngineering - in
in
1.PROBLEM SOLVING
Problem solving is the systematic approach to define the problem and creating
.in
number of solutions.
The problem solving process starts with the problem specifications and ends with a
ng
Correct program.
a problem.
Problem Solving Techniques:
eri
Problem solving technique is a set of techniques that helps in providing logic for solving
ine
Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
g
3. Pseudo codes.
En
4. programs
1.2.ALGORITHM
It is defined as a sequence of instructions that describe a method for solving a
arn
Example:
Example
Write an algorithm to print „Good Morning”
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
.in
In a computer statements might include some of the following actions
input data-information given to the program
ng
process data-perform operation on a given input
output data-processed result
2.2.State:
eri
Transition from one process to another process under specified condition with in a
ine
time is called state.
2.3.Control flow:
g
The process of executing the individual statements in a given order is called control
En
flow.
The control can be executed in three ways
1. sequence
arn
2. selection
3. iteration
Le
Sequence:
All the instructions are executed one after another is called sequence execution.
w.
Example:
Add two numbers:
ww
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.
.in
ng
Example
Write an algorithm to check whether he is eligible to vote?
Step 1: Start
Step 2: Get age
Step 3: if age >= 18 print “Eligible to vote” eri
ine
Step 4: else print “Not eligible to vote”
Step 6: Stop
g
Iteration:
En
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.
arn
Example
Le
Step 1: Start
w.
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.
.in
Step 2: Call the function add()
Step 3: Stop
ng
sub function add()
Step 1: Function start
Step 2: Get a, b Values
Step 3: add c=a+b
Step 4: Print c
eri
ine
Step 5: Return
g
En
arn
Le
w.
ww
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.
.in
ng
eri
ine
Rules for drawing a flowchart
1. The flowchart should be clear, neat and easy to follow.
2. The flowchart must have a logical start and finish.
3. Only one flow line should come out from a process symbol.
g
En
4. Only one flow line should enter a decision symbol. However, two or three flow
arn
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.
.in
require re-drawing completely.
3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of
flowchart becomes a problem.
ng
4. Cost: For large application the time and cost of flowchart drawing becomes
costly.
3.2.PSEUDO CODE:
eri
Pseudo code consists of short, readable and formally styled English languages
used for explain an algorithm.
ine
It does not include details like variable declaration, subroutines.
It is easier to understand for the programmer or non programmer to understand
the general working of the program, because it is not based on any programming
g
language.
En
.in
... INITIALIZE i=1
ENDFOR FOR (i<=n) DO
ng
PRINT i
i=i+1
ENDFOR
PRINT i
En
i=i+1
ENDWHILE
END
arn
Advantages:
Pseudo is independent of any language; it can be used by most programmers.
It is easy to translate pseudo code into a programming language.
Le
3.3.PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer
.in
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
ng
computer using language which it can understand.
Types of programming language
1. Machine language
2. Assembly language eri
ine
3. High level language
Machine language:
The computer can understand only machine language which uses 0’s and 1’s. In
g
Machine language is the only language which the computer understands. For
executing any program written in any programming language, the conversion to
Le
The machine language program is translation free. Since the conversion time is
saved, the execution of machine language program is extremely fast.
ww
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.
.in
Disadvantage
Machine dependent
ng
The assembly language program which can be executed on the machine depends
on the architecture of that computer.
Hard to learn
eri
It is machine dependent, so the programmer should have the hardware
ine
knowledge to create applications using assembly language.
Less efficient
Execution time of assembly language program is more than machine language
g
program.
En
compilers are used for converting these programs in to machine readable form.
Translating high level language to machine language
w.
The programs that translate high level language in to machine language are called
interpreter or compiler.
ww
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
code and executes it immediately before translating the next statement. When an error
is found the execution of the program is halted and error message is displayed on the
screen.
Advantages
Readability
High level language is closer to natural language so they are easier to learn and
understand
Machine independent
High level language program have the advantage of being portable between
machines.
Easy debugging
.in
Easy to find and correct error in high level language
Disadvantages
ng
Less efficient
The translation process increases the execution time of the program. Programs in
eri
high level language require more memory and take more execution time to execute.
Examples:
Pascal
Python
.in
programs should take to reach to an intended state.
A procedure is a group of statements that can be referred through a procedure call.
Procedures help in the reuse of code. Procedural programming makes the programs
ng
structured and easily traceable for program flow.
Examples:
Hyper talk
MATLAB
eri
ine
Scripting language:
Scripting language are programming languages that control an application.
Scripts can execute independent of any other application. They are mostly embedded in
g
the application that they control and are used to automate frequently executed tasks
En
Examples:
arn
Apple script
VB script
Le
Markup languages:
A markup language is an artificial language that uses annotations to text that
define hoe the text is to be displayed.
w.
Examples:
HTML
ww
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.
Examples:
Lava
Moto
.in
ng
eri
ine
g
En
arn
Le
w.
ww
.in
Algorithm Design Techniques
An algorithm design technique (or “strategy” or “paradigm”) is a general
ng
approach to solving problems algorithmically that is applicable to a variety of
problems from different areas of computing.
eri
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
ine
Methods of Specifying an Algorithm
Pseudocode is a mixture of a natural language and programming language-like
g
constructs. Pseudocode is usually more precise than natural language, and its
En
In the earlier days of computing, the dominant vehicle for specifying algorithms
arn
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.
.in
Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer
programs. Programming an algorithm presents both a peril and an opportunity.
ng
A working program provides an additional opportunity in allowing an empirical
analysis of the underlying algorithm. Such an analysis is based on timing the
eri
program on several inputs and then analysing the results obtained.
iterations.
1. for loop
2. While loop
arn
ENDFOR PRINT i
i=i+1
ENDFOR
ww
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
.in
ng
eri
ine
5.2.Recursions:
g
Main function:
Step1: Start
Le
Step2: Get n
Step3: call factorial(n)
w.
.in
ng
Pseudo code for factorial using recursion: eri
ine
Main function:
g
BEGIN
En
GET n
CALL factorial(n)
PRINT fact
arn
BIN
IF(n==1) THEN
fact=1
w.
RETURN fact
ELSE
ww
RETURN fact=n*factorial(n-1)
More examples:
Write an algorithm to find area of a rectangle
.in
ng
eri
ine
Write an algorithm for Calculating area and circumference of circle
g
END
w.
ww
.in
ng
eri
ine
Write an algorithm for Calculating engineering cutoff
Step 1: Start
g
BEGIN
READ a,b
IF (a>b) THEN
DISPLAY a is greater
ELSE
DISPLAY b is greater
END IF
END
.in
ng
eri
g ine
Step 1: Start
Step 2: get y
Step 3: if(y%4==0) print leap year
arn
READ y
IF (y%4==0) THEN
w.
.in
To check positive or negative number
Step 1: Start
ng
Step 2: get num
Step 3: check if(num>0) print a is positive
Step 4: else num is negative
Step 5: Stop eri
ine
BEGIN
READ num
g
IF (num>0) THEN
En
.in
ELSE
DISPLAY num is odd
END IF
ng
END
eri
g ine
En
arn
Step1: Start
Step2: Get A, B, C
w.
BEGIN
READ a, b, c
IF (a>b) THEN
IF(a>c) THEN
DISPLAY a is greater
ELSE
DISPLAY c is greater
END IF
ELSE
IF(b>c) THEN
.in
DISPLAY b is greater
ELSE
DISPLAY c is greater
ng
END IF
END IF
END
eri
ine
g
En
arn
Le
w.
ww
BEGIN
GET n
IF(n==0) THEN
DISPLAY “ n is zero”
ELSE
IF(n>0) THEN
DISPLAY “n is positive”
ELSE
DISPLAY “n is positive”
END IF
.in
END IF
END
ng
eri
ine
g
En
arn
Le
w.
ww
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
.in
BEGIN
GET n
ng
INITIALIZE i=1
WHILE(i<=n) DO
eri
PRINT i
i=i+1
ENDWHILE
ine
END
g
En
arn
Le
w.
ww
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
.in
BEGIN
ng
GET n
INITIALIZE i=1
eri
WHILE(i<=n) DO
PRINT i
i=i+2
ine
ENDWHILE
END
g
En
arn
Le
w.
ww
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
.in
BEGIN
ng
GET n
INITIALIZE i=2
WHILE(i<=n) DO
PRINT i eri
ine
i=i+2
ENDWHILE
END
g
En
arn
Le
w.
ww
.in
BEGIN
GET n
INITIALIZE i=1
ng
WHILE(i<=n) DO
PRINT i*i
i=i+2
ENDWHILE eri
ine
END
g
En
arn
Le
w.
ww
.in
GET n
INITIALIZE i=1
WHILE(i<=n) DO
ng
PRINT i*i*i
i=i+2
ENDWHILE
END eri
ine
g
En
arn
Le
w.
ww
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
.in
step 9: stop
BEGIN
ng
GET n
INITIALIZE i=1,sum=0
WHILE(i<=n) DO
sum=sum+i eri
ine
i=i+1
ENDWHILE
PRINT sum
g
END
En
arn
Le
w.
ww
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
.in
step 9: stop
BEGIN
ng
GET n
INITIALIZE i=1,fact=1
WHILE(i<=n) DO
fact=fact*i eri
ine
i=i+1
ENDWHILE
PRINT fact
g
END
En
arn
Le
w.
ww
.in
a=l*b 30
print(a)
ng
Area & circumference of circle output
r=eval(input(“enter the radius of circle”)) enter the radius of circle4
a=3.14*r*r
c=2*3.14*r eri
the area of circle 50.24
the circumference of circle
ine
print(“the area of circle”,a) 25.12
print(“the circumference of circle”,c)
Calculate simple interest Output
g
.in
if(b>c):
print(“the greatest no is”,b)
ng
else:
print(“the greatest no is”,c)
eri
Programs on for loop
Print n natural numbers Output
ine
for i in range(1,5,1): 1234
print(i)
g
for i in range(1,10,2):
1 3 5 79
print(i)
arn
for i in range(2,10,2):
2 4 6 8
print(i)
w.
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)
.in
i=i+2 8
10
ng
Print n even numbers Output
i=1 1
eri
while(i<=10): 3
print(i) 5
i=i+2 7
ine
9
Print n squares of numbers Output
g
i=1 1
En
while(i<=5): 4
print(i*i) 9
i=i+1 16
arn
25
Le
print(i*i*i) 27
i=i+1
ww
.in
a=eval(input(“enter a value”)) 6
b=eval(input(“enter b value”)) enter b value
ng
c=a+b 4
print(“the sum is”,c) the sum is 10
eri
add()
a=l*b rectangle 5
print(“the area of rectangle is”,a) the area of rectangle is
area() 100
arn
c=a
a=b
ww
b=c
print("a=",a,"b=",b)
swap()
.in
def reminder(): enter a 6
a=eval(input("enter a")) enter b 3
ng
b=eval(input("enter b")) the reminder is 0
R=a%b enter a 8
eri
print("the reminder is",R) enter b 4
def quotient(): the reminder is 2.0
a=eval(input("enter a"))
ine
b=eval(input("enter b"))
Q=a/b
g
reminder()
quotient()
arn
.in
print("the diff is",c) enter a value 10
def mul(): enter b value 10
ng
a=eval(input("enter a value")) the div is 1
b=eval(input("enter b value"))
c=a*b
print("the mul is",c)
def div():
eri
ine
a=eval(input("enter a value"))
b=eval(input("enter b value"))
g
c=a/b
En
mul()
div()
Le
w.
ww
Part A:
1. What is mean by problem solving?
2. List down the problem solving techniques?
3. Define algorithm?
4. What are the properties of algorithm?
5. List down the equalities of good algorithm?
6. Define statements?
7. Define state?
8. What is called control flow?
9. What is called sequence execution?
.in
10. Define iteration?
11. What is mean by flow chart?
ng
12. List down the basic symbols for drawing flowchart?
13. List down the rules for drawing the flowchart?
14. What are the advantages of flowchart?
15. What are the disadvantages of flowchart?
16. Define pseudo code? eri
ine
17. List down the keywords used in writing pseudo code?
18. Mention the advantages of using pseudo code?
g
26. What are the advantages and disadvantages of machine level language?
27. What is high level programming language and mention its advantages?
w.