






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







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 }
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 }
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
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
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 }
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 }