COM SCI 31 Fall - 1998 Midterm

COM SCI 31 Fall - 1998 Midterm - UCLA CS 31 Midterm Winter,...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UCLA CS 31 Midterm Winter, 1996 2/12/96, 8:00 am - 9:50 am at Kinsey 141 Signature: __ __ y I. True/False Questions [20 points, 2 points each] Indicate whether each of the following statements is true (T) or false (F). 1. T ® In C++, we can use -n and --n + 1 to get the 2’s complement and 1's complement of the unsigned integer :1 respectively. 2. F In C++, the functions none Him: 1, int: j){:cturn 1 t 11} and int cum: a. int b){roturn a + m) have exactly the same function signatures. Therefore, they can not be used in the same program. 3.® F A global variable always has a static storage class, while a local variable may have either a static or an auto storage class, depending on how it is defined. 4. T Recursive functions usually take shorter time and less memory space to run than their iterative equivalents. 5. ® The function fin: “int: c - 2. in: 1:) {return a * by} contains a valid default argument value (2) for its first parameter. (an only 0156’ Offlaul’l figuring econ" elvlilr‘h mtg/unten‘fS 6.®F The time complexity of the Tower of Hailioi problem is exponential because its solution involves 2“ - 1 steps of disc moving for n discs. 7.®F In C++, if a function a has its function prototype declared as int. rune an, int: n b. in: can, then its first and second parameters are call-by-reference and its third parameter is call- by-value. 8.®F In C++, for an unsigned non-zero integer n, the program fragment :1 a. (11-1) will change the right-most l in the binary representation of 11 into 0. H a n 5.. I) 9. T @In C++, the function defined as void unpum: 1. int 3) (in: e; t-i; 1-1: jun} swaps the values of its two parameters provided by the caller for the caller. 10. T F Selection sort and quick sort algorithms have the time complexity 0(n2) and bubble sort has the time complexity 0(n * log n). 11. What will be printed? [30 points, 2 points for each cent] In front of each of the coue's in the following C++ code segments, indicate exactly what will be printed out. All code segments are independent to each other. 1.’ [6 points] [Constant, Type Conversion, Operator Precedence] ‘ _ V4. goo (D 6 1“ 1'13?“ 91°“ 1-2: cout << (il(int)£) << ondl; ' z flmmm] (fl cout << (1‘ 9s 3 [H05 — 2 > 1 7 9 + 3 .‘g = a I 3) << .ndh 1 ~mm g cout << {(H 5 << 1) r. 9) << 2) >> 1) << ondl: ' a, 30‘ V nnufr \000 toovl 2. [6 points] [Function Template] [Type Conversion] togplato <clnss 'r> '1' cub-('1' 1H roturn i * :L * i; } nn1n(){ int 1 I 2: float I - 3; _‘ «U 1 [3L double «1 - 4; F .-J b cout << (cub.(i) << 2) << audit; (9 lg 5 com: << (cubo(£)0\/ 2) << ondl: (2] ‘1‘ cout << ((2%) (cubed) + 0.1) 9s 5) << audit: 1 *nggq$.L\ 3. [4 points] [Control Structure, Reference Variable] int: i I 5: JR?) int 5 j - 1; :K3 in: k - l/déA ._ LP )H- + i i ‘ 1. 1--1 . } ' {biggiggj > 3); (1 2 cont: <<makh<7g until: 2 cout << :|. << .1141; U 4. [14 points] [Variable Scope, Default Argument, Function Overloading] Note: This program is stored in four separate files. lli'il. 1 #includo <ioutronm.h> int 1 - int £(1nt is. int I 2): in: {(unlignod - 3): int £(chnr): Wilcout << £(:::I.) << anal: {$9 '6 cout << £(:l., ::1) << anal: unlined 1 - 5; Hi I 6: 62% cout << Hi.) << cndl; char c - ‘7': {6) 11-] com: << 1(a) << until: 0 7 cont. << ::1 << ondl: } 22 cout << fl) << undl; (élo cont. << 1(1) << ondl: //r11. 3 ext-rs: int: i; P‘ 1:11: “mega, 1m: 3” 14-4-3 j++1 ::1++: roturn 1 + j + H1: 1_®5 5 Mr. l/l'ilo 3 static in: :L int “unsigncd 15 1“: 11-4-3 :31 4+; raturn :L + :::l. * 2: 1 ~ We [U _ QR ,‘ lll'ilo t oxtcrn int 1; int £(chlr c){ . :::|.++; it (cu-‘9') return Hi I 2; 01-. return 3:1 * 2: 111. Code Rewriting [10 points] 4: g 1. [5 points] [Control Structures] k Rewrite the following C++ code fragment with the while structure. for-(i - 0: i < :1: i++){ 11(1 > k} continua: a 01.. it“. < k) broak: 01:01.- 1’ } 1°n+ :- 0; * whilelc'znl phi“) E- "F‘ ‘rn 1“} ,‘H + 3 I f 7’3 L’[ Ili‘t‘flt(§n+'r”lfif’3} 2 r ‘r PM: ,‘L’ “1).; ) br'fi‘mK'J Else MIA-.2 3 + . v; L + j 3 I 2. [5 points] [Control Structures] ‘ Rewrite the following C++ code fragment with the it: 1.1-. étructure. L1,...“ .r . 11: 13(1)! ’ t .'CI.CII 1: can. 2: C [ls-1) L3: 2) j-I: NIH brook; 'J can. 3: ?’59 J‘F k I b: ca..- 4.: n - c: bro-k1 dot-.111: : “€3,512 :1 I d; break) false 1 __________________________ n: W52 2 , in» n r) '/~ V4” ; ‘- » woo . _ flw 31.00 -/-J@. = o Vl/m /. H): 3 IV. Programming [40 points] V0.3? V~ n/mv y, m 5 Li Write C++ codes for each of the following problems: I) / W U 1 {U : 5 Jo 1. [12 points] [Integer Manipulation, Program Assignment II a. [6 points] Write a function 1m: Iuantunt n): which returns the sum of aLthLdigils of its only parameter :1, a positive integer of arbitrary length of digits. in+ sombg-ttm’l’ fl) iirrl- 5Um=03w , . ‘ 40¢ (marl 5px}, ) Hum} ‘ Sum+:(YJ/E)'/—l03 reflm Sdm'J \/ 5 b. [6 points] Write a function in: mgictim: n): which returns the magic g' number of its only parameter I: (a positive integer) using method I (i.e. keep adding up the digits of the number or sums until you get a single- digit sum). (You may assume that in: Iuantunt n) 1, as specified in La is already available for you to use when you write Lb.) jn-l- moflidmrr n) 5 while (Sumdtqwch) 2 lo) h: (Um&ik5i+{n); FE+Urn g 1.0/1); 3 2. [3 points] [Unix] A CS31 student just finished writing a C++ program and saved it into a file named pained in his/her SEAS—net account, how should s/he l). compile, 2). run and 3). submit his/her program? . _ typhoon) 3H- pal. CC 3 [2/ {Whom}? (Loui' (3) {’thoun) {alumni (5'34 mime 3. [10 points] [Binary Search, Recursive Function] Consider the number guessing game we talked about in the class in which one is asked to guess a secret code randomly picked from a list of numbers ranged from n to n (inclusively). We mentioned a binary-search algorithm which is very efficient in terms of minimizing the average number of guesses. Write a recursive function void binauuuine n, in: n) 1 to implement that algorithm. You may assume that there is an integer function in: ropzyune g); available which, upon the guess 3, returns 1 if g is lee, returns Wrflums 0 when it is the same. Your function should print outfiall it guesses un__til______it gets the secret code at which point it should print mat—codé’and stop. It should do this in at most 103: (n - n) + 1 guesses. Void). bin GueSS( fn‘l‘ m {n‘f n) W 9::- W“ i H3 (Jock/(fl) :2: o} FED/W “(m-t dc ” Ra‘sh‘l “ (a. Pram-J +0 EI‘t’flrwn ') Paphxh m I WWW} I H m reply“ “1155;— a) { W9 2 \- bffiGUBSSZ m) a —-| j \ Wax ir'sgfri': 1 l (rant/0(a) ___ : __0 I‘m" m‘wictllf,‘S K.r~ 1"" LD—nb" “3', “1/; while, (gm § 1 U€S§( 3-H ) fl) 3 “Mater: m+h . 2 NW) ’ WWW. reply/9,) +0 m+n 2 4. [15 points] [Control Structure, Program Tracing, Recursive Function] We have seen a bubble sort algorithm in class. Here is another version: void bacrtunt at], int n){ fox-(int :L I O: i < :13— 1: i++) forum: :I - n3- 1; :l >l1; i") 21/?“ iEUIU-ll > IIUI) lwtpflU-lla IUD: J ’ 6m mi“; M3] (7. i l '9 a. [6 points] Assuming a is defined as int all] . um], 5, 2, 1);, specify the mumps among elements in the array a when hacrtu. 4) That is write down all the values of the elements in a each time w the function swap is calle : b. [9 points] Write a recursive function void bubsortunt It] . in: n) , V which sorts the first n elements of the array stored in a. You may assume Viol \ UP the function "up if available for you to use. - 0 Lb‘ - Ci Voitil b§or+ (in-i- qL’L ,‘n-/- n) 9 5:4 (h 2: o OWN r9+vrfl a! (Wind/S ) 42“" (W [=03 L4 n—J 3 6M) \ tr‘zi'il ‘ \ wammmm (am a! 'H‘ so b5oP+(q) 0-03 ’ P. J} [L J \ fl 3 J ...
View Full Document

Page1 / 8

COM SCI 31 Fall - 1998 Midterm - UCLA CS 31 Midterm Winter,...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online