PSP Unit 1
PSP Unit 1
PSP Unit 1
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
❖▪▪ Derived results should be obtained only after the algorithm terminates.
Statements:
Statement is a single action in a computer.
In a computer statements might include some of the following actions
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:
❖▪▪ Function is a sub program which consists of block of code(set of instructions) that
❖▪▪ For complex problems, the problem is been divided into smaller and simpler tasks during
algorithm design.
Example:
Advantages of flowchart:
1. Communication: - Flowcharts are better way of communicating the logic of a system to all
concerned.
2. Effective analysis: - With the help of flowchart, problem can be analyzed in more effective
way.
3. Proper documentation: - Program flowcharts serve as a good program documentation,
which is needed for various purposes.
4. Efficient Coding: - The flowcharts act as a guide or blueprint during the systems analysis
and program development phase.
5. Proper Debugging: - The flowchart helps in debugging process.
6. 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.
❖▪▪ Pseudo code consists of short, readable and formally styled English languages used for
explain an algorithm.
❖▪▪ It is easier to understand for the programmer or non programmer to understand the
general working of the program, because it is not based on any programming language.
Advantages:
❖▪▪ Converting a pseudo code to programming language is very easy as compared with
Disadvantages:
❖▪▪ For a beginner, It is more difficult to follow the logic or write pseudo code as compared to
flowchart.
Example:
Addition of two numbers:
BEGIN
GET a,b
ADD c=a+b
PRINT c
END
PROGRAMMING LANGUAGE
A programming language is a set of symbols and rules for instructing a computer to perform
specific tasks. The programmers have to follow all the specified rules before writing program
using programming language. The user has to communicate with the computer using language
which it can understand.
Machine language:
The computer can understand only machine language which uses 0’s and 1’s. In machine
language the 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 any 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:
Machine dependent: According to architecture used, the computer differs from each 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.
Assembly language:
To overcome the issues in programming language and make the programming process easier,
an assembly language is developed which is logically equivalent to machine language but it is
easier for people to read, write and understand.
are 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 in to 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.
Compiler:
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 whole program written
in high level language and translates it to machine language. If any error is found it display
error message on the screen.
Interpreter
Interpreter translates the high level language program in line by line manner. The interpreter
translates a high level language statement in a source program to a machine 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 screen.
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.
They are divided into following categories:
1. Interpreted programming languages
2. Functional programming languages
3. Compiled programming languages
4. Procedural programming languages
5. Scripting programming language
6. Markup programming language
7. Concurrent programming language
8. Object oriented programming language
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
Markup languages:
A markup language is an artificial language that uses annotations to text that define hoe the
text is to be displayed.
Examples:
HTML
XML
PSEUDO CODE:
❖▪▪ Pseudo code consists of short, readable and formally styled English languages used for
explain an algorithm.
❖▪▪ It is easier to understand for the programmer or non programmer to understand the
general working of the program, because it is not based on any programming language.
Advantages:
1. What is an algorithm?
Algorithm is an ordered sequence of finite, well defined, unambiguous instructions for completing a
task. It is an English-like representation of the logic which is used to solve the problem. It is a step-
by-step procedure for solving a task or a problem. The steps must be ordered, unambiguous and
finite in number.
2. Write an algorithm to find minimum of 3 numbers in a list. ALGORITHM : Find
Minimum of 3 numbers in a list
Step 1: Start
Step 2: Read the three numbers A, B, C
Step 3: Compare A and B.
If A is minimum, go to step 4 else go to step 5. Step 4: Compare A and C.
If A is minimum, output “A is minimum” else output “C is minimum”. Go to step 6.
Step 5: Compare B and C.
If B is minimum, output “B is minimum” else output “C is minimum”.
Step 6: Stop
3. List the building blocks of an algorithm.
The building blocks of an algorithm are
∙€€€€€€€€€€€€€€€€€Statements
∙€€€€€€€€€€€€€€€€€Sequence
∙€€€€€€€€€€€€€€€€€Selection or Conditional
∙€€€€€€€€€€€€€€€€€Functions
4. Define statement. List its types.
Statements are instructions in Python designed as components for algorithmic problem solving,
rather than as one-to-one translations of the underlying machine language instruction set of the
computer.
There are three types of high-level programming language statements Input/output statements make
up one type of statement. An input statement collects a specific value from the user for a variable
within the program. An output statement writes a message or the value of a program variable to the
user’s screen.
5. Write the pseudo code to calculate the sum and product of two numbers and display it.
INITIALIZE variables sum, product, number1, number2 of type real
PRINT “Input two numbers”
READ number1, number2
sum = number1 + number2
PRINT “The sum is “, sum
COMPUTE product = number1 * number2
PRINT “The Product is “, product
END program
6. How does flow of control work?
Control flow (or flow of control) is the order in which individual statements, instructions or function
calls of an imperative program are executed or evaluated. A control flow statement is a statement in
which execution results in a choice being made as to which of two or more paths to follow.
7. What is a function?
Functions are "self-contained" modules of code that accomplish a specific task. Functions usually
"take in" data, process it, and "return" a result. Once a function is written, it can be used over and
over and over again. Functions can be "called" from the inside of other functions.
8. Write the pseudo code to calculate the sum and product displaying the answer on the
monitor screen.
INITIALIZE variables sum, product, number1, number2 of type real
PRINT “Input two numbers”
READ number1, number2 sum = number1 + number2
PRINT “The sum is “, sum
COMPUTE product = number1 * number2
PRINT “The Product is “, product
END program
9. Give the rules for writing Pseudo codes.
∙ Write one statement per line.
Algorithm:
Step 1: Start
Step 2: Initialize the value of result, r=1.
Step 3: Repeat step4 for 4 times
Step 4: calculate r=r*2
Step 5: Print the value of r
Step 6: Stop
Flowchart:
∙In a pseudo code, some terms are commonly used to represent the various actions. For example,
∙The control structures—sequence, selection, and iteration are also used while writing the pseudo
code.
∙The selection constructs—if statement and case statement. In the if-statement, if the condition is
true then the THEN part is executed otherwise the ELSE part is executed. There can be variations of
the if-statement also, like there may not be any ELSE part or there may be nested ifs. The case
statement is used where there are a number of conditions to be checked. In a case statement,
depending on the value of the expression, one of the conditions is true, for which the corresponding
statements are executed. If no match for the expression occurs, then the OTHERS option which is
also the default option, is executed.
6. a) What is flowchart?
Flowchart is a diagrammatic representation of the logic for solving a task. A flowchart is drawn
using boxes of different shapes with lines connecting them to show the flow of control. The purpose
of drawing a flowchart is to make the logic of the program clearer in a visual form.
7. a) Write an algorithm and give the flowchart to find the net salary of an employee.
Algorithm:
Step 1: Start
Step 2 : Read the basic salary
Step 3 : IF the basic is greater than or equal to 4000 ELSE Goto Step 4
Step 3.1 : DA= 0.32 * basic (Dearness Allowance)
Step 3,2 : HRA = 0.15 * basic (House Rent Allowance)
Step 3.3 : CCA = 325 (City Compensatory Allowance)
Step 3.4 : Net Salary basic + DA HRA + CCA
Step 4 : Print the Net Salary
Step 5 : Stop
b) Write an algorithm and give the pseudo code to guess an integer number in a range.
Algorithm:
step 1: Start the program
step 2: Read an 'n' number
step 3: Read an Guess number
step 4: if Guess> n; print "Your Guess too high" Step 5: elif Guess <n ; print "Your Guess
too low" step 6: elif Guess = = n; print "Good job"
Step 7: else print"Nope "
Step :8 Stop the program
Pseudocode:
BEGIN
READ n
READ Guess = 20
IF Guess> n
print"Your Guess too High" elif Guess< n
print "Your Guess too low" elif Guess = = 20
print "Good Job"
ELSE
print"Nope"
PART B:
1. Explain in detail about problem solving techniques?
2. Explain in detail about building blocks of algorithm?
3. Discuss the symbols and rules for drawing flowchart with the example?
4. Explain in detail about programming language?
5. Discuss briefly about algorithmic problem solving?
6. Write algorithm, pseudo code and flow chart for any example?
7. Explain in detail about simple strategies for developing algorithms?