Problem Solving Unit 1
Problem Solving Unit 1
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.
ALGORITHM
It is defined as a sequence of instructions that describe a method for
solving a problem. In other words it is a step by step procedure for solving
a problem.
Properties of Algorithms
v Should be written in simple English
v Each and every instruction should be precise and unambiguous.
v Instructions in an algorithm should not be repeated infinitely.
v Algorithm should conclude after a finite number of steps.
v Should have an end point
v 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.
Example
Write an algorithm to print „Good Morning”
Step 1: Start
Step 2: Print “Good Morning”
Step 3: Stop
Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
Ø input data-information given to the program
Ø process data-perform operation on a given input
Ø output data-processed result
State:
Transition from one process to another process under specified condition
with in a time is called state.
Control flow:
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
2. selection
3. iteration
Sequence:
All the instructions are executed one after another is called sequence
execution.
Example:
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.
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”
Step 4: else print “Not eligible to vote”
Step 6: Stop
Iteration:
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.
Example
Write an algorithm to print all natural numbers up to n
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 7
Step 5: Print i value and increment i value by 1
Step 6: go to step 4
Step 7: Stop
Functions:
v Function is a sub program which consists of block of code(set of
instructions) that performs a particular task.
v For complex problems, the problem is been divided into smaller and
simpler tasks during algorithm design.
Example: