Unit 1
Unit 1
PROGRAMMING
UNIT-1
ALGORITHMIC PROBLEM SOLVING
PART-A
1. ALGORITHAM
Algorithm is defined as a step procedure for solving any problem it is
also
a previous rule specifying how to solve a problem
QUALITIES
Accuracy
Memory
Time
Sequence
2. What are characteristics of algorithm?
A algorithm has a first number of inputs
Every instruction should be preise
Ensure that algorithm has proper fernination
Effectness of each step is very important
The desired output must be obtrined only after the algorithm terminates
the algorithm should be written in sequence
SIMPLE STATEMENT
Assignment A:=A+5
goto: goto Next
return: return 5
call: function( )
COMPOUND STATEMENT
Block
Do loop
For loop
If statement
Else
Switch statement
Switch ( )
{
Case ‘a’;
Alert( );
Break;
Quit( )
Break;
}
While loop
Exec
Eval
START
Start
Read A, B
If
A>B
Print A Print B
Stop
Start
Initialize
R=1 Fact=1
If(i<=n)
Fact=fact*i i=i+1
Print fact
stop
PART –B
1. Building block of algorithm ?
INSTRUCTION:
In computer programming a statement is the smallest stand alone
element of an imperation programming language that express some
action to be carried out
KIND OF SATEMENTS
Simple statement
Compound statement
SIMPLE STATEMENT
Assignment A:=A+5
goto: goto Next
return: return 5
call: function( )
COMPOUND STATEMENT
Block
Do loop
For loop
If statement
Else_
While.loop :which(…..)do…
Switch statement:
Switch(c){ case ‘a’;alert( );break;case’q’;quit( );break;}.
This is found is the exes and eval function found in some language
In the python both and found with exes applied to statement and eval
applied to statement and eval applied to expression
STATE:
CONTROL FLOW:
Process 1
Process 1 Read the
radius r
Process 2
Process 2
Process 3
S=A+B
Process 3
Print S
Stop
Pseudo code:
If condition THEN
Process 1
..
..
End if
Flow chart:
If
condition
Process
PSEUDO CODE
IF Condition then
Process 1
..
..
Else
..
..
Process 2
End if
Flow chart
If
condition
Process 1 Process 2
CASE STRUCTURE :
This is a multiways selection structure that is used to choose one option
from many options
Pseudo code:
Case type 1
Case type 1
Process 1
Case type 2
Process 2
..
..
Case type n
Process n
..
..
End if
Flow chart:
Type
Process 1 Process n
Process 2
FUNCTION:
Function are self contained modules of code that accomplish a specific
task , function useally take in data ,process it and return a result
SYNTAX:
Def(function name)( );
2. What is pseudo code and explain the rules for writing pseudo code with
suitable example?
PSEUDO CODE:
Pseudo code came from two word pseudo and code ”pseudo” means
initiation and ”code” refer to instruction written in the programming
language pseudo code is programming analysis tool that is used for
planning program logic
EXAMPLE:
READ num1,num2
Result=num1+num2
WRITE result
EXAMPLE:
TOT=m1+m2+m3
Avg=tot/5
WRITE Tot,avg
EXAMPLE:
EXAMPLE:
If a>b then
Print a
ELSE
Print b
stop
start
Write within statement symbols briefly if necessary, you can use the
annotation symbol to describe data or computational
If the flowchart becomes complex,it is bettes to use connector symbol to
reduce the number of flow line . avoid the inter section of flow line if
you want to make it more effective and better way of communication
Ensure that the flow chart has a logical start and stop
ADVANAGES OF FLOWCHART:
Communication
Effective analysis
Proper documentation
Proper debugging
Efficient coding
Efficient program maintenance
DIS-ADVANAGES OF FLOWCHART:
Complex logic
Modification or Pltesation
Reproduction
Unknown
Cost
Read A, B
If a>b and
a>c
Stop
4. Function
Function are “self contained contained” moduler of code that
accomplish a specific task function usually “the in “ data process it and
“return” a result. once a function is written, in can be used over and
over again function can be”called” from the inside of other function
Function “Encapsulate” a task most programming language provide
many step to accomplish
When a function is called the program levess the current section of
code and begin to execute the first line inside the function
The program come to line of code containing a function call
The enter the function
All instruction inside of the function are execute from top to bottom
The program leaves the function and goes back to where it started from
Flow chart:
start
Create a num
random
between 1-10
Read guess
number from
user
If
Guess> Number is
num higher
If
Guess>n Number is
um lower
If
Guess>
num
Winner ( i )
Program:
imput random
BOUNDS= (1,100)
TRIES-ALLOWED=5
The number= random,randint (*BOUNDS)
Print("If welcome to "Gruess my number"In`")
Print ("I'm thinking of a number between %d and %d;%BOUNDS)
Print ("Try to guess it in as few attempts as possible.(n")
for tries in range (TRIES-ALLOWED);
Guess=int(input("Take a guess;"))
i. guess>The-number;
print ("lower")
elif guess<The-number;
print ("Higher.....")
else;
print("you gussed if! The number was %d"% (The-number))
print("And it only took you %d tries /n/n"%(triestl));
break
else;
print("you failed to guess in time !/n/n")
# Admittedly a controted way to write a statement
that works in both python 2 and 3
try;
input ("process the enter key to exit")
execpt
pass
write (from,to);
return;
}# move top n-1 disks from A to B using C towers
(n-1),from.aun,to);
Write (from,to);
#More remaining disk from B to C using A towers
(n-1,aux,to,fr);
Output
Welcome to " Guess My Number"!
I'm thinking of a number between 1 and 100
Try to guess it in as few attempt as possible
Take a guess :3
Higher
Take a guess :4
Higher,,,,,,
Take a guess ; 99
you guessed it!The number wall 99
And it only took you 4 tries !
Press the enter key to exit.
Algorithm of Tower of Hanoi:
{ write ("/n/n Enter the total numbers of disk");
Read (n);
towers(n,'A','C','B');
}
Subroutime towers (n,fr,to,aux)
{
# if only one disk has to be moved
if n==1
{
Flow chart:
start
Enter n (i.e)
number of
disks
Call the
function towers
(n, A, B, C)
stop
If
N=1?
Call the
function towers
( n-1, fr to aux
)
Program:
def-init-(self);
self counter=0
def hanoi (self,n,a,c,b).
if n==1;
self. countes+=1
print('{0}->{1}'. formal (a,b))
else;
self, hanoi(n-1,a,b,c,)
self,hanoi(1,a,c,
self,hanoi(n-1,b,c,a)
towes=Tower()
tower,hanoi(3,"a","b","c")
Output:
a->b
a->c
c->a
a->b
b->c
b->a
a->b
ix. Find Miuinum in a list python:
x=[2,3,5,9,1,o,8,7]
def my-min(sequence);
low= sequence [0]#used to start with some value
for i in sequence;
if i<low;
low=i
return low
print my-min (x)
Output:
2
x=[2,3,5,9,1,0,8,7]
high=sequence[0]#need to start with some value
for i in sequence
if i>high
high=i
return high
print my max (x)
Output:
9
Flow chart:
start start
If If
i<low i>high
low=i high=i
Return low Return high
stop stop