10+-+Variables%2C+Scope%2C+and+Lifetime+-+Full

10+-+Variables%2C+Scope%2C+and+Lifetime+-+Full -...

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

View Full Document Right Arrow Icon
Engineering 101 Names:  Variables, Scope, and  Lifetime
Background image of page 1

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

View Full DocumentRight Arrow Icon
Quote of the Day - Confucius Our greatest glory is not in never falling, but in getting up every time we do.  
Background image of page 2
A Riddle… n Suppose you have 7 stacks of 100 coins each where each stack  is composed entirely of real coins or counterfeit coins: n A regular coin weighs 10 grams and counterfeit coin weighs  11 grams: n Given that you can combine and weigh any combination of  coins from any of the stacks, what is the minimum number of  times that you would need to weigh coins to determine which  stacks contain counterfeit coins?  How would you do this? 10 grams 11 grams
Background image of page 3

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

View Full DocumentRight Arrow Icon
The Answer! ** Courtesy of the Car Talk Puzzler on 4/23/2011 ** n Only  one   weighing is needed!  Heres how you do it: n Take 64 coins from the 1st stack, 32 from the 2nd, 16 from the 3rd, 8  from the 4th, 4 from the 5th, 2 from the 6th, and 1 from 7th.  n Weigh these coins and calculate: overage = weight – 1,270 grams       1311 - 1270        41 n Calculate the binary number that represents the overage and “map” it  onto the stacks to see which stack(s) contributed counterfeit coins and  Take:        64             32              16             8                4               2                1
Background image of page 4
Modifications to the  Usage of Data n Usually when an identifier is declared the  computer sets aside memory in which to store  the associated data. n The memory is then available for use.  It can  be read and altered during the course of the  program. n However, sometimes we may want to treat  certain data specially.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Constants  n If you want a variable to never change in the  context of the code you can define it to be a  const const double speedoflight = 3e8; n Note that const does not replace the type, but  rather it modifies the data type.
Background image of page 6
Aliases (References) n We can declare a new variable that is a stand-in for an  other variable. double v = 5.0; double & w = v; cout << w << endl; n The above code will print 5.0 because v and w are the  same variable. n Note:  References cannot be changed after they are  created (i.e. you can’t make ‘w’ refer to something  else).
Background image of page 7

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

View Full DocumentRight Arrow Icon
Aliases (References) n We have already used such variables implicitly  when we do pass-by-reference. n
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Page1 / 44

10+-+Variables%2C+Scope%2C+and+Lifetime+-+Full -...

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

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