CIA-1 Answer Key Sec-A
CIA-1 Answer Key Sec-A
Write the algorithm to calculate the average of three numbers and display it.
Step 1: Start.
Step 2 : Read a,b,c from the user
3 Step 3 : sum = a+b+c AP 1 2
Step 4: Avg=sum/3
Step 5:Display "sum " and "Avg"
Step 6 :End
7 As it scans the code in one go, the It scans code one line at a time; U 2 2
error are shown at the end together. errors are shown line by line.
11(a) AP 1 12
Example: Flowchart for finding area of a rectangle:
Example:
Flowchart for finding the greatest of three numbers.
2. Simplicity.
An algorithm should be precisely defined and investigated with
mathematical expressions.
9.Coding an Algorithm
Most algorithms are destined to be ultimately implemented as computer
programs.
(ii) Write algorithm, pseudo code, flowchart to find the Fibonacci series for
the first n terms
Algorithm:
Step 1: START
Step 2: Declare variable n1, n2, sum, n, i
Step 2: Initialize variables: n1 = 0, n2 = 1, i = 2
Step 3: Read n
Step 4: Repeat this step until i <= n:
sum = n1 + n2
print sum
n1 = n2
n2 = sum
i=i+1
Step 5: STOP
Pseudocode:
BEGIN
DECLARE variable n1, n2, sum, n, i
INITIALIZE variables: n1 = 0, n2 = 1, i = 2
READ n
REPEAT this step until i <= n:
SUM = n1 + n2
PRINT SUM AP 1 10
n1 = n2
n2 = SUM
i=i+1
END
Flowchart:
12(a) Write an algorithm pseudo code and flow chart for finding the minimum number AP 1 16
from the given list A:[50,40,5,9,45]
Algorithm:
Step 1: Start
Step 2: Read n
Step 3:Initialize i=0
Step 4: If i<n, then go to step 4.1, 4.2 else go to step 5
4.1: Read a[i]
4.2: i=i+1 go to step 4
Step 5: Compute min=a[0]
Step 6: Initialize i=1
Step 7: If i<n, then go to step 8 else go to step 10
Step 8: If a[i]<min, then go to step 8.1,8.2 else go to 8.2
Step 8.1: min=a[i]
Step 8.2: i=i+1 go to 7
Step 9: Print min
Step 10: Stop
Pseudo code:
BEGIN READ n
FOR i=0 to n, then
READ a[i]
INCREMENT i
END FOR
READ item
FOR i=n-1 to 0 and item<a[i], then
CALCULATE a[i+1]=a[i]
DECREMENT i
END FOR
COMPUTE a[i+1]=a[i]
COMPUTE n=n+1
FOR i=0 to n, then
PRINT a[i]
INCREMENT i
END FOR
END
Flowchart:
Outline the Towers of Hanoi problem. Suggest a solution to the Towers of Hanoi
problem with relevant diagrams.
Algorithm:
Step 1: Start
Step 2: Read n
Step 3: Calculate move=pow(2,n)-1
12(b) AP 1 16
Step 4: Function call T(n,Beg,Aux,End) recursively until n=0
Step 4.1: If n=0, then goto step 5 else go to step 4.2
Step4.2: T(n-1,Beg,End,Aux) T(1,Beg,Aux,End) ,
Move disk from source to destinationT(n-1,Aux,Beg,End)
Step 5: Stop
Pseudo code:
BEGIN
READ n
CALCULATE move=pow(2,n)-1
FUNCTION T(n,Beg,Aux,End) Recursively until n=0
PROCEDURE
IF n=0 then,
No disk to move
Else
T(n-1,Beg,End,Aux)
T(1,Beg,Aux,End), move disk from source to destination
T(n-1,Aux,Beg,En )
END PROCEDURE
END
Flowchart:
(i) Write a python program to rotate a list by right n times with and without slicing
techniques
1. Right rotate a list by n using slicing Technique
One way of slicing list is by using len () method.
# Python program to right rotate
# a list by n using list slicing
n=3
list_1 = [1, 2, 3, 4, 5, 6]
list_1 = (list_1[len(list_1) - n:len(list_1)] + list_1[0:len(list_1) - n])
print(list_1)
Output
[4, 5, 6, 1, 2, 3]
2. Right Rotate a List by n (Without Slicing)
To right rotate a list by n positions, you can use the rotate () method of the
deque object.
This method rotates the elements of the list by the specified number
ofpositions to the right.
For example:
from collections import deque
13(a) # Create a deque object from the list AP 2 10
list_1 = [1, 2, 3, 4, 5, 6]
deque_1 = deque(list_1)
# Rotate the deque by 3 positions to the right
deque_1.rotate(3)
# Convert the deque back to a list
list_1 = list(deque_1)
print(list_1)
Output
[4, 5, 6, 1, 2, 3]
(ii)Demonstrate debugging and its types with relevant example
Debugging
Errors occurring in programming are called as bugs.
The process of tracking these bugs is called as debugging.
There are three types of errors that can occur while coding :
Syntax Error, Runtime Error and Semantic Error.
1. Syntax Error
The syntax is a defined structure or set of rules while writing a program.
EXAMPLE:
>>> a=1+2
>>> a
3
>>> 1+2=a
Syntax Error: can't assign to operator
2. Runtime Error
In this, program is executed successfully without syntax or logical error and
this type oferror occurs at runtime i.e. while accepting values from user.
EXAMPLE:
>>>a=2
b=3
c=4
d=input("enter a number")
sum=a+b+c+d
print(sum)
OUTPUT
enter a number5
Traceback (most recent call last):
File "C:/Python34/runtime error.py", line 5, insum=a+b+c+d
3. Semantic Error
In this program will be executed without any error on prompt but will not
give desired output.
Such type of error is called Semantic error.
EXAMPLE: Program to find sum of two digits
AP 2 6
num1=int(input("Enter first number|:"))
num2=int(input("Enter second number:"))
add = num1 - num2
print("sum of two nums is ", add )
OUTPUT:
Enter first number:200
Enter second number:57
sum of two nums is 143
In above example sum of two numbers is executed.
Still desired output is not achieved due to logical error.
Instead of “+” symbol “-” is used.
(i)Explain statements and its types with example.
Statement:
A statement is an instruction that a Python interpreter can execute.
There are mainly four types of statements in Python
Print Statements
Assignment Statements
Conditional Statements
Looping Statements
Print Statements:
The result of a print statement is a value.
Example:
Print(‘Hello)
Output:
Hello
Assignment Statements:
Assignment statements don’t produce a result it just assigns a value to the
operand on its left side.
Example:
a=10
This is an assignment statement, wherea is a variable name and 10 is its
value. 10 is stored in a’s memory location. 10 is assigned to variable a.
Conditional Statements:
if statement: It is a control flow statement that will execute statements
under it, if the condition is true. It is also known as a conditional statement.
Example:
13(b) if a>b: AP 2 8
print(“a is greater”)
else:
print(“b is greater”)
Looping Statements:
while statements: The while loop statement repeatedly executes a code
block while a particular condition is true.
for statement:Using for loop statement, we can iterate any sequence or
iterable variable.
The sequence can be string, list, dictionary, set, or tuple..
Example:
for i in range(1,11):
print(i)
Output:
1 2 3 4 5 6 7 8 9 10
14.(a) What is the role of an interpreter? Give a detailed note on python interpreter
and interactive mode of operation.
Role of an interpreter:
Python Interpreter translates high level instructions into an
machine level language. It executes instructions directly without compiling.
1. + (Addition)
x + y= 25+5 = 30
2. - (Subtraction)
x-y= 25-5 = 20
3. * (Multiplication)
x*y= 25*5 = 125 AP
4. / (Division)
x/y= 25/5 = 5
5. // Floor division)
15(a)
x//y= 25//5 = 5
6. ℅ (Modulus)
x % y= 25%5 = 0
(OR)
Write a syntax, program and draw a flow chart for chained condition execution and
nested conditionals.
Chained condition execution
Chained if...elif...else U 3
The elif is short for else if.
This is used to check more than one condition.
If the condition1 is False, it checks the condition2 of the elif block.
If all the conditions are False, then the else part is executed.
15(b)
The if block can have only one else block.
But it can have multiple elif blocks.
Syntax:
Flowchart:
Example:
Student mark system
mark=int(input("enter your mark:"))
if(mark>=90):
print("grade:S")
elif(mark>=80):
print("grade:A")
elif(mark>=70):
print("grade:B")
elif(mark>=50):
print("grade:C")
else:
print("fail")
Output
enter your mark:78
grade:B
Nested conditionals
One conditional can also be nested within another.
Any number of conditions can be nested inside one another.
In this, if the condition is true it checks another if condition1.
If both the conditions are true statement1 get executed otherwise
statement2 get execute.
If the condition is false statement3 gets executed.
Syntax:
If (condition):
If(condition 1):
Statement 1
else:
statement 2
else:
statement 3
Flowchart:
Example:
Greatest of three numbers
a=int(input(“enter the value of a”))
b=int (input(“enter the value of b”))
c=int (input(“enter the value of c”))
if(a>b):
if(a>c):
print(“the greatest no is:”,a)
else:
print(“the greatest no is:”,c)
else:
if(b>c):
print(“the greatest no is:”,b)
else:
print(“the greatest no is:”,c)
Output
enter the value of a: 9
enter the value of b: 1
enter the value of c: 8
the greatest no is 9