Unit 1 Notes
Unit 1 Notes
[Problem solving aspects: Algorithms Pseudo code, Flow chart – Steps in Problem
Solving – Simple strategies for developing algorithms (iteration and recursion) –
Programming methodologies – Illustrative examples: Exchanging the values –Counting –
Finding minimum in a list – Factorial computation – Fibonacci sequence]
Steps in Problem Solving
A computer cannot solve a problem on its own. One has to provide step by step solutions of
the problem to the computer. In fact, the task of problem solving is not that of the computer. It is the
programmer who has to write down the solution to the problem in terms of simple operations which
the computer can understand and execute.
In order to solve a problem by the computer, one has to pass though certain stages or steps.
They are
1. Understanding the problem
2. Analyzing the problem
3. Developing the solution
4. Coding and implementation.
1. Understanding the problem: Here we try to understand the problem to be solved in
totally. Before with the next stage or step, we should be absolutely sure about the objectives
of the given problem.
2. Analyzing the problem: After understanding thoroughly the problem to be solved,
we look different ways of solving the problem and evaluate each of these methods. The idea
here is to search an appropriate solution to the problem under consideration. The end result of this
stage is a broad overview of the sequence of operations that are to be carries out to solve the given
problem.
3. Developing the solution: Here the overview of the sequence of operations that
was the result of analysis stage is expanded to form a detailed step by step solution to the
problem under consideration.
4. Coding and implementation: The last stage of the problem solving is the
conversion of the detailed sequence of operations in to a language that the computer can
understand. Here each step is converted to its equivalent instruction or instructions in the
computer language that has been chosen for the implantation.
1
Algorithms
An algorithm is a step by step procedure of solving a problem. The word
“algorithm” is derived from the name of the 9 th century Persian mathematician Al-
Khwarizmi.
An algorithm properties:
5. Input/output: Each algorithm must take zero or more quantities as input data and give out
one or more output values.
2
order to lead to output from the input are to be identified in an orderly manner.
Step 4: Processing Definiteness: The instructions composing the algorithm must be clear
and there should not be any ambiguity in them.
Step 5: Processing Finiteness: If we go through the algorithm, then for all cases, the
algorithm should terminate after a finite number of steps.
Step 6: Possessing Effectiveness: The instructions in the algorithm must be sufficiently
basic and in practice they can be carries out easily.
List the data needed to solve the problem (input) and know what is the end
result (output).
Describe the various step to process the input to get the desired output.
Break down the complex processes into simpler statements.
sets.
Step1: Start
Step 4: Print c
Step 5: Stop.
The above algorithm is to add two numbers a and b. The numbers are the input
provided by the user .After adding the result is stored in a variable c and it is printed.
The basic building blocks of an algorithm are Statements, Control flow and Functions.
3
1. Statements:
2 . Control flow:
Sequence
Selection
Iteration
(a) Sequence
Step1: Start
4
Step6: Print d
Step7: Stop
The above algorithm finds the average of three numbers a, b and c. The numbers are
the input provided by the user .After adding the total is divided by 3 and the result is
stored in a variable d and it is printed.
(b) Selection
If the condition is true, one part of the program will be executed, otherwise the other
part of the program will be executed.
Step1: Start
Step 6: ENDIF
Step 7: Stop
The above algorithm is used to find the Largest of two numbers a and b. The numbers
are the input provided by the user .The number a and b are compared, If a is larger
Print “A is Large” or if b is larger print “B is Large”.
(c)Iteration
Step 1: Start
Step 6: Go to step 4
Step 7: Stop
The above algorithm is for printing first n natural numbers .The user provides
the input. The first value, i is initialized. A loop is initialized. The first value is
printed and the number is incremented. The i value is checked with n, the user input.
The numbers are printed until i becomes greater than n.
3. Functions
Function is a sub program which consists of block of instructions that performs
a particular task. For complex problems, the problem is been divided into smaller and
simpler tasks during algorithm design.
Benefits of Using Functions
6
Step 1: Start
Step 2: Call the function add ()
Step 3: Stop
The above algorithm is to call the function add. This function is called as Main
function or calling function.
Subfunction add ()
The above algorithm is called as the called function which is to add two numbers a
and b. The numbers are the input provided by the user .After adding the result is
stored in a variable c and it is printed.
Pseudo Code
Pseudo –False.
Code- Set of Instructions.
Pseudo code means a short, readable set of instructions written in English to explain
an algorithm.
7
Common keywords used in writing a Pseudo code
Comment: //
Start: BEGIN
Stop: END
Input: INPUT, GET, READ
Calculate: COMPUTE, CALCULATE, ADD, SUBTRACT, INITIALIZE
Output: OUTPUT, PRINT, DISPLAY
Selection: IF, ELSE, ENDIF
Iteration: WHILE, ENDWHILE, FOR, ENDFOR
BEGIN
GET a, b
ADD c=a+b
PRINT c
END
Advantages:
Disadvantages:
It is not visual.
For a beginner, it is more difficult to follow the logic or write pseudo code
as compared to flowchart.
Flow Chart
Flow chart is defined as the graphical or diagrammatical representation of the
process of solving a problem. It represents the steps of a process in a sequential order.
Rules:
Only one flow line should come out from a process symbol
Only one flow line should enter a decision symbol. However, two or three
flow lines may leave the decision symbol
Only one flow line is used with a terminal symbol.
9
Fig 1.1 Symbols used in Flow chart
10
Advantages:
Flowcharts help in communicating the logic of a program in a better way.
Disadvantages:
The flowcharts are complex and clumsy when the program is large or
Complicated.
The cost and time taken to draw a flowchart for larger applications are
expensive.
Example: Add two numbers
11
The above flowchart is used for adding two numbers Number1 and Number2.The numbers are the
input provided by the user .After adding, the result is stored in the variable Sum and is printed.
The above flowchart is used for printing all odd numbers up to 100.
The first value, i is initialized as zero. A loop is initialized. Starting from i=0, each
value is divided by 2 and the remainder is captured .This operation is called modulo
of a number.
The modulo of first value is calculated if the result is not equal to zero then
12
the number is printed and the number is incremented. The i value is checked with the
limit, 100. The numbers are printed until the value is less than the input value.
The above flowchart is to find the Largest of two numbers NO1 and
NO2.The numbers are the input provided by the user .The number NO1 and NO2
are compared, If NO1 is larger, the number NO1 is printed or if NO2 is larger, the
number NO2 is printed.
13
Fig 1.5a Flowchart of a calling function
14
The flowchart 1.5a is for the calling function. This function is also called as
Main function . The flowchart1.5b is called as the called function which is to add two
numbers a and b.The numbers are the input provided by the user .After adding the
result is stored in a variable c and it is printed.
Types of Flowcharts
Process Flowchart
Data Flowchart
Business Process Modeling Diagram
15
Simple Strategies for Developing Algorithms (Iteration, Recursion)
Iterations:
1. For loop
2. While loop
END
16
Step 7: Stop ENDWHILE
END
The above flowchart is to print all natural numbers up to n .The user provides
the input value n. The first value, i is initialized as one. A loop is initialized. The i
value is checked with n, the user input. The numbers are printed until the value is less
than the user input value. When the condition becomes false the loop terminates.
17
Recursion:
Example: Factorial
RETURN fact=n*factorial(n-1)
18
Fig 1.9 a) Flowchart of a calling function b) Flowchart of a Sub function to
find factorial of a given number
The flowchart 1.9a is to call the function factorial. This function is called as
Main function or calling function. The flowchart1.5b is called as the called function
which is to find the factorial of the number passed from the main function. The loop
exists until the n value becomes one. Factorial is a recursive function where the
function itself is called again and again.
19
Property Iteration Recursion
Used when time complexity Used when code size needs to
Usage needs to be balanced against an be small, and time complexity
expanded code size. is not an issue.
Code Size Larger Code Size. Smaller code size
Relatively lower time
Time Complexity
complexity Very high time complexity.
20