CSc 17 Test 1 Wednesday 8 October 1997 ANSWERS 1. (15 pts) This is a true-false question. After each expression state whether the expression evaluates to true or to false. ( 3/2 == (2/3+1)) TRUE (('a'>'b') || ( ('b'-'a')>0 ) ) TRUE (('a'+int(true))>'a') TRUE ((3/2.0) == (2/3.0 + 1)) FALSE ((17%3+1) > (3%17)) FALSE ( ((2>3)==(3>2)) == ( (4-3>0)==(10<4) ) ) TRUE ('0' == int('0') ) TRUE ( 'T'-'t' + 'q'-'Q' >0) FALSE ('t'-'r'+true == 3) TRUE ( !(4==5) == !(7>5) ); FALSE 2. (10 pts) Given the function and variable declarations below, state what output is generated by the code below the dashed line void a(int &x,int &y,int z) { if (x==y) {if (x>z) x++; z++;} else {x++; y++;} cout << " "<< x <<" "<< y <<" "<< z; return; } ----------------------------------------------------------------- int x,y,z; x=4; y=3; z=5; a(x,y,z); 5 4 5 cout <<" "<< x << " " << z; 5 5 x=4; y=3; a(x,x,y); 5 5 4 cout <<" "<< x << " " << y; 5 3 3. (15 pts) Find 10 syntax errors in the following code: #include << iostream.h misspelled a(int b); int c << missing semi-colon main << missing parentheses {float d; cin << d; << cin does not understand if a(d,8) << missing parentheses << too many parameters d=-d << missing semi-colon cout << d;} << missing return int a(int b) {b=d; << d is inaccessible to a return;} << must return a value 4. (30 pts) Given the declaration int list[20],count write a function called 'powers' such that the call powers(list,count) lists, in reverse order of entry in list, three vertical columns, the first column consisting of the values (in reverse order) in the list, the second column consisting of the squares of the values, and the third column consisting of the cubes of the values. The function assumes that count stores the number of entries in the array. Thus, if count=3, and the first three entries are 2, 8 3, then the output would look something like 3 9 27 8 64 512 2 4 8 void powers(int list[],int count) { int k; while (count>0) {count--; cout << list[count] << " " << list[count]*list[count] << " " << list[count]*list[count]*list[count] << endl; } return; } 5. (30 pts) Write a function called large such that for floats x and y large(x,y) will either (a) swap x and y if x is larger than y and return a value of true (b) leave x and y alone if x is not larger than y and return a value of false. Thus, if we have float x=5,y=3 then the call large(x,y) will return a value of true and x will be 3 and y will be 5. If we have float x=13,y=15 then the call large(x,y) will return false and x will still be 13 and y will still be 15. bool large(float &x, float &y) { float temp; if (x>y) {temp=x; x=y; y=temp; return true; } else return false; }