CSc 17 Test 1
Friday 2 October 1998
Page 1
>>>>>>>>>>>>>>>>>>>>>>>ANSWERS<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1. Assume that we have #include<math.h>. Explain why the segment
of code in (a) causes a runtime error (i.e., crashes) and why the
segment of code in (b) executes without a runtime error (i.e., does
not crash).
(4 pts)
(a) x=5;
if (sqrt(x)>1  x<0)
cout<<x;
(11 pts) (b) x=5;
if(x<0  sqrt(x)>1)
cout<<x;
(a) Taking the square root of a negative number (DOMAIN error).
(b) The expression "sqrt(x)>1" never gets evaluated, because
the expression "X<0" is true ("Shortcut evaluation")
2.
(10 pts)
Write out the value of each of the following expressions:
(a) 7+6*5/4%3
8
(b) 7+6*5/4.0
14.5
(c) 7.6345int(7.9345)
0.6345
(d) (!(2>3) && (4<5))
true
(e) (
( (2>3) && (4<5) ) == (1==2) )== (4==5)
false
3. (25 pts)
Write a function bigBoy with the following properties.
When
we have the declaration "double x,y;" the call bigBoy(x,y) returns the
smaller of x and y, and after the call both x and y contain the larger
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 varies
 United States Declaration of Independence, Following, Negative and nonnegative numbers, The Declaration, Run time

Click to edit the document details