CS11001 Programming and Data Structures MA 2011

You might also like

Download as pdf
Download as pdf
You are on page 1of 4
INDIAN INSTITUTE OF TECHNOLOGY, KHARAGPUR Department of Computer Science & Engineering Programming and Data Structures (CS11001) ‘Midsem (Autumn, 1* Year) Date: Tue, Sep 27, 2011 Time: 09:00-11:00am ‘Students: 660 Marks: 55 Answer ALL the questions. Do all rough work on separate rough sheets which you should not submit. Answer on the question paper itself in the spaces provided. Roll no: ____._Seetion: Name: 1. A vector X may be expressed in terms of its components as: X = Xz ¥+Xyj +Xzk, where (Xz, Xy, Xz) are the Cartesian co-ordinates of X and 7’, j', are the unit vectors. It may be represented using an array as: float x{3]=(Xz, Xy, Xz) (substituting Xz, Xy, Xz with their actual numerical values in the ‘C’ code). Assume that A,B, C, P, etc are similarly declared/defined (with initialisation, if necessary). ‘The dot product A - B may be computed and returned via the following ‘C’ function veeDP () defined as: ‘Also, the cross product C’ = A x B may be computed and stored in cf} via the following ‘weccP () as: function { // extra clo}= L// clij=...; CI2]=...; /* computed similarly to ClO] +/ ‘Suppose you are given two vectors A and B corresponding to the endpoints of the line segment AB, the vector AB (float. ABI3]) may be computed as: ; / done similarly +/ —1oof 4 pages — PDS (C1101) _ ‘You are also given the vector F for another point P. The vector AP (float AP [3] )may be computed as: ta. a tat=..; f+ one simttarty +/ The cross product Zp.ag = AB x AP (float %_P_AB{3]) may be computed using an above defined functions: ‘Suppose that A, B,C’ are vertices of a triangle which are co-planer to P. The vector AG (¢1oat ACL31) ‘may be computed as: : AcI1]=...; AC(2]=...; /* done similarly +/ ‘The cross product Zc,ap = AB x AC (float z_c_AB{3]) may be computed using an above defined function as: a, The dot product drc,az =-Z pap Zc,ag (Eloat. d_2_PC_AB) may be computed using an above defined function ‘The condition that P and C are on the same side of AB is: ( ) Similarly, let Z 2.0 AC x B,dpp.ac = Z pac: Z p.ac (42280), Z ayo = BC x A and dp.a.no = Z rac 2 4,nc (a2 PARC) be available. [Now the condition to détermine whether P is inside AABC is: 2. Given a polynomial of degree n, p(t) = dnt + an—12"" +... + 124 + a0, dy % 0, it can be rewritten a8: p(t) = (((dnz + dy-1)2 +. + a3)z + ap, This is the Homer's scheme for evaluating p(z). The advantage of this method of evaluation is that explicit exponentiation is avoided. Let the coefficients forthe various powers of « of p(z) be stored in an array (say) P(1, as float P{] Quate veer ty O0) An iterative function based on the above scheme is as follows: hornerPoly (, » // declarations for ( ) { // 100p } // end of Loop return sum; } CM, PM, PSD Midsem, Autumn, 2011 —2of 4 pages — PDS (CS11001) Roll: Sec: int isPerfect (int n) { // return values: 1 if perfect, 0 otherwise // declarations with initialisations 2 repeat { // stepwise computation of dvisors of n 5 xtra } until ( } // final result is computed and returned, in the last step 1 5S. (a) Representation of NaN in IEEE floating point 754 format is: 2 (®) Representation of oo in IEEE floating point 754 format is: 1 (©) Representation of (1.4)19 in IBEE floating point 754 format is: 3 (@) Decimal value of the IEEE floating point 754 number 0 10000101 10111100000000000000000 is 3 (CM, PM, PSD Midsem, Autumn, 2011 —4 of 4 pages — I ' i t | i ' ' t PDS (CS11001) ‘A recursive function based on the above scheme is as follows: hornezPoly (, rf 1 } 3. The digits of a given decimal integer n may be rotated right (e.g. 123 > 321) as follows: (i let d be the least significant digit ofr, let n’ represent the number after d is removed from n (ii let n! have & digits (k® digit is non-zero) (it) required result is 10d + n’ . Complete the function rotRight () given below, following the ‘comments and computing 10*d in steps as you determine k (rather than compute 10d separately). { // function declaration 1 int 4, nDash; // any more declarations // extract d 1 // compute ndash 1 // stepwise computation of k and 10°k + d 4 | | } // final result is computed and returned, in the last step 1 4. A perfect number is a positive integer n that is equal to the sum of its proper positive divisors (positive divisors excluding the number itself); e.g. 6=1+243 isa perfect number. It is only necessary to test whether numbers in the range 1..| yn} divide n (easily done without computing /n) to find the divisors; ¢.g. numbers in 1..5 are enough to find all divisors of 26. Complete the function 4sPerfect (), given next, following the comments. (CM, PM, PSD Midsem, Autumn, 2011 —3of 4 pages —

You might also like