Unit I Part B
Unit I Part B
Unit I Part B
https://www.poriyaan.in/ PROGRAMMING
UNIT – I https://eee.poriyaan.in/
/
in
ALGORITHM Problem Solving and Python Programming
Algorithm is defined as step-by-step description of how to arrive at the
n.
solution of the given problem.
aa
It also defined as finite sequence of explicit instructions that when provided
with a set of input values, produces an output and then terminates.
An algorithm provides a blueprint to writing a program to solve a particular
iy
problem.
It is an effective procedure for solving a problem in a finite number of steps.
Characteristics of Algorithm:
or
.p
Be precise
w
Be unambiguous
It has finite number of inputs
w
Qualities of Algorithm:
//
s:
Different algorithm may perform the same task with different set of
instructions. Some algorithm is considered better than the other in solving the
tp
problem. The algorithm is written in such a way that it optimizes all necessary
conditions.
ht
Advantages:
* It is a step-wise description of a solution to a given problem, which makes it easy to
Understand.
* It uses a definite procedure to solve a problem.
* It is independent on any programming language.
Disadvantages:
* It is not a computer program.
* It is time consuming. (An algorithm needs to be developed first, which is then converted
into
Flowchart and then into a computer program.)
* It is difficult to show branching and looping statements.
/
in
Stpe1: Start
n.
Step2: Read the values of a, b
Step3: Calculate c = a + b
aa
Step4: Print the value of c.
iy
Step5: Stop
or
Step1: Start .p
Step2: Read length and breadth l, b
w
Step3: Calculate area of rectangle, area = l x b
Step4: Print the value of area.Step5: Stop
w
Any algorithm can be constructed from four basic building blocks. These four building blocks
are
//
s:
* Statements
* State Sequence
tp
* Function Iteration
Instructions / Statements:
* Instructions: Commands given to the computer that tell what it has to do.
* Statements: → A section (Segment) of code that represents a command.
→ Each and every line in an algorithm is called statement.
* Types of statement:
State:
* Each and every action in an algorithm is called state.
* An algorithm is a finite number of steps for accomplishing a goal which, given an initial
state, will terminate in a defined end state.
/
in
Control flow:
n.
* It is also called flow of control. It is the order in which instructions or statements are
executed. There are three types of control flow, they are
aa
Sequential control structure → Action
Selection control structure → Decision (or) Branching
Iteration control structure → 𝑅𝑒𝑝𝑒𝑡𝑖𝑡𝑖𝑜𝑛 (𝑜𝑟)𝐿𝑜𝑜𝑝𝑖𝑛𝑔
iy
or
* Sequential control structure (or) sequence structure:
ALGORITHMS
w
Statement -. 2
ALGORITHMS
//
Statement -. 3
s:
ALGORITHMS
tp
→ Example:
ht
→ The choice made depends on whether the condition is true or false. It is also called a
Decision.
/
in
program
n.
to perform in this way: If condition A is true then perform Action X else perform Action Y.
aa
iy
or
.p
→ Example: Algorithm to check the given number is odd or even.
w
Step1: Start
w
Step4: Stop
Count controlled Iteration : It will repeat a set of instructions for a specific number
Of times.
Condition controlled iteration : It will repeat the instructions until a specific
Condition is met.
/
in
→ Example: Algorithm to find factorial of a number
n.
Step1: Start
aa
Step2: Initialize the variable,
iy
fact = 1 and i =1
or
Step3: Read the value of num .p
i=i+1
w
Step5: Print the value of fact
w
fact = fact * i
//
Step6: Stop.
s:
Function:
tp
* A function is a set of instructions that are used to perform a specified task which repeatedly
ht
* The user defined functions are defined by the user, according to the user requirements.
NOTATION;
PSEUDOCODE:
* Pseudo code is another programming analysis tool, which is used for planning program
code.
* Pseudo means imitation or false and code refers to the instructions written in programming
Language.
/
in
* It is also called program Design Language (PDL).
n.
* Pseudo code is somewhat halfway in between English and a programming language.
aa
* Here pseudo code is detailed yet readable and it ensures that actual programming is likely
iy
* Pseudo code keywords:
→ Each statement should express just one action for the compiler.
→ Each set of instructions are written from top to bottom with only one entry and one exit.
Advantages:
* It is easily modified
* It is compact
/
in
* It is language independent
n.
Disadvantages:
aa
* It doesn’t provide visual representation of the program lagic.
iy
* There is no accepted standards for writing pseudo code.
or
* There is no real formatting or syntax rules.
.p
* It is not used to understand the flow of the program control.
w
Example: (i) Pseudo code for finding sum of two numbers
w
BEGIN
w
END
ht
(ii) Pseudo code for finding area of circle with radius ‘r’
BEGIN
area = 3.14 * r * r
END
Flowchart:
* Flowchart is the diagrammatic representation of an algorithm, in which the steps are
Represented in the form of different shapes of boxes and logical flow is indicated by
Interconnecting arrows.
* The boxes represent operations and the arrows represent the data flows (the sequence in
/
.in
Which the operations are implemented.
n
* The purpose of the flowchart is to help the programmer in understanding the logic of the
aa
Program.
iy
* Flowchart symbols:
Symbol
or
Symbol Name Description
.p
w
w
Processing
Used to represent the
processing function of a
program.
/
in
Decision to be made.
n.
aa
Used to join different flow
iy
Connector lines.
or
.p
Used to indicate that the
w
Off-page connecto flowchart continues on the
w
next page.
//w
s:
flowchart symbol.
ht
> Only one flow line should come out from a process symbol.
> Only one flow line should enter a decision symbol, but two or three flow lines should leave
/
the
.in
decision symbol.
a an
iy
or
> Only one flow line is used with a terminal symbol.
.p
w
w
> Within standard symbols write briefly. If necessary use annotation to describe data or
//w
/
* Benefits of flowcharts:
in
> Communication
n.
> Effective Analysis
aa
> Makes logic clear
iy
> Useful in coding
or
> Proper testing and debugging .p
> Appropriate documentation
w
* Limitations of flowcharts:
w
* Complex
w
* Difficult to modify
//
s:
> No update
tp
PROGRAMMING LANGUAGE;
* A computer is the ideal machine to execute computational algorithms.
/
in
> Low level (or) Machine Language
n.
> Assembly Level Language
aa
> High level (or) programming Language.
iy
* Machine Language:
or
> It consists of binary numbers that encode instructions for the computer.
.p
> Every computer has its own machine language.
w
> Example: 101011101
w
100110110
Assembly Language
// w
> Example:
Start
---
---
End
/
in
y = 20
z=x+y
n.
Print “The sum is”, z
aa
Output:
The sum is 30
iy
Types of High Level programming Languages
or
Computer programming languages are used to communicate instructions to a
computer. They are based on certain syntactic and semantic rules, which define the
.p
meaning of each of the programming language constructs. They are divided into the
w
following categories:
Interpreted programming Languages
w
/
in
→ Also called imperative programming languages.
→ A procedure is a group of statements that can be referenced through a procedure
n.
Call.
aa
→ It help in the reuse of code.
→ Example: Hyper Talk, MATLAB.
iy
Scripting Programming Languages:
or
→ It is a programming language that control an application
→ Scripts can execute independentof any other application.
.p
→ Example: PHP, Apple script, VBScript.
w
Mark-up programming Languages:
w
→ It is an artificial language that uses annotations to text that define how the text is to
Be displayed.
w
→ Example: HTML
//
s:
XML, XHTML
tp
/
in
n.
aa
iy
or
.p
w
w
// w
s:
tp
ht
→ Example: Agora, Beta, Lava
/
in
n.
aa
o riy
.p
w
w
//w
s:
tp
ht
/
.in
The next step is to choose how to solve the problem.
Solving the problem exactly is called an exact algorithm.
n
Solving a problem approximately is called an approximation algorithm.
aa
We select an approximation algorithm for three reasons:
(i) There are problems that cannot be solved exactly.
iy
Example: extracting square roots, solving non-line equations.
or
(ii) Available algorithms are slow because of its complexity.
.p
(iii) An approximation algorithm can be part of sophisticated algorithm that
w
Solves a problem exactly.
w
(i) Efficiency:
/
in
(ii) Simplicity:
n.
Simpler algorithms are easier to understand and to program.
aa
(iii) Generality:
iy
There are two issues:
or
(i) Generality of the problem the algorithm solves.
.p
(ii) Set of inputs it accepts.
w
Coding an Algorithm:
Algorithms are implemented as programs.
w
correct.
//
For a complex problem, its algorithm is often divided into smaller units called
modules.
This process of dividing algorithm into modules is called modularization.
The advantage of modularization is :
→ It makes the complex algorithm simpler to design and implement.
Each module can be designed independently.
There are two main approaches to design an algorithm
Top – down approach
Bottom – up approach
Top – down Approach:
It starts by dividing the complex algorithm into one or more modules.
These modules can be further divided into one or more sub – modules.
This process is iterated until the design level of module complexity is achieved.
Bottom – up Approach:
It is the reverse of top-down approach.
It starts designing the most basic or concrete modules and then proceed towards
/
in
designing higher level modules.
In this approach sub –modules are grouped together to form higher level module.
n.
This process is repeated until the design of the complete algorithm is obtained.
aa
Iteration:
Iteration means, executing one or more steps for a number of times.
iy
It can be implemented using while and for loop.
or
These loops execute one or more steps until some condition is true.
Example: Algorithm to print ‘N’ numbers
.p
Step1: Start
w
Step2: Read the value of N
w
Step7: Stop
tp
Recursion:
Recursion is the process of calling the same function itself again until some condition
ht
is satisfied.
Example: Algorithm to find factorial of a number.
Step1: Start
Step2: Read the value of N
Step3: Call function, factorial (N)
Step4: Stop
Step1: Initialize f = 1
ILLUSTRATIVE PROBLEMS
Problem Description:
/
in
Minimum in a list of elements can be achieved in different ways.
n.
One way is to sort the list of elements in ascending order and get the first element as
minimum.
aa
Another method, is to compare each element with other.
Assume the first element as the minimum element and start comparing with the next
iy
(second) element.
or
If the next element is smaller than assume that element as the minimum, and keep
repeating the process till the last element.
.p
Finally obtain the minimum element.
w
w
ALGORITHM PSEUDOCODE
w
Step1 : Start BE GI N
://
Step2 : Read the limit of list as n READ the limit of list as ‘r’
Step3: Read the ‘n’ elements of list a (). READ the ‘n’ elements
s
Flowchart:
/
in
n.
aa
iy
or
2. Insert a card in a list of sorted cards
.p
w
Problem Description:
w
Playing cards is one of the techniques of sorting. To insert a card in a list of sorted
//w
Insert a new card in the appropriate position by comparing each element’s value with
s:
/
in
Step5: Set new card to the current index. SET new card to the
Step6 : Display all the card elements of a Current index.
n.
new list. DISPLAY all the card
aa
Step7: Stop. Elements of a new list
END.
iy
or
.p
w
w
// w
s:
tp
ht
/
in
n.
aa
iy
or
.p
w
w
//w
s:
tp
ht
Problem Description
/
Step4.1: Print “your guess too high”. PRINT “your guess too high”.
in
Step5: Elif (guess <n) then ELIF (guess < n ) THEN
n.
Step5.1: Print “your guess too low”. PRINT “your guess too low”.
aa
Step6 : Elif (guess = = n) then ELIF (guess = = n) THEN
Step6.1: Print “your guess is correct”. PRINT “your guess is correct”.
iy
Step7 : Else print “your guess is wrong” ELSE PRINT “your guess is wrong”.
Step8 : Stop END
or
.p
w
w
//w
s:
tp
ht
4. Towers of Hanoi
Problem Description:
Tower of Hanoi is a mathematical puzzle with three rods and ‘n’ number of different
sized disks, each disk has a hole in centers, allowing it to be stacked around any of
the poles.
/
Initially the disks are stacked on the left most pole in the order of decreasing size,
in
i.e., the largest disk at the bottom and the smallest on the top.
n.
The aim (objective) of this game is to move the disks from the left mast pole (rod) to
the right most poles, without ever placing a larger disk on the top of the smaller disk.
aa
Rules for Towers of Hanoi:
Only one disk may be moved at a time.
iy
Only the top most disk can be moved
or
Only the smaller disk can be placed above the larger disk.
.p
Solution: Let us consider n =3 and three rods (pegs) named A, B and C.
w
w
//w
s:
tp
ht
/
in
n.
aa
iy
or
.p
w
w
//w
s:
tp
ht
ALGORITHM PSEUDOCODE
Step1 : Start PROCEDURE
Step2 : Define the function tower() TOWER (N, A, C, B)
Step3: Read the number of disk N. BEGIN
Step4 : Initialize the pegs IF N = = 1 THEN
A = Source, B = aux, Move disk from A to C
C = dest. ELSE
Step5 : Call the function TOWER (N-1, A, B,C)
tower (N, A, C, B) move disk from A to C
Step6 : Stop TOWER (N-1, B,C,A)
User Defined Function: ENDIF
Step1 : Define the function END
tower ( N, A, C, B) END PROCEDURE
Step2 : if N = = 1 then move disk from A to
Step3 : Else
Step3.1 : tower (N-1, A, B, C)
/
in
Step3.2 : move disk from A to C // N → no. of disks.
Step3.3 : tower (N-1, B, C, A) 3 pegs → A, B, C
n.
aa
iy
Source aun Dest
or
.p
w
w
// w
s:
tp
ht
Problem Solving and Python Programming (GE3151) – Reg 2021
Unit I: Computational Thinking and Problem Solving
Computational Thinking and Problem Solving | Fundamentals of Computing | Identification of Computational Problems |
Algorithms | Building Blocks | Notation | Algorithmic Problem Solving | Simple Strategies for Developing Algorithms |
Illustrative Problems | Anna University Two Marks Questions & Answers | Multiple Choice Questions and Answers
https://www.poriyaan.in/