UNIT_I_notes_Python_studentcopy
UNIT_I_notes_Python_studentcopy
Fundamentals of Computing:
OVERVIEW
An electronic machine takes information from the user, process the input and gives the
output
1
Computer performs five major operations
INPUT UNIT
2
CONTROL UNIT.
The control unit which acts like a supervisor that the things are done in proper fashion.
The control unit determines the sequence in which computer programs and instructions are
executed. It coordinates all activities of the peripheral of the computer.
Perform arithmetic and logical functions of the computer. The major operations
performed by ALU are addition, subtraction, multiplication, division, logic and
comparison. Logic operations are AND, OR, XOR, NOT etc.
MEMORY UNIT
OUTPUT UNIT
Output unit process of producing results from the data for getting user information’s.
It also stores values after processing the inputs.
Example: Monitor, Printer, etc.
SOFTWARE
Set of instructions written in any programming language. A program controls all the
activities of the processor.
CHARACTERISTICS OF COMPUTER
3
DILIGENCE-It can perform repeated tasks with the same speed and accuracy,
Unlike human beings, a computer is free from monotony, tiredness, and lack of
concentration.
REABILITY-A computer is a reliable machine and designed to make
maintenance easy.
VERSATALITY-This machine can be used to solve the problems related to
various fields
COST REDUCTION.
APPLICATION OF COMPUTER
Decision problems
Search problems
Counting problems
Optimization problems
Decision problems
Search problems
A search algorithm which solves a search problem. A search algorithm work to retrieve
information stored within some data structure. Three types of search algorithms.
4
1. Linear search
2. Binary search
3. Hashing
Counting problems
Counting problem is the problem of finding the number of distinct elements in a data stream.
Optimization problems
An optimization problem is the problem of finding the best solution from all the feasible
solution. It can be divided into two categories, depending on whether the variables are
continuous or discrete.
ALGORITHM:
Algorithm is defined as a step by step procedure for solving any problem. The sequence
of steps to be performed in order to solve a problem by the computer is known as an
algorithm.
Characteristics of an Algorithm:
Algorithm has a finite number of inputs.
Every instruction should be precise and unambiguous.
Ensure that the algorithm has proper termination.
Effectiveness of each step is very important.
The desired output must be obtained only after the algorithm terminates.
The algorithm should be in sequence.
Quality of algorithm:
Time - Lesser the time taken better the quality.
Memory - Require minimum computer memory.
Accuracy – Should provide accurate result.
Sequence - Procedure of an algorithm in a sequential form.
Generality – fit to all type of inputs.
Representation of algorithm:
Algorithm has a starting point and a final point. Between these two points are
the instructions that solve the problem.
5
Algorithms often have steps that repeat or require decisions.
Algorithm can be expressed in any language from natural languages to
programming languages.
Example:
Algorithm (find the area)
Step 1: start
Step 2: Read r
Step 3: calculate area=3.14*r*r
Step 4: Display area
Step 5: Stop
6
Step 5 : Account Type
Instruction/Statements
State/Selection
Control flow
Functions
Instruction/Statements
A statement is the smallest standalone element of a programming language that
expresses some action to be carried out.
7
An instruction written in a high-level language that commands the computer to
perform a specified action.
8
Functions
Function is a sub program which consists of block of code (set of instructions) that
performs a particular task.
For complex problems, the problem is being divided into smaller and simpler tasks
during algorithm design.
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 again and again.
Functions can be from inside of other functions.
Example: Addition of two numbers.
Step 1: Start
Step2: Call add() function
Step 3: End
add() function:
Step 1: Start add() function
Step 2: Read a,b
Step 3: c=a+b
Step 4: Display c
Step 5: End
Pseudocode
Flowchart
Programming Language
(i)Pseudocode
Pseudocode came from two words. Pseudo and Code Pseudo means imitation and
Code refer to instructions written in a programming language.
Rules for writing Pseudocode
Write one statement per line.
Each task will correspond to one line of Pseudocode. (Example READ
name,regnno,marks)
Capitalize initial keywords.
The keywords should be written in Capital
letters(EX:READ,IF,THEN,REPEAT,ENDIF)
Ident to show hierarchy.
Indentation is a process of showing the boundaries of the structure.
Sequence – It keeps the statements in sequence.
Selection – It indent the statements that fall inside the selection structure.
Looping -It indent the statements that fall inside the loop.
EX: WHILE(i<=10) // LOOPING
If i<10 THEN // SELECTION
9
i=i+2
ELSE
i=i //SEQUENCE
ENDIF
ENDWHILE
End multi line structure.
Each structure must be ended properly.
EX:ENDIF for IF statement
Keep statement language independent
Input READ,OBTAIN,GET
Output PRINT,DISPLAY,SHOW
Compute COMPUTE,CALCULATE,DETERMINE
Initialize SET,INIT
Add one INCREMENT,BUMP
Input, Output, Compute, Initialize, Add one SEQUENCE
IF,THEN,ELSE,ENDIF IF-THEN-ELSE
CASE,OF,OTHERS,ENDCASE A CASE
Multi-way branch based on conditions CASE
10
Iterate with index i from m to n TO
Advantages of Pseudocode.
Can be done easily on a word processor
Easily modified
No symbols are used
It is simple because it uses English-like statements.
No specific syntax is used.
Disadvantages of Pseudocode.
It’s not visual
There is no accepted standard.
Cannot be compiled not executed.
(ii) Flowchart
A flow chart is a diagrammatic representation that illustrates the sequence of
operations to be performed to arrive at the solutions.
Each step in the process is represented by a different symbol and contains a of
the process step.
The flowchart symbols are linked together with arrows showing the flow of
directions.
11
Denoted either an input
2. Input/output
or output operation.
Represent decision
4. Decision
making and branching
Advantages of flowchart.
Communication.
Effective analysis.
Proper documentation.
Efficient coding.
Proper Testing and Debugging.
Efficient Program Maintenance
12
(iii) Programming Language
In computer technology, a set of conventions in which instructions for the machine are
written. There are many languages that allow humans to communicate with computers.
(a)Machine Language
The machine language consists of binary numbers that encode instructions
for the computer.
Every computer has its own machine language.
Example: 10110101-B5
(b)Assembler Language
An assembler language consists of mnemonics
There is one mnemonic for each machine instruction of the computer.
Each assembler instruction is a mnemonic that corresponds to a unique
machine instruction.
Example:
Start
Add x,y
Sub x,y
…………….
……………..
END
(c)High level language
A high level programming language allows the programmer to write
sentences in this language which can be easily translated into machine
instructions.
The sentences written in a high level programming language are called
statements.
Example:
main()
{
if(x<y)
min=x
}
13
EXAMPLES FOR NOTATIONS:
Example
Algorithm (Add two numbers) Flowchart START
Step 1: Start
Step 2: Get the value of a and b.
Step 3: c=a+b Read a, b
Step 4: Display c value.
Step 5: End. C=a+b
Pseudocode
READ a, b Display C
C=a+b
WRITE C
END
Algorithm :
Step 1 : Start
Step 2 : Read n
Step 3 : Check n%2==0 then
Step 4: Display n is even
Step 5: Otherwise Display n is odd Flowchart
Start 4 : Stop
Pseudocode:
READ n
IF n%2 ==0 THEN
DISPLAY “EVEN”
ELSE
DISPLAY “ODD”
14
3. Write an algorithm, flowchart & pseudocode for sum of ‘n’ numbers.
Algorithm:
Step 1: Start
Step 2: Read n, sum=0
Step 3: Initialize loop variable i=1
Step 4: Check i<=n then
Step 5: sum=sum+i
Step 6: i=i+1
Step 7: Goto step 4 until the condition gets false.
Step 8: Display sum
Pseudocode:
READ n
INITIALIZE sum=0, i=1
WHILE i<=n THEN
sum=sum+i
i=i+1
END WHILE
WRITE sum
Flowchart
Start
False
Check i<=n
True
sum=sum+i
i =i+1
Display sum
End
15
ALGORITHMIC PROBLEM SOLVING
Decide on:
Computational means exact vs
approximate, algorithm design
technique solving
Design an algorithm
Prove correctness
16
1. Understanding The Problem:
The given problem must be understood completely.
An input to an algorithm specifies an instance of the problem the
algorithm solves.
2. Ascertaining The Capabilities of the Computational Device:
After understanding a problem, ascertain the capabilities of the
computational device.
Sequential algorithm: Instructions are executed one after another, one
operations at a time.
Parallel algorithms: The central assumption of the RAM model does not
hold for some newer computers that can execute operations
concurrently, (i.e.) in parallel.
3. Choosing between exact and approximate problem solving:
Solving the problem exactly is called an exact algorithm.
Solving it approximately is called an approximation algorithm.
4. Algorithm design techniques:
Algorithm design technique is a general approach to solving problems
algorithmically that is applicable to a variety of problems from different
areas of computing.
It provides guidance for designing algorithms for new problem.
5. Methods of specifying an algorithm:
Pseudo code
Flowchart
Pseudo code:
It is a mixture of a natural language and programming language like
constructs.
Pseudo code is usually more precise than programming language.
Flowchart:
Flow chart is a method of expressing an algorithm by a collection of
connected geometric shapes containing descriptions of the algorithm steps.
6. Proving an algorithm correctness:
After specifying the algorithm, it should be proved for its correctness.
A common technique for proving correctness is to use mathematical
induction.
Tracing the algorithm correctness for specific inputs can be very
worthwhile activity, it cannot prove the algorithm’s correctness
conclusively.
The notion of correctness for approximation algorithms is less straight
forward than it is for exact algorithms.
7. Analyzing an algorithm
17
There are two kinds of algorithm efficiency:
Time Efficiency-Indicating how fast the algorithm runs
Space Efficiency-Indicating how much extra memory it uses.
Simplicity:
Simpler algorithms are easier to understand and easier to program.
The resulting programs usually contain fewer bugs.
Generality:
Generality of the problem the algorithm solves.
The set of input it accepts.
8. Coding an Algorithm:
Coding or programming is the process of translating the algorithm into
the syntax of a given programming language.
Convert each step in the algorithm into one or more statements in a
programming language
18
Alert by Alarm
19
SIMPLE STRATEGIES FOR DEVELOPING ALGORITHMS
Iteration and recursion are key computer science techniques used in creating
algorithms and developing software.
Iteration
Iterative are programs that follow a path from the starting instruction till the end of the
algorithm.
Iterative functions are on that loops to repeat some part of the code.
Using a simple loop to display the numbers from one to ten is iterative process.
Steps to develop an iterative algorithm:
Define problem: The problem that needs iteration has to be identified and defined.
Initial conditions: The condition that has to satisfied to start the iteration.
Define loop variants: The variable that controls the number of iteration has to be
defined.
Define step: The steps that are to be repeated must be defined.
Define measure of progress: The loop invariant that have been defined would be
changed when algorithm progress.
Define Exit condition: When the iteration would be stopped has to be identified.
Make progress: Move forward after executing an instruction.
Maintain Loop variants: In order to repeat the steps, the loop invariant must be
maintained in range.
Ending: When the iteration has stop.
20
Recursion:
Recursive function is one that calls itself again to repeat the code.
Recursion is a problem, solving approach by which a function calls itself
repeatedly until some specified condition has been satisfied.
Recursion splits a problem into one or more simpler versions of itself.
In a recursive algorithm the algorithm calls itself with smaller input values.
The recursive programs require more memory and computation compared with
iterative algorithms.
Example: factorial of a given number.
n!=n x (n-1)!
4!=4 x(4-1)!
=4 x (3!)
=4 x 3 x 2 x1
=24
Advantages of recursive functions:
Recursion can produce simpler, more natural solutions to a problem.
It is written with less number of statements.
Recursive functions are effective where the terms are generated successively
to compute a value.
It requires few variables.
It is useful for branching process.
Example algorithm for recursion:
GCD using recursion.
Factorial given number using recursion.
Step 1 : Start
Start 2 : Read n
Start 3 : Initialize loop variable i = 1 and fact = 1
Start 4 : Check i <= n then
Start 5 : calculate fact = fact * i
Start 6 : i=i+1
Step 7 : Go to step 4 until the condition gets false
Start 8 : Display fact
Start 8 : Stop
21
Pseudocode :
READ n
INITIALIZE i=1, fact=1
Line 1: IF i<=n THEN
COMPUTE fact=fact*1
i=i+1
GOTO line1
DISPLAY fact
Flowchart :
Start
False
Check i<=n
True
fact = fact*i
i =i+1
Display fact
End
22
Pseudocode:
READ n
factorial=CALL fact(n)
WRITE factorial
fact(n)
BEGIN
IF n==1 OR n==0 THEN
RETURN 1
ELSE
RETURN n*fact(n-1)
ENDIF
END
Flowchart:
Start
Read n fact(n)
True
factorial=fact(n)
Check
n==0||n==1 Return 1
False
Display factorial
Return n*fact(n-1)
End
23
ILLUSTRATIVE PROBLEMS:
24
2. Inserting a card in a list of sorted cards
Problem Statement:
Imagine that you are playing a card game. You are holding the cards in your hand, and
these cards are sorted. You are given exactly one new card. You have to put it into the correct
place so that the cards you are holding are still sorted.
25
Pseudocode:
BEGIN
READ n
FOR i=0 to n THEN
READ a[i]
i=i+1
END FOR
READ item
FOR i=n-1 to 0 and item<a[i] THEN
CALCULATE a[i+1]=a[i]
i=i-1
END FOR
ASSIGN a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n then
DISPLAY a[i]
i=i+1
END FOR
END
Flowchart
26
3. GUESSING AN INTEGER NUMBER IN A RANGE
Problem Statement:
Imagine you are trying to guess a number in a range. The objective is to randomly
generate integer number from 0 to n. Then the player has to guess the number. If the player
guesses the correctly, output an appropriate message.
ALGORITHM:
Step 1: Start
Step 2: Generate a random number and call it num.
Step 3: Repeat the following steps until the player has guessed the correct number.
Step 3.1: Enter the number to guess.
Step 3.2: Check guess==num
27
Print “You guessed the correct number”
Otherwise check guess <num
PSEUDOCODE 1:
BEGIN
READ Guess_num,input_num
IF guess_num>input_num THEN
WRITE “Your guess is higher than the input_number!”
ELSE IF guess_num<input_num THEN
WRITE “Your guess is lower than the input_number!”
ELSE
WRITE “You guessed the correct number!”
END IF
END
Flowchart
28
4.TOWER OF HANOI:
Problem Statement:
The smaller one sits over the larger one. The objective is to move the entire disk to some
another tower without violating the sequence of arrangement.
Rules to be followed:
1. Only one disk can be moved among the towers at any given time.
2. Only the “Top” disk can be removed.
3. No large disk can sit over a small disk.
29
Algorithm:
Step 1 :Start
Step 2: Read n,source,dest,aux
Step 3:Call Tower(disk,source,dest,aux)
Step 4:Stop
Tower() Function
Step 1: Start Tower(disk, source, dest, aux)
Step 2: Check disk == 1, THEN
Step 3: move disk from source to dest
Step 4: Otherwise
Hanoi(disk - 1, source, aux, dest) // Step 1
move disk from source to dest // Step 2
Hanoi(disk - 1, aux, dest, source) // Step 3
Step 5: Repeat Step1 until the last disk moved.
Step 6: Return
30
FLOWCHART
Pseudocode:
BEGIN
READ disk, source, dest, aux
CALL MoveTower(disk,source,dest,aux)
END
FUNCTION MoveTower(disk, source, dest, aux):
IF disk == 0, THEN:
move disk from source to dest
ELSE:
MoveTower(disk - 1, source, aux, dest) // Step 1 above
move disk from source to dest // Step 2 above
MoveTower(disk - 1, aux, dest, source) // Step 3 above
END IF
31