Answers to Mid-semester exam
questions
Q.1 a) 12 in binary is 1100
0.8 in binary is 0.110011001100…
12.8 is 1100.110011001100….
Since 9 bits for mantissa out of
which 1 is for sign, only 8 bits can be stored
Exponent is to the base 16. 3 bits implies exponent is between –3 to 4.
Number stored would be 0.11001101 ´ 161
This is 12.8125 ( note that last bit
is rounded) ( 2 Marks, no partial
credit)
Largest number obtained by all
mantissa bits = 1 and exp = 4, approximately 216
Smallest number has all mantissa
bits except last = 0 and exp = -3, 2-8 ´ 16-3 = 2-20
(1 Mark each , no partial credit)
b) fact = PRODUCT( (/
(fact, fact = 1, n)
/ ) ) (2 Marks, no partial credit)
c)
1
2 3 4 5 8 7 7 9 6
a(: : 2) is the array 1 3 5 7 9
a(7:3:-1) is 7 6 5 4 3
a(: : 2) <=
a(7:3:-1) is the logical array T T T F F
a(10:2:-2) = a(6:) is an array assignment performed
only where the logical array is T
a(10) = 6
a(8) = 7 and a(6) = 8 are the only changes (2 Marks, no partial credit)
Q.2 A sample program
READ *, k;
right = n (2 Marks
for correct logic)
left = n
grade = “A”
DO i = 1, 4
DO (2 Marks for correct loop)
IF
(left = = 0) EXIT
IF
((marks (right)-marks (left)) > k) EXIT
left
= left – 1
END DO
PRINT *, “number of students with
grade”, grade, “ is ”, right – left
right = left
grade=achar (iachar (grade)+1) (1
Mark for printing grade correctly)
END
DO
PRINT *, “number of students with grade”,
grade, “ is ”, right
(1 Mark for correctly handling fail grade)
It is possible to use binary search in inner loop to
get a more efficient program. However in this case simplicity is more
important. Also, grade would have to be
printed for every student, so anyway, the array would have to be traversed.
Q.3 The main idea is that some power of b is divisible by n iff
every prime number which divides n also divides b. n and b satisfy this property the iff n and gcd(n,b) do which is true iff n/gcd(n,b) and gcd(n,b)
satisfy the property. When n = = 1, all
prime factors of n have been removed by dividing by some prime factors of b. If
gcd = = 1 while n > 1, n has prime factors which are not divisors of b.
READ *, n, b
no_of_digits
= 0
DO
IF ( n =
= 1) EXIT
! find
gcd of n and b, one more digit required
no_of_digits
= no_of_digits + 1
m = n
l = b
DO
IF
( l = = 0) EXIT
temp
= modulo(m,l)
m
= l
l
= temp ( 1 mark
for each correct blank)
END DO (no
partial credit)
IF
( m = = 1) EXIT
! if gcd
= 1 then it is not possible
! modify
values of n and b.
n = n /
m
b = m
END DO
If ( n = = 1) then
PRINT *,
“ representation possible”
ELSE
PRINT *,
“representation not possible”
ENDIF
Q.4
Main points in the program.
Identify required substrings ( username, dept and domain name)
Find first nonblank character, first
occurrence of “@” and first occurrence
of “.”
Check validity of each field.
Sample program
PROGRAM check_address
IMPLICIT NONE
CHARACTER (LEN=30) :: address
INTEGER :: i, first, at, dot
LOGICAL :: valid (1 Mark for using a logical
variable)
READ *, address
valid = .TRUE.
first = verify(address, “ ”)
at = scan(address, “@”) (2
Marks for correctly identifying substrings)
dot = scan(address, “.”)
IF ((at – first) > 8 .OR..
(at-first) < 1 ) valid =
.FALSE.
DO i = first, at-1
SELECT CASE (address (i : i))
CASE (“0” : “9”,
“a” : “z”, “A” : “Z”)
CASE DEFAULT (2 Marks for correctly checking
username,
valid =
.FALSE. 1 mark if IF THEN ELSE used)
END SELECT
END DO
SELECT CASE (address (at+1 : dot)) (2 Marks for correctly checking dept)
CASE (“aero.”, “cse.”, “ee.” ….)
! include the . , otherwise blanks
may be present after dept
CASE DEFAULT
valid
= .FALSE.
END SELECT
SELECT CASE (address (dot+1 :)) (1 Mark for checking remaining
substring)
CASE (“iitb.ac.in”, “iitb.ernet.in”)
CASE DEFAULT
valid =
.FALSE.
END SELECT
IF (valid) THEN
PRINT
*, “address is a valid email address” (upto
2 Marks may be
ELSE deducted for clarity)
PRINT
*, “address is not valid”
ENDIF
END PROGRAM check_address