Unit I - Notes PDF
Unit I - Notes PDF
Unit I - Notes PDF
UNIT I
ALGORITHMIC PROBLEM SOLVING
Algorithms, building blocks of algorithms (statements, state, control flow, functions), notation (pseudo
code, flow chart, programming language), algorithmic problem solving, simple strategies for developing
algorithms (iteration, recursion). Illustrative problems: find minimum in a list, insert a card in a list of
sorted cards, guess an integer number in a range, Towers of Hanoi.
DESIGN TOOLS:
Program – is a set of instructions that tells the computer as how to solve a particular problem
Design tools are used to design a blueprint of the program
Various design tools are
o Algorithms
o Flowchart
o Pseudocodes
ALGORITHMS:
An algorithm is a well-defined computational procedure consisting of a set of instructions that
takes some value as input, and produces some value as output.
It is a blueprint of a program that contains sequence of instructions
It gives the logic of the program in a finite, step – by – step fashion
Use – Software Reuse
Characteristics of Algorithm:
Precise – Each instruction should be precise
Unique – Each instruction should be unique
Finite – No instruction must be repeated infinitely
Effective – Algorithm should be effective in terms of memory and time
Input – Must receive an input
Output – Must produce a output
Generality – Should work for various set of inputs
Qualities of an Algorithm:
Qualities of an Algorithm are determined by
Accuracy – should produce accurate results
Time – should consume less time to run
Space – should consume less memory space
RECURSION ITERATION
It a method in which the function calls itself It is a method in which certain part of code is
again and again until the condition is satisfied repeated using loops
Popular examples of recursion are Towers of Examples of iteration include Printing first n
Hanoi, Factorial of a number, Eight Queens Natural numbers, Fibonacci series, Sum of
problem, etc digits, etc.
4. FUNCTIONS
They are self contained modules of code that takes input and returns a output
It can be called any number of times. That is, when a function is written it can be called any
number of times
The functions change the flow of control of a program
When a function is called, the current flow is switched to the function. Once the function gets
executed, the flow returns back to the next line of code
Have two parameters – Actual and Formal parameter
o Actual Parameter – the list of parameters actually passed during the function call
o Formal Parameter – acts as a placeholder for the actual parameters during function
definition
Advantages – Reduce memory by providing code reusability
Procedural programming languages like C, Object Oriented programming languages like C++,
FORTRAN, etc., follow this approach JAVA, etc., follow this approach
It provides a way to store and organize the data in a computer for efficient use
Few examples of data structure are Lists, Arrays, Files, Dictionaries, Sets, etc
Algorithm design techniques
A problem can be solved in various techniques
The technique that gives accurate solution by consuming less time and memory is chosen finally
Various algorithm design techniques exist. Examples: Brute force method, Divide and Conquer
method, Greedy method, Dynamic programming, etc
Hence an appropriate algorithm design technique has to be devised out for the problem
Methods of Specifying the algorithm
Once the algorithm is designed, it can be specified either by using any natural language or with
the help of pseudocode / flowchart
This specification must be in step by step fashion
Proving the algorithm correctness
After writing the algorithm, it has to be validated
Validation ensures that the algorithm works correctly for all sets of possible inputs in a finite
amount of time irrespective of the programming language
Analysing the Performance of an algorithm
An algorithm is analysed for its performance based on its consumption of CPU time and memory
space
Time efficiency refers to how fast the algorithm can execute while Space efficiency measures
how small memory the algorithm needs for execution
ADVANTAGES:
Makes the problem representation easier
Follows a finite procedure
Even a non – programmer can understand the algorithm
LIMITATIONS:
Time Consuming to write algorithm
It does not reduce the difficulties of writing code
FLOWCHARTS:
Flowchart is a graphical or symbolic representation of a process.
Users can visualize the processing / logic of the problem
Provides better and easy understanding
Follows top – down approach in problem solving
Each step of the program is depicted using different symbols
The symbols are connected using arrows to depict the logic flow
Symbols used in flowchart are as follows:
EXAMPLES:
1. Flowchart to calculate sum of first 10 natural numbers
ADVANTAGES:
Good communication tools
Used for documentation
Act as guide or blueprint
Helps programmers to identify and correct mistakes
Aids the programmers in knowing the sequence
LIMITATIONS:
Laborious & Time Consuming
Difficult to represent a complex task
Small change in program may lead to complete redrawing of flowchart
No specific standards
PSEUDOCODES:
It is a compact and informal high-level description of an algorithm
It uses the structural conventions of a programming language
An ideal pseudocode must be
o Complete - describing the entire logic of the algorithm
o Human readable
o Easy to translate into program
ADVANTAGES:
Makes reviewing an easier task
Modifications can be easily done
Easier to maintain than other documenting standards
Reduced complexity
No special symbols are used
Very easy to translate pseudocode to high level language
DISADVANTAGES:
There is no visual representation
No specific standards are followed to write a pseudocode
M.Kiruthika [AP/CSE] Page 16
GE8151 – PROBLEM SOLVING AND PYTHON PROGRAMMING UNIT I
EXAMPLES:
1. Write a pseudocode to calculate the price of the product.
.
2. Write a pseudocode to print the first n natural numbers.
Solution
1. Read n
2. Initialise i to 1
3. While i<=n, do:
Write i.
Increment i
End while
PROGRAMMING LANGUAGES:
Once the algorithm is designed, it is essential to code the algorithms. The language used for
coding is called programming language
Program is a set of instructions that instruct the computer to perform a specified task and a
program may be written using any programming language
There are three types of Programming languages
1. Machine language
2. Assembly language
3. High Level language
Machine and Assembly languages are low level languages
1. MACHINE LANGUAGE:
It is language that is understood by the computer directly without a need of a translator
It consists of 0s and 1s
Pros:
Since it does not need a translator, it executes quickly
Memory is used efficiently
Cons:
It is very hard to write programs using 0s and 1s
It is difficult find errors and debug
It consumes more time to write a program in machine language
No human readability
2. ASSEMBLY LANGUAGE:
It is language that uses mnemonic codes instead of 0s and 1s
Example of mnemonic codes are ADD, SUB
Assemblers are needed to translate assembly language programs to machine language program
Pros:
Easier to write, read and understand compared to machine language
ILLUSTRATIVE PROBLEMS:
1. FIND MINIMUM IN A LIST
Problem Statement:
To find the minimum number in a given list of numbers
Solution:
Count of the numbers in the list is received from the user
Numbers in the list is given by the user
Consider the first number in the list as minimum number
Compare this number with all numbers
If another number is less than the minimum number, set this new number as minimum number
and do the same process until all numbers are compared and minimum is found
Algorithm:
Step 1: Start
Step 2: Read the count of numbers in list as N
Step 3: Set I = 0
Step 4: Set the first element in the list as MIN
Step 5: Repeat steps 6 to 8 until I < N
Step 6: Read the next number as NUM
Step 7: If NUM < MIN then set MIN = NUM
Step 8: Increment I
Step 9: Print MIN
Step 10: End
Pseudocode:
1. READ the count of numbers in list as N
2. SET I = 0
3. SET LIST[I] as MIN
4. WHILE I < N
READ the next number as LIST[I]
IF LIST[I] < MIN
MIN = LIST[I]
I=I+1
END WHILE
5. WRITE MIN
Variables: N, I, MIN, NUM
Flowchart:
Start
No
Is I < N?
Yes
Read the next
element in list
as LIST [I]
No MIN = LIST
Is LIST [I] < MIN?
[I]
Yes
I=I+1
PRINT
MIN
End
J=J+1
END WHILE
6. LIST [J+1] = X
Variables: N, I, J, LIST, X
Flowchart:
Start
Read the
number of
elements as N
I = 0, J = 0
Read the
Yes elements in
Is I < N?
sorted list as
LIST [I]
No
Read the
I=I+1
elements in
sorted list as
LIST [I]
No
LIST [J + 1] J=J+1
=X
End
Flowchart:
Start
Read the
range of
numbers as N
X = random number
generated between 1
and N
NUM = number
guesses by the user
Yes
Print “You
Is X = = NUM?
Win”
No
Yes Print “Value
Is X = = NUM? is too low..
Play Again”
No
Print “Value
is too high..
Play Again”
End
Flowchart:
Hanoi (N,A,C,B)
No Yes
Is N = = 1?
Hanoi (N-1, B, C, A)
End Hanoi
EXERCISES:
Develop an algorithm, flowchart and pseudocode for the following problems:
1. To find the area of a circle / square
2. To print “Hello JIT” 10 times
3. To convert Celsius to Fahrenheit [C = ((F-32)*5)/9]
4. To find a year is a leap year or not
5. To find the greatest of three numbers
6. To find the average and grade of a student (Marks are given as input)
7. To find the average and grade of „n‟ number of students (Marks are given as input)
8. To cook noodles
9. To find the roots of a Quadratic Equation
10. To determine the salary of an employee based on the following conditions: Net Salary =
Basic_Pay + DA + TA + HRA, Gross Pay = Net_Salary – EPF – LIC
11. To login to facebook
12. To find the factorial of a number
13. To generate Fibonacci series
14. To find if a number is a prime number or not
15. To reverse a number
16. To swap two numbers using a temporary variable
17. To check if a string is a palindrome or not
REFERENCES:
1. Reema Tharaja, “Python Programming using Problem Solving Approach”, Oxford University
Press, First Edition, 2017
2. http://google.com