GE 8151-Notes
GE 8151-Notes
The problem solving process starts with the problem specifications and ends with a
Qualities of a good algorithm
Correct program.
The following are the primary factors that are often used to judge the quality of the
PROBLEM SOLVING TECHNIQUES algorithms.
Problem solving technique is a set of techniques that helps in providing logic for solving Time – To execute a program, the computer system takes some amount of time. The
a problem. lesser is the time required, the better is the algorithm.
Problem Solving Techniques: Memory – To execute a program, computer system takes some amount of memory
space. The lesser is the memory required, the better is the algorithm.
Problem solving can be expressed in the form of
It is defined as a sequence of instructions that describe a method for solving a problem. Step 1: Start
In other words it is a step by step procedure for solving a problem.
Step 2: Print “Good Morning”
Step 3: Stop
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 1 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 2
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
functions)
All the instructions are executed one after another is called sequence execution.
Algorithms can be constructed from basic building blocks namely, sequence, selection
Example:
and iteration.
Add two numbers:
Statements:
Step 1: Start
Statement is a single action in a computer.
Step 2: get a,b
In a computer statements might include some of the following actions
Step 3: calculate c=a+b
input data-information given to the program
Step 4: Display c
process data-perform operation on a given input
output data-processed result Step 5: Stop
State: Selection:
Transition from one process to another process under specified condition with in a A selection statement causes the program control to be transferred to a specific part of
time is called state. the program based upon the condition.
Control flow: If the conditional test is true, one part of the program will be executed, otherwise it will
execute the other part of the program.
The process of executing the individual statements in a given order is called control
flow.
1. sequence
2. selection
3. iteration
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 3 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 4
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Example
Step 1: Start
Step 6: go to step 4
Example
Step 7: Stop
Write an algorithm to check whether he is eligible to vote?
Functions:
Step 1: Start
Function is a sub program which consists of block of code(set of instructions) that
Step 2: Get age
performs a particular task.
Step 3: if age >= 18 print “Eligible to vote” For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.
Step 4: else print “Not eligible to vote”
Benefits of Using Functions
Step 6: Stop
Reduction in line of code
Iteration: code reuse
Better readability
In some programs, certain set of statements are executed again and again based upon
Information hiding
conditional test. i.e. executed more than one time. This type of execution is called
Easy to debug and test
looping or iteration.
Improved maintainability
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 5 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 6
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Example:
Main function()
Step 1: Start
Step 3: Stop
Step 4: Print c
The purpose of flowchart is making the logic of the program clear in a visual
representation.
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 7 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 8
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
4. Only one flow line should enter a decision symbol. However, two or three flow lines 6. Efficient Program Maintenance: - The maintenance of operating program
may leave the decision symbol. becomes easy with the help of flowchart. It helps the programmer to put efforts more
efficiently on that part.
1. Complex logic: - Sometimes, the program logic is quite complicated. In that case,
5. Only one flow line is used with a terminal symbol.
flowchart becomes complex and clumsy.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in more for explain an algorithm.
It does not include details like variable declaration, subroutines.
effective way.
It is easier to understand for the programmer or non-programmer to understand the
3. Proper documentation: - Program flowcharts serve as a good program
documentation, which is needed for various purposes. general working of the program, because it is not based on any programming
4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems language.
It gives us the sketch of the program before actual coding.
analysis and program development phase.
It is not a machine readable
5. Proper Debugging: - The flowchart helps in debugging process.
Pseudo code can’t be compiled and executed.
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 9 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 10
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
9. FOR, ENDFOR: Another iterative incremented/decremented tested automatically. Example: Print n natural numbers
BEGIN
Syntax for if else:
GET n
IF (condition)THEN
statement INITIALIZE i=1
…
FOR (i<=n) DO
ELSE
statement PRINT i
...
i=i+1
ENDIF
ENDFOR
END
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 11 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 12
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Syntax for While: Converting a pseudo code to programming language is very easy as compared with
statement
Disadvantages:
For a beginner, it is more difficult to follow the logic or write pseudo code as
Example: Print n natural numbers compared to flowchart.
BEGIN
GET n Example:
WHILE(i<=n) DO BEGIN
ADD c=a+b
i=i+1
PRINT c
ENDWHILE
END
END
Advantages:
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 13 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 14
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
A programming language is a set of symbols and rules for instructing a computer to It is hard to find errors in a program written in the machine language.
perform specific tasks. The programmers have to follow all the specified rules before Writhing program in machine language is a time consuming process.
writing program using programming language. The user has to communicate with the
computer using language which it can understand.
Machine dependent: According to architecture used, the computer differs from each
Types of programming language 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.
1. Machine language
Machine language: easier, an assembly language is developed which is logically equivalent to machine
language but it is easier for people to read, write and understand.
The computer can understand only machine language which uses 0’s and 1’s. In
Assembly language is symbolic representation of machine language. Assembly
machine language the different instructions are formed by taking different
languages are symbolic programming language that uses symbolic notation to
combinations of 0’s and 1’s.
represent machine language instructions. They are called low level language because
Advantages: they are so closely related to the machines.
Machine language is the only language which the computer understands. For executing Assembler is the program which translates assembly language instruction in to a
any program written in any programming language, the conversion to machine machine language.
language is necessary. The program written in machine language can be executed
Easy to understand and use.
directly on computer. In this case any conversion process is not required.
The machine language program is translation free. Since the conversion time is saved,
the execution of machine language program is extremely fast.
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 15 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 16
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Disadvantage Compiler:
Machine dependent 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
The assembly language program which can be executed on the machine depends on the
whole program written in high level language and translates it to machine language. If
architecture of that computer.
any error is found it display error message on the screen.
Hard to learn
Interpreter
It is machine dependent, so the programmer should have the hardware knowledge to
Interpreter translates the high level language program in line by line manner. The
create applications using assembly language.
interpreter translates a high level language statement in a source program to a machine
Less efficient 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
Execution time of assembly language program is more than machine language
screen.
program.
Because assembler is needed to convert from assembly language to machine Advantages
language. Readability
High level language is closer to natural language so they are easier to learn and
High level language
understand
High level language contains English words and symbols. The specified rules are to be Machine independent
followed while writing program in high level language. The interpreter or compilers are High level language program have the advantage of being portable between machines.
used for converting these programs in to machine readable form.
Easy debugging
Translating high level language to machine language Easy to find and correct error in high level language
The programs that translate high level language in to machine language are called Disadvantages
The translation process increases the execution time of the program. Programs in high
level language require more memory and take more execution time to execute.
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 17 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 18
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Examples:
Categories of programming languages
Clean
They are divided into following categories:
Haskell
1. Interpreted programming languages
Compiled Programming language:
2. Functional programming languages
3. Compiled programming languages A compiled programming is a programming language whose implementation are
4. Procedural programming languages typically compilers and not interpreters.
5. Scripting programming language
It will produce a machine code from source code.
6. Markup programming language
7. Concurrent programming language Examples:
8. Object oriented programming language
C
C#
An interpreted language is a programming language for which most of its
implementation executes instructions directly, without previously compiling a program JAVA
into machine language instructions. The interpreter executes the program directly Procedural programming language:
translating each statement into a sequence of one or more subroutines already
Procedural (imperative) programming implies specifying the steps that the programs
compiled into machine code.
should take to reach to an intended state.
Examples:
A procedure is a group of statements that can be referred through a procedure call.
Pascal
Procedures help in the reuse of code. Procedural programming makes the programs
Python structured and easily traceable for program flow.
Functional programming language:
Examples:
Functional programming language defines every computation as a mathematical
Hyper talk
evaluation. They focus on the programming languages are bound to mathematical
MATLAB
calculations
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 19 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 20
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Scripting language are programming languages that control an application. Scripts can Object oriented programming is a programming paradigm based on the concept of
execute independent of any other application. They are mostly embedded in the
objects which may contain data in the form of procedures often known as methods.
application that they control and are used to automate frequently executed tasks like
communicating with external program.
Examples:
Examples:
Lava
Apple script
Moto
VB script
ALGORITHMIC PROBLEM SOLVING:
Markup languages:
Algorithmic problem solving is solving problem that require the formulation of an
A markup language is an artificial language that uses annotations to text that define
algorithm for the solution.
hoe the text is to be displayed.
Examples:
HTML
XML
Examples:
Joule
Limbo
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 21 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 22
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
Understanding the Problem First, they provide guidance for designing algorithms for new problems,
It is very important to specify exactly the set of inputs the algorithm needs to handle. Methods of Specifying an Algorithm
A correct algorithm is not one that works most of the time, but one that works
Pseudocode is a mixture of a natural language and programming language-like
correctly for all legitimate inputs.
constructs. Pseudocode is usually more precise than natural language, and its usage
Ascertaining the Capabilities of the Computational Device often yields more succinct algorithm descriptions.
In the earlier days of computing, the dominant vehicle for specifying algorithms was
If the instructions are executed one after another, it is called sequential algorithm.
a flowchart, a method of expressing an algorithm by a collection of connected
If the instructions are executed concurrently, it is called parallel algorithm.
geometric shapes containing descriptions of the algorithm’s steps.
Choosing between Exact and Approximate Problem Solving Programming language can be fed into an electronic computer directly. Instead, it
Deciding a data structure: 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
Data structure plays a vital role in designing and analysis the algorithms.
a finite amount of time.
Some of the algorithm design techniques also depend on the structuring data
A common technique for proving correctness is to use mathematical induction
specifying a problem’s instance
because an algorithm’s iterations provide a natural sequence of steps needed for
Algorithm+ Data structure=programs.
such proofs.
It might be worth mentioning that although tracing the algorithm’s performance for
Algorithm Design Techniques
a few specific inputs can be a very worthwhile activity, it cannot prove the
An algorithm design technique (or “strategy” or “paradigm”) is a general approach algorithm’s correctness conclusively. But in order to show that an algorithm is
to solving problems algorithmically that is applicable to a variety of problems from incorrect, you need just one instance of its input for which the algorithm fails.
different areas of computing.
Learning these techniques is of utmost importance for the following reasons.
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 23 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 24
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
GET n
Coding an Algorithm
INITIALIZE i=1
Most algorithms are destined to be ultimately implemented as computer programs.
FOR (i<=n) DO
Programming an algorithm presents both a peril and an opportunity.
A working program provides an additional opportunity in allowing an empirical
PRINT i
analysis of the underlying algorithm. Such an analysis is based on timing the i=i+1
program on several inputs and then analysing the results obtained. ENDFOR
iterations.
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 25 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 26
IDHAYA ENGINEERING COLLEGE FOR WOMEN IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201. CHINNASALEM – 606 201.
GET n Recursion is a process by which a function calls itself repeatedly until some specified
WHILE(i<=n) DO
PRINT i
Algorithm for factorial of n numbers using recursion:
i=i+1
Main function:
ENDWHILE
Step2: Get n
Step5: Stop
GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 27 GE8151 PROBLEM SOLVING AND PYTHON PROGRAMMING 28
IDHAYA ENGINEERING COLLEGE FOR WOMEN
CHINNASALEM – 606 201.
Main function:
BEGIN
GET n
CALL factorial(n)
PRINT fact
END
IF(n==1) THEN
fact=1
RETURN fact
ELSE
RETURN fact=n*factorial(n-1)
More examples:
END
Step3:Calculate CALCULATE S
SI=(p*n*r)/100 SI=(p*n*r)/100
Step3:calculate CALCULATE
READ a,b
Step 2: get a,b value
IF (a>b) THEN
Step 3: check if(a>b) print a is
DISPLAY a is greater
greater
ELSE
Step 4: else b is greater
DISPLAY b is greater
Step 5: Stop END IF
END
READ y
Step 2: get y
IF (y%4=0) THEN
Step 3: if(y%4=0) print leap year
DISPLAY leap year
Step 4: else print not leap year ELSE
END IF
END
END IF
END
READ n
Step 2: get n
IF (n%2==0) THEN
Step 3: check if(n%2==0) print n is
DISPLAY n is even
even
ELSE
Step 4: else n is odd
DISPLAY n is odd
Step 5: Stop END IF
END
GET n
Step 2: Get n value.
IF(n==0) THEN
Step 3: if (n ==0) print
DISPLAY “ n is zero”
then “Given number is
Zero” ELSE
IF(n>0) THEN
3.1 : Else if (n > 0) then
DISPLAY “n is positive”
Print then “Given number
is +ve” ELSE
DISPLAY “n is positive”
3.2: else Print “Given
number is -ve” END IF
END IF
Step 4: Stop
END