02 - C++ Basics, Programs, and Expressions

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

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
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 caller’s 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

## This note was uploaded on 01/30/2012 for the course ENGR 101 taught by Professor Ringenberg during the Winter '07 term at University of Michigan.

### 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
Ask a homework question - tutors are online