Docsity

C++ Functions Lecture Questions: Part II - Prof. Keith E. Hellman, Exams of Computer Science

Toggle sidebar
bg1
CSCI261 Lecture Questions C++ Functions
Part II
This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a
useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in
general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be
happy to review your answers with you during office-hours, via Email, or instant messaging.
1. Consider the snippet of code below, what does it print out to the console?
1
# in c lu de < io st re am >
2
using namespace std;
3
4
in t le f t_t o _ rig h t ( int & x, int y , in t z )
5
{
6
z = y = x;
7
return y*x;
8
}
9
10
in t ri g ht_ t o _le f t ( int & x, int y , in t z )
11
{
12
x = y;
13
y = z;
14
return y/z;
15
}
16
17
vo id d i spl a y ( i n t x , int y , i nt z )
18
{
19
co ut < < " x = " < < x < < " " ;
20
co ut < < " y = " < < y < < " " ;
21
co ut << "z = " << z << end l ;
22
}
23
24
int main()
25
{
26
in t x (8 ) , y (9) , z(x);
27
28
di s p lay ( x, y , z );
29
30
z = left_to_right ( y, x, z );
31
di s p lay ( x, y , z );
32
33
y = right_to_left ( z, x, y );
34
di s p lay ( x, y , z );
35
36
x = left_to_right ( y, x, z );
37
di s p lay ( x, y , z );
38
39
return 0;
40
}
Solution:
x=8 y=9 z=8
x=8 y=9 z=81
x=8 y=1 z=8
x=1 y=1 z=8
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Functions Lecture Questions: Part II - Prof. Keith E. Hellman and more Exams Computer Science in PDF only on Docsity!

CSCI261 Lecture Questions

Part II

This worksheet is for your use during and after lecture. It will not be collected or graded, but I think you will find it a

useful tool as you learn C++ and study for the exams. Explain all false answers for the “True or False” questions; in

general, show enough work and provide enough explanation so that this sheet is a useful pre-exam review. I will be

happy to review your answers with you during office-hours, via Email, or instant messaging.

1. Consider the snippet of code below, what does it print out to the console?

1 # include < iostream > 2 using namespace std ; 3 4 int left_to_right ( int & x , int y , int z ) 5 { 6 z = y = x; 7 return y *x ; 8 } 9 10 int right_to_left ( int & x , int y , int z ) 11 { 12 x = y ; 13 y = z ; 14 return y /z ; 15 } 16 17 void display ( int x , int y , int z ) 18 { 19 cout << "x = " << x << " " ; 20 cout << "y = " << y << " " ; 21 cout << "z = " << z << endl ; 22 } 23 24 int main () 25 { 26 int x (8) , y (9) , z ( x ); 27 28 display ( x , y , z ); 29 30 z = left_to_right ( y , x , z ); 31 display ( x , y , z ); 32 33 y = right_to_left ( z , x , y ); 34 display ( x , y , z ); 35 36 x = left_to_right ( y , x , z ); 37 display ( x , y , z ); 38 39 return 0; 40 }

Solution:

x=8 y=9 z=

x=8 y=9 z=

x=8 y=1 z=

x=1 y=1 z=

CSCI261 Lecture Questions

Part II

2. Consider the source listing at the right.

(a) How many times is line 14 traversed by the CPU?

Solution: Line 14 is in function g. g is called

once, its input from the return value of the first

call to f, and g’s return value is provided as input

to the second call of f. Since g is called once,

and line 14 is not in a looping structure, line 14 is

executed once by the CPU.

(b) What are the values of B just before line 8 is executed

the first time?

A. the value − 1

B. the value 0 ⇐ answer

C. the value of variable a

D. “garbage”, or an unknown value

(c) How many times is line 8 traversed by the CPU?

Solution: Line 8 is in function f. As described in

the solution to part a, f is called twice. Since line

8 is not in a looping structure, line 8 is executed

twice by the CPU.

(d) What is the value of B printed to the console when the

program ends?

A. the value − 1

B. the value 0

C. the value 1

D. the value 2 ⇐ answer

E. more than 3

F. the value of variable a

G. “garbage”, or an unknown value

1 # include < iostream > 2 using namespace std ; 3 4 int B (0); 5 6 int f ( int a ) 7 { 8 B = a ; 9 return a ; 10 } 11 12 int g ( int a ) 13 { 14 a += B ; 15 return -a; 16 } 17 18 int main () 19 { 20 f ( g ( f ( -1 ) ) ); 21 cout << "B = " << B << endl ; 22 return 0; 23 }

3. Match the variable storage and scope types at the left to their singular description on the right.

A auto or “local” variables ( 1 )

B Global variable ( 3 )

1. Variables declared on the stack each time a function is called.

2. Variables held in main memory that guarantee “1-cycle” manipulations

