PSPP Unit 1
PSPP Unit 1
Definition
Computing is any type of calculation that includes both arithmetical and non-
arithmetical steps and follows a well defined model. It is the process of using computer
technology to complete a given goal-oriented task.
Computational thinking
Computational thinking is the process of approaching a problem in a systematic manner.
Creating and expressing a solution such that it can be carried out by a computer.
It is not necessary that one need to be a computer scientist to think like a computer
scientist.
Computational thinking is sometimes referred to as algorithmic thinking, but computational
thinking is the more comprehensive term.
Computational problems are one of the main objects of study in theoretical computer
science.
Computational thinking gives us the basis to organize our thought process, and a language
to communicate such organization to others including a computer.
Computational problem solving does not simply involve the act of computer programming.
The organizing part has relation to the study of algorithms, the language part to the design
of programming languages and systems and the last communication part to the activity of
coding in a given programming language.
Computational thinking provides the foundation for learning how to translate any real-
world problem into a computer program or system through the use of algorithms,
languages and coding.
Computational problem is a problem that can be solved step-by step with a computer.
These problems usually have well-defined input, constraints and conditions that the output
must satisfy. Ex: Given a +ve integer n, find a nontrivial prime factor of n.
Abstraction: Removing the aspects of a problem that are not needed for its solution.
Example: When we are driving car, we are only concerned about driving the car like
start/stop the car, accelerate/break, etc. We are not concerned about how the actual
start/stop mechanism or accelerate or break mechanism works internally
Decomposition: Breaking a big problem down into smaller and more manageable
sub problems
Pattern Recognition: Analyzing any kind of repeated elements or sequences in the
problem. Example: photo recognition in photos
2. Algorithmic Expression
Finding an algorithm which is a precise sequence of steps that solves the problem
using appropriate data representations. This step is called algorithmic thinking.
Final stage is to create the actual solution and systematically evaluate it to determine
its correctness and efficiency. This step also involves seeing if the solution can be
generalized via automation or extension to other kinds of problems
Types of computational problems are
1. Decision problems
2. Search problems
3. Counting problems
4. Optimization problems
Problem solving technique is a set of techniques that helps in providing logic for solving a problem.
Problem solving can be expressed in the form of
1. Algorithms.
2. Flowcharts.
3. Pseudo codes.
4. Programs
1.3 ALGORITHM
Algorithm 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. The steps must be
ordered, unambiguous and finite in number
ii) State: Transition from one process to another process under specified condition within a
time is called state.
iii) 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; a) Sequence, b) Selection & c)
Iteration.
a) Sequence: All the instructions are executed one after another is called sequence of
execution.
Example:
Add two numbers:
Step 1: Start
Step 2: get a,b
Step 3: calculate c=a+b
Step 4: Print c
Step 5: Stop
Flow Chart
C. 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: while (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
iv) Functions
Function is a sub program which consists of set of instructions or code that performs a
particular task.
For complex problems, the problem is been divided into smaller and simpler tasks
during algorithm design.
Main function()
Step 1: Start
Step 2: Call the function add()
Step 3: Stop
1.5.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 fol. Following Table shows the symbols and its description used in flowchart
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.
Advantages of flowchart:
1. Easy Communication: - Flowcharts are better way of communicating the logic of
a system to all involved.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in
more effective way.
3. Efficient Coding: - The flowcharts act as a guide or blueprint during the
systems analysis and program development phase.
4. Efficient Program Maintenance: - The maintenance of operating program becomes
easy with the help of flowchart. It helps the programmer to put efforts more efficiently
on that part.
5. Documentation: - Program flowcharts serve as a good program documentation, which
is needed for various purposes.
6. Debugging: - The flowchart helps in debugging process.
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.
It can be easily modified as compared to flowchart.
Converting a pseudo code to programming language is very easy as compared with
converting a flowchart to programming language.
Disadvantages
It does not provide visual representation of the program’s logic.
There are no accepted standards for writing pseudo codes.
It cannot be compiled nor executed.
For a beginner, it is more difficult to follow the logic or write pseudo code as compared
to flowchart.
The computer can understand only machine language which uses 0’s and 1’s.
In machine language, different instructions are formed by taking different combinations
of 0’s and 1’s.
Advantages
Translation free: Machine language is the only language which the computer
understands. For executing any program written in any programming language, the
conversion to machine language is necessary.
The program written in machine language can be executed directly on computer. In this
case conversion process is not required.
High speed: The machine language program is translation free. Since the conversion
time is saved, the execution of machine language program is extremely fast.
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.
2. Assembly language
Assembly language is logically equivalent to machine language but it is easier for people
to read, write and understand.
Assembly language is symbolic representation of machine language.
Assembly language is symbolic programming language that uses symbolic notation to
represent machine language instructions. They are called low level language because
they are so closely related to the machines.
Assembler: Assembler is the program which translates assembly language instruction into a
machine language.
Disadvantage
Machine dependent: The assembly language program which can be executed on the
machine depends on the architecture of that computer.
Hard to learn: It is machine dependent, so the programmer should have the hardware
knowledge to create applications using assembly language.
Less efficient: Execution time of assembly language program is more than machine
language program because assembler is needed to convert from assembly language to
machine language.
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: Easy to find and correct error in high level language
Disadvantages
Less efficient
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.
5 Scripting language
Scripting language are programming languages that control an application. Scripts can
execute independent of any other application. They are mostly embedded in the application
that they control and are used to automate frequently executed tasks like communicating with
external program.
Examples: Apple script, VB script, Java script
6 Markup languages
A markup language is an artificial language that uses annotations to text that define how
the text is to be displayed.
Examples: HTML, XML
Design an Algorithm
Prove correctness
This means solving problems that require the formulation of an algorithm for the
solution.
Algorithms are procedural solutions to problems.
Good algorithms take less time and memory to perform a task.
Problems to which algorithmic solutions may be required fall into 4 basic classes:
Designing an Algorithm
Analyzing an Algorithm
Next to correctness, the desirable characteristic features to be analyzed are
• Efficiency
• Simplicity
• Generality
The two kinds of algorithm efficiency are
• time efficiency-indicating how fast the algorithm runs
• space efficiency-indicating how much extra memory it uses
Coding an Algorithm
• Most of algorithms are implemented as computer programs.
• The validity of programs is still established by testing.
• A working program provides an additional opportunity in allowing an
experimental analysis of the basic algorithm.
• Such an analysis is based on timing the program on several inputs and then
analyzing the results obtained.
• A good algorithm is a result of repeated effort and rework.
• The important is use of algorithmic problem solving is the question of whether all
problems can be solved by an algorithm or not.
1.7.1 ITERATION
Iteration is the act of repeating a process, either to generate an unbounded sequence of
outcomes, or with the aim of approaching a desired goal, target or result.
Each repetition of the process is also called iteration
Result of each iteration as the starting point for the next iteration.
When a sequence of instructions is executed in a repeated manner, it is called a loop.
They are used in programming to repeat sections of code.
Examples where iteration is important
• Calculating f(x) for 100 values of x
• Computing a result iteratively until a certain accuracy is reached, such as in
evaluating a series like x = x+1+2+3+ .... n
• Printing a table of results
• Reading large quantities of data from a data file
• Working with arrays
Iterative Structures
There are three types of iterative structures
i. for loop: Loop for a specific number of iterations based on an index variable
ii. while loop: Continue looping while a condition is true - Pre-test on the condition,
so loop is executed 0 or more times.
iii. do-while loop:- Continue looping while a condition is true - Post-test on the
condition, so loop is executed 1 or more times.
1.7.2 RECURSION
The process of calling a function by itself is called recursion and the function which
calls itself is called recursive function.
Recursion is used to solve various mathematical problems by dividing it into smaller
problems. This method of solving a problem is called Divide and Conquer.
In programming, it is used to divide complex problem into simpler ones and solving
them individually.
Advantages of recursion
Recursive functions make the code look clean and elegant.
A complex task can be broken down into simpler sub-problems using recursion.
Sequence generation is easier with recursion than using some nested iteration.
Disadvantages of recursion
Sometimes the logic behind recursion is hard to follow through.
Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
Recursive functions are hard to debug.
Main function:
BEGIN
GET n
CALL factorial(n)
PRINT fact
END
ILLUSTRATIVE PROGRAMS
1. To find a minimum value in a given list
To find the minimum element, take the first element and compare its value against
the values of all other elements. Once we find a smaller element, we continue the
comparisons with its value. Finally we find the minimum
Example : Let the list of elements be 2,5,8,4,1,3,4,9,6
Diagram
Algorithm
Step 1: Start
Step 2: Set Initial value list[10], i=0,n=0,min=0
Step 3: Input n;
Step 4: Loop: Read i from 0 to n-1
Step 5: Input list[i]
Step 6: End Loop
Step 7: Set Min=list[0]
Step 8: Loop: Read i from 1 to n-1
Step 9: if list[i]<min then
Step 10: Set min=list[i]
Step 11: End Loop
Step 12: Print min
Step 13: Stop.
Pseudo code
BEGIN
GET n
INITIALIZE list[10],i=0;
WHILE(i<n-1) DO
GET list[i]
END WHILE
Min=list[0]
WHILE(i+1<n-1) DO
IF list[i]<min
min=list[i]
END WHILE
PRINT min
END
Flow chart
2. Insert a card in a list of sorted cards
To insert a card is set of list of sorted cards, first find the position of to insert. Then shift
the other cards and insert the card
Example : Let the list contains 2,3,7,8 10, 13. Let the value to insert is 5, then
position to insert is 2
Diagram :
Algorithm
Step 1: Start
Step2: Read all nos in an array a[]
Step 3: Read value to be inserted Flow Chart
Step 4: if value<a[i] then assign i to pos
Step 5: Return pos
Step 6: From n-1 to pos move one position down
Step 7: Store the value in pos location
Step 8: Stop
Pseudo code
START
GET a[]
INT n, value, pos
FOR I IN 0 to N-1
IF(value<a[i])
pos=i
RETURN pos
BREAK
END IF
END FOR
FOR i IN n-1 to pos, i--
a[i+1]=a[i]
END FOR
a[pos]=value
STOP
3. To guess an integer number in a range problem.
Procedure
To guess an integer, first set a number in the range as hidden.
Get the guess from the user and check if they are same.
Print according to the result
Algorithm
Step 1: Start
Step 2: Declare hidden, guess, range =1 to 100
Step 3: compute hidden=choose a random value in a range
Step 4: Read guess
Step 5: If guess=hidden, then
Step 6: print “Guess is hit”
Step 7: Else
Step 8: Print “Guess is not hit”
Step 9: Print hidden
Step 10: Stop
Pseudo code
BEGIN
GET a random number
Hidden = random number
Read Guess
IF (value=guess)
Print(“ Success. Guess is hit”)
Else
Print(“ not success. Guess is not hit”)
STOP
Flowchart
4. Tower of Hanoi problem
Procedure: The mission is to move all the disks to some another tower without violating the
sequence of arrangement. A few rules to be followed for tower of Hanoi are
Rules
Only one disk can be moved among the towers at any given time
Only the top disk can be removed
No large disk can sit over a small disk
Diagram
Algorithm
Here we mark 3 towers with names, source and destination and aux.
Pseudo Code
START
Procedure Hanoi(disk, source, dest, aux)
IF disk==1 THEN
move disk from source to dest
ELSE
Hanoi(disk-1, source, aux, dest,)
move disk from source to dest
Hanoi(disk-1, aux, de s t , s o u r c e )
ENDIF
END Procedure
Stop
Flow Chart
Algorithm Flowchart
Step 1: Start
Step 2: get l,b values
Step 3: Calculate A=l*b
Step 4: Display A
Step 5: Stop
Pseudo Code
Algorithm Flowchart
Step 1: Start
Step 2: get r value
Step 3: Calculate A=3.14*r*r
Step 4: Calculate C=2.3.14*r
Step 5: Display A,C
Step 6: Stop
Pseudo Code
BEGIN
READ r
CALCULATE A and C
A=3.14*r*r
C=2*3.14*r
DISPLAY A, C
END
Pseudo Code
BEGIN
READ p, n, r
CALCULATE SI=(p*n*r)/100
DISPLAY SI
END
4 Write an algorithm to Calculate engineering cutoff
Algorithm Flowchart
Step 1: Start
Step2: get P,C,M value
Step3: calculate Cutoff= P/4+C/4+M/2
Step 4: Display Cutoff
Step 5: Stop
Pseudo Code
BEGIN
READ P,C,M
CALCULATE Cutoff= P/4+C/4+M/2
DISPLAY Cutoff
END
Pseudo Code
BEGIN
READ a,b
IF (a>b) THEN
DISPLAY “A is greater”
ELSE
DISPLAY “B is greater”
END IF
END
PseudoCode
BEGIN
READ n
IF (n>0) THEN
DISPLAY “n is positive”
ELSE
DISPLAY “n is negative”
END IF
END
ALGORITHM FLOWCHART
Step 1: Start
Step 2: get n value.
Step 3: initialize i=1
Step 4: if (i<=n) print i value
Step 5: else go to step 8
step 6 : increment i value by 1
Step 7: go to step 4
Step 8: Stop
PSEUDO CODE
BEGIN
GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i
i=i+1
END WHILE
END
Algorithm Flowchart
Step 1: start
step 2: get n value
step 3: set initial value i=2
step 4: check if(i<=n) print i value
step 5: else goto step 8
step 6: increment i value by 2
step 7: goto step 4
step 8: stop
BEGIN GET n
INITIALIZE i=2
WHILE(i<=n) DO
PRINT i
i=i+2
END WHILE
END
26 PANIMALAR ENGINEERING
COLLEGE CHENNAI CITY CAMPUS – Python R 2021
13 Write an algorithm to print squares of the given N numbers
Algorithm Flowchart
Step 1: start
step 2: get n value
step 3: set initial value i=1
step 4: check if(i<=n) print i*i value
step 5: else goto step8
step 6: increment i value by 1
step 7: goto step 4
step 8: stop
Pseudo code
BEGIN GET n
INITIALIZE i=1
WHILE(i<=n) DO
PRINT i*i
i=i+1
ENDWHILE
END
Pseudo code
BEGIN
GET n
INITIALIZE i=1,sum=0
WHILE(i<=n) DO
sum=sum+i
i=i+1
ENDWHILE
PRINT sum
END
Pseudo Code
BEGIN
GET n
INITIALIZE i=1,fact=1
WHILE(i<=n) DO
fact=fact*i
i=i+1
ENDWHILE
PRINT fact
END