02 - C++ Basics, Programs, and Expressions

02 - C++ Basics, Programs, and Expressions - 1 Engineering...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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 Pass-by-value copies the data from the caller to a new location in the algorithm. n When pass-by-value 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 Pass-by-reference gives direct access to the data in the callers environment. n The caller cannot ignore pass-by-reference 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 x-y to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A x y Call-by-value Call-by-reference 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 x-y to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 20 x y Call-by-value Call-by-reference 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 x-y to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 20 7 x y Call-by-value Call-by-reference 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 x-y to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 13 7 x y Call-by-value Call-by-reference 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 x-y to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 6 7 x y Call-by-value Call-by-reference 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 x-y to x 5: return x 6: end Caller Algorithm A x y Caller Algorithm A 6 7 x y Call-by-value Call-by-reference 6 7 20 7 6 6 a b output a b output Caller Algorithm A x y Caller Algorithm A 6 7 x y Call-by-value Call-by-reference 6 7 20 7 6 6 a b a b Call-by-reference 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

Page1 / 13

02 - C++ Basics, Programs, and Expressions - 1 Engineering...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online