Nothing Special   »   [go: up one dir, main page]

2 1 GS 4353 Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

B.Sc. Engg. (CEE)/ 3rd Sem.

Date: 16 September, 2021


Semester: Winter 2020-2021 Time: 11:00 am – 12:30 pm

ISLAMIC UNIVERSITY OF TECHNOLOGY (IUT)


ORGANISATION OF ISLAMIC COOPERATION (OIC)
DEPARTMENT OF CIVIL AND ENVIRONMENTAL ENGINEERING

Semester Final Examination Winter Semester : 2020 - 2021


Course Number: GS 4353 Full Marks: 75
Course Title: Numerical Methods and Computer Time : 1.5 Hours
Programming

There are 3 (Three) questions. Answer all 3 (Three) questions. The symbols have their usual
meanings. The examination is Online and Open Book. Marks of each question and corresponding
CO and PO are written in the brackets.

1. (a) The program shown below was written by a newly appointed programmer to (10)
find out the cube root of any number by using exhaustive enumeration (CO2)
algorithm. Examine the code thoroughly to identify the problems that may (PO2)
arise if:
i. You enter last three digits of your ID in the variable increment,
ii. You enter last two digit of your ID in the variable epsilon,
iii. You enter (last two digit of your ID × -1) in the variable X,
iv. You enter a number between 0 and last digit of your ID in the variable
X.
Program
X = float(input('Enter any number: '))
epsilon = float(input('Enter the allowable error: '))
guess = 1
increment = float(input('Enter the increment value: '))
num_guesses= 0
while abs(guess**3 - X) >= epsilon and guess <= X:
guess += increment
num_guesses+= 1
print('num_guesses=', num_guesses)
print(guess, 'is close to the cube root of', X)

Page 1 of 3
(b) Write a python program that uses Bisection search algorithm to find the root (15)
of the equation f(x) = x3 – 5x2 + 5x + 1 = 0. Consider an allowable error of (CO3)
(last digit of your ID × 0.001). Follow the instructions given below to set the (PO1)
low and high value for the bisection search range:
Step 1: Declare two variables n1 and n2. These variables should take an
integer value as input from the user.
Step 2: Create a loop that will break only when the value of the function f(n1)
is negative. Otherwise, continues to print an instruction that tells the user to
try again.
Step 3: Create another loop that will break only when the value of the function
f(n2) is positive. Otherwise, continues to print an instruction that tells the user
to try again.
Step 4: Set the values of f(n1) and f(n2) as your low and high values
respectively.
Step 5: You can now proceed to solve equation using bisection algorithm.

2. (a) What would happen if the “return y” line was missing from func_b? Explain (10)
using Variable scope (Global and Local Scope). (CO2)
(PO2)
Program
def func_a():
print('inside func_a')
def func_b(y):
print('inside func_b')
return y
def func_c(z):
print('inside func_c')
return z()
func_a()
x = input("Enter the last three digits of your student
ID: ")
print(5 + func_b(x))
func_c(func_a)

(b) Write a python program to show at least 5 different list operations. (15)
(CO3)
(PO1)

Page 2 of 3
3. (a) i) What type of error will occur if you try to run the given python script? Set (10)
the average mark as zero in such cases. (CO2)
(PO2)
ii) Also, improve the code to print ‘invalid input’ for handling the
‘ValueError’ in case of additional entries in test_grades.
Program
def get_stats(class_list):
new_stats = []
for elt in class_list:
new_stats.append([elt[0], elt[1],
avg(elt[1])])
return new_stats
def avg(grades):
return sum(grades)/len(grades)

test_grades = [[['peter', 'parker'], [80.0, 70.0, 85.0]],


[['bruce', 'wayne'], [100.0, 80.0, 74.0]], [['dead', 'pool'],
[]]]
get_stats(test_grades)

(b) The following python script uses the concept of class to keep track of a (15)
student’s marks. Introduce a new subclass named Grades to show a student’s (CO3)
letter grade from her total marks. Use the conversion table given below. (PO1)

Total Marks Letter Grade


80-100 A
70-79 B
60-69 C
59 and below F
Program
class Marks():
def __init__(self, quiz):
self.quiz = quiz

def totalmarks(self, bonus):


total = self.quiz + bonus
return total

x = input("Enter the last two digits of your student ID: ")


mygrade = Marks(x)
print(mygrade.totalmarks(9))

Page 3 of 3

You might also like