This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 Engineering 101 C++ Basics: Programs and Expressions Quote of the Day  Confucius The people may be made to follow a path of action, but they may not be made to understand it. How is data sent to/from the caller? n Passbyvalue copies the data from the caller to a new location in the algorithm. n When passbyvalue is used to send data back to the caller, this is called a return. n The caller has the option to ignore this kind of data. n Passbyreference gives direct access to the data in the callers environment. n The caller cannot ignore passbyreference data send to it since the data was directly changed. Consider the Remainder Algorithm Remainder 1: Request a value for x 2: Request a value for y 3: while x y do 4: assign xy to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A x y Callbyvalue Callbyreference 20 7 20 7 a b a b Consider the Remainder Algorithm Remainder 1: Request a value for x 2: Request a value for y 3: while x y do 4: assign xy to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 20 x y Callbyvalue Callbyreference 20 7 20 7 a b a b Consider the Remainder Algorithm Remainder 1: Request a value for x 2: Request a value for y 3: while x y do 4: assign xy to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 20 7 x y Callbyvalue Callbyreference 20 7 20 7 a b a b 2 Consider the Remainder Algorithm Remainder 1: Request a value for x 2: Request a value for y 3: while x y do 4: assign xy to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 13 7 x y Callbyvalue Callbyreference 13 7 20 7 a b a b Consider the Remainder Algorithm Remainder 1: Request a value for x 2: Request a value for y 3: while x y do 4: assign xy to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 6 7 x y Callbyvalue Callbyreference 6 7 20 7 a b a b Consider the Remainder Algorithm Remainder 1: Request a value for x 2: Request a value for y 3: while x y do 4: assign xy to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 6 7 x y Callbyvalue Callbyreference 6 7 20 7 6 6 a b output a b output Caller Algorithm A x y Caller Algorithm A 6 7 x y Callbyvalue Callbyreference 6 7 20 7 6 6 a b a b Callbyreference can cause changes in the caller aside from returning output explicitly. This output could be ignored. This output cannot be ignored. What does the algorithm do? Mystery Algorithm n Request a value for a n Request a value for b n if a b then assign b to c n if a < b then assign a to c n while (remainder( a , c ) 0 OR remainder( b , c ) 0) n subtract one from c n return c n end remainder( x , y ) 1. Request a value for x 2. Request a value for y 3. while x y do 4. assign x  y to x 5. return x 6. end a) Computes the Least Common Denominator of 2 fractions b) Computes the Largest Common Prime of 2 numbers c) Computes the Greatest Common Factor of 2 numbers All Algorithms Utilize 3 Primary Tools n Sequence n Selection...
View Full
Document
 Winter '07
 Ringenberg
 Scaffolding

Click to edit the document details