by CPUs.

3. Variables available to all functions in a source file.

4. Variables representing a complex number a + b i.

The remaining worksheet questions ask you to write several different functions, plenty of

additional blank pages have been provided for your work.

4. Write a function named inSquare that returns a Boolean value indicating whether a point (given by two double

coordinates ( x , y )) is within a square with one corner at ( a , b ) and the opposite corner at ( c , d ). x , y , a , b , c , and d

should be parameters to the function.

5. Write a function named pointDistance that takes two points specified by ( x , y ) and ( u , v ) and returns the distance

CSCI261 Lecture Questions

Part II

“heads” or “tails”. “Same” wins if both frisbees land in the same orientation (both rim up, or both rim down);

“different” wins if they land in different orientations.

Suppose an Evil Captain uses a specially selected unfair disc that lands “up” 75% of the time. If the Honest

Captain uses a fair disc, should she call “same” or “different” to win first possession — or does it not matter?

Write a main() routine that uses outOf100() and queries the user for the bias of the Evil Captain’s unfair disc.

The program should run 10,000 simulated frisbee flips to test your answer.

CSCI261 Lecture Questions

Part II

—Solutions to the Geometry Questions—

2 ∗ S o l u t i o n s f o r w o r k s h e e t I I g e o m e t r y p r o b l e m s 3 ∗ / 4 5 # include < cmath > 6 using namespace std ; 7 8 9 / ∗ ∗∗ 10 ∗ r e t u r n s t r u e i f p o i n t ( x , y ) i s i n s i d e t h e s q u a r e bounded 11 ∗ by ( a , b ) and ( c , d ) 12 ∗ / 13 bool inSquare ( double x , double y , 14 double a , double b , double c , double d ) 15 { 16 / ∗ ∗∗ 17 ∗ we a r e n o t g a u r a n t e e d w hat t h e r e l a t i v e o r i e n t a t i o n 18 ∗ o f ( a , b ) and ( c , d ) a r e. We c an ’ t assume ( a , b ) i s t h e 19 ∗ l o w e r l e f t and ( c , d ) i s t h e u p p e r r i g h t! 20 ∗ / 21 / / i f x i s < = t h e s m a l l e r o f a and c , t h e p o i n t i s o u t s i d e 22 if ( a < c ) { 23 if ( x <= a ) { 24 return false ; 25 } else { 26 if ( x < c ) { 27 return false ; 28 } 29 } 30 } 31 32 / ∗ ∗∗ 33 ∗ t h a t i s t h e l o n g − w i n d e d way t o w r i t e t h e l o g i c. now I ’ l l show y ou t h e 34 ∗ t e r n a r y o p e r a t o r and how i t g r e a t l y r e d u c e s t h e t y p i n g r e q u i r e d. i f 35 ∗ need be , r e v i e w t h e t e r n a r y o p e r a t o r f r o m t h e l e c t u r e s l i d e s o r t h e 36 ∗ book! 37 ∗ / 38 39 / / i f x i s > = t h e g r e a t e r o f a and c , t h e p o i n t i s o u t s i d e 40 / / The a > c ?a : c makes s u r e x i s c om pared a g a i n s t t h e l a r g e r o f a o r c 41 / / NOTE : we m us t us e p a r e n s! 42 if ( x >= (a > c? a: c ) ) return false ; 43 44 / / now t h e same l o g i c i n t h e y d i r e c t i o n 45 if ( x <= (b < d? b: d ) ) return false ; 46 if ( x >= (b > d? b: d ) ) return false ; 47 48 / / i f we a r e t h i s f a r , t h e p o i n t i s i n t h e c i r c l e 49 return true ; 50 } 51 52

CSCI261 Lecture Questions

Part II

—Solutions to the Probability Questions—

2 ∗ S o l u t i o n s t o w o r k s h e e t I I p r o b a b i l i t y q u e s t i o n s 3 ∗ / 4 5 # include < iostream > 6 # include < cstdlib > 7 # include < ctime > 8 9 using namespace std ; 10 11 / ∗ ∗∗ 12 ∗ r e t u r n s t r u e p e r c e n t / 1 0 0 t i m e s on a v e r a g e 13 ∗ / 14 bool outOf100 ( double percent ) 15 { 16 return rand () % 100 < percent ; 17 } 18 19 20 / ∗ ∗∗ 21 ∗ s i m u l a t e s a l o t o f c o i n t o s s e s w i t h o u t o u t p u t 22 ∗ t o c o u t. b i a s i s t h e c o i n ’ s b i a s f o r f l i p p i n g heads , 23 ∗ i t s h o u l d be bet w een 0 and 100 i n c l u s i v e. 24 ∗ / 25 void flips_managed ( double bias ) 26 { 27 const int TRIALS ( 10000 ); 28 const int OUTPUT_INTERVAL ( 100 ); 29 int longest_heads _ st re ak (0) , longest_tails _s tr ea k (0); 30 int current_heads _ st re ak (0) , current_tails _s tr ea k (0); 31 int heads (0); 32 bool heads_this_flip ; 33 34 for ( int t (0); t < TRIALS ; t ++ ) { 35 36 heads_this_flip = outOf100 ( bias ); 37 38 / / s i n c e a b o o l t r u e i s 1 and f a l s e i s 0 , we c an do : 39 heads += heads_this_flip ; 40 41 if ( heads_this_flip ) { 42 / / d i d we j u s t f i n i s h a l o n g s t r e a k o f t a i l s? 43 if ( current_tails _s tr ea k > longest_tails _s tr ea k ) { 44 longest_tails s tr ea k = current_tails s tr ea k ; 45 } 46 current_tails st re ak = 0; 47 current_heads st re ak ++; 48 } else { 49 / / d i d we j u s t f i n i s h a l o n g s t r e a k o f heads? 50 if ( current_heads _s tr ea k > longest_heads _s tr ea k ) { 51 longest_heads s tr ea k = current_heads s tr ea k ; 52 } 53 current_heads st re ak = 0; 54 current_tails st re ak ++; 55 } 56

CSCI261 Lecture Questions

Part II

57 / / o u t p u t e v e r y OUTPUT INTERVAL t i m e s −−− o u r c o u n t e r i s 58 / / z e r o based , so we add one ( when == 9 9 , we ’ ve f l i p p p e d 100 t i m e s ) 59 if ( (t +1) % OUTPUT_INTERVAL == 0 ) { 60 cout << " heads " << heads << " " ; 61 cout << " tails " << t +1 - heads << " "; 62 cout << " total " << t +1 << " "; 63 cout << " longest heads " << longest_heads_ s tr ea k << " " ; 64 cout << " longest tails " << longest_tails_ s tr ea k << " " ; 65 cout << endl ; 66 } 67 } 68 } 69 70 71 / ∗ ∗∗ 72 ∗ u l t i m a t e f r i s b e e f l i p s i m u l a t i o n 73 ∗ / 74 int main () 75 { 76 / / s eed t h e random number g e n e r a t o r w i t h t h e c l o c k 77 srand ( time (0) ); 78 79 double evil_bias ; 80 do { 81 cout << " Enter the evil_bias of the Evil Captain ’s disc [0 -100]: " ; 82 cout << flush ; 83 cin >> evil_bias ; 84 } while ( evil_bias < 0 || evil_bias > 100 ); 85 86 const double FAIR_BIAS ( 50.0 ); 87 const int FLIPS ( 10000 ); 88 89 int same (0) , diff (0); / / c o u n t e r s o r number o f same and d i f f e r e n t f l i p s 90 91 for ( int f (0); f < FLIPS ; f ++ ) { 92 if ( outOf100 ( FAIR_BIAS ) == outOf100 ( evil_bias ) ) { 93 same ++; 94 } else { 95 diff ++; 96 } 97 } 98 99 cout << " After " << FLIPS << " flips , same = " << same ; 100 cout << " and diff = " << diff << endl ; 101 102 return 0; 103 }

CSCI261 Lecture Questions

Part II

54 heads_this_flip = outOf100 ( bias ); 55 56 / / s i n c e a b o o l t r u e i s 1 and f a l s e i s 0 , we c an do : 57 heads += heads_this_flip ; 58 59 / / manage t h e s t r e a k c o u n t e r l o g i c 60 if ( heads_this_flip ) { 61 managed_streaks ( current_heads_streak , 62 current_tails_streak , longest_tails_ st re ak ); 63 } else { 64 managed_streaks ( current_tails_streak , 65 current_heads_streak , longest_heads_ st re ak ); 66 } 67 68 / / o u t p u t e v e r y OUTPUT INTERVAL t i m e s −−− o u r c o u n t e r i s 69 / / z e r o based , so we add one ( when == 9 9 , we ’ ve f l i p p p e d 100 t i m e s ) 70 if ( (t +1) % OUTPUT_INTERVAL == 0 ) { 71 cout << " heads " << heads << " " ; 72 cout << " tails " << t +1 - heads << " "; 73 cout << " total " << t +1 << " "; 74 cout << " longest heads " << longest_heads_ s tr ea k << " " ; 75 cout << " longest tails " << longest_tails_ s tr ea k << " " ; 76 cout << endl ; 77 } 78 } 79 } 80 81 82 / ∗ ∗∗ 83 ∗ t e s t managed f l i p s l o g i c 84 ∗ / 85 int main () 86 { 87 / / s eed t h e random number g e n e r a t o r w i t h t h e c l o c k 88 srand ( time (0) ); 89 90 managed_flips ( 50.0 ); 91 }