Hands On Section 2.3 - 2.5

1. Tonnage: A metric ton is 35,273.92 ounces. Write a program that will read the weight of a package of breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield 1 metric ton of cereal. Your program should allow the user to repeat this calculation as often as the user wishes.

1b. Make sure the indenting of the code follows the Style rule in 2.5, also here.

1c.. Review the quality of the variable names.

2. Square Root: The Babylonian algorithm to compute the square root of a number n is as follows:

  1. Make a guess at the answer (you can pick n/2 as your initial guess).
  2. Compute r = n / guess
  3. Set guess = (guess + r) / 2
  4. Go back to step 2 for as many iterations as necessary. The more that steps 2 and 3 are repeated, the closer guess will become to the square root of n.
Write a program that inputs a double for n and iterates through the Babylonian algorithm 100 times. For a more challenging version, iterate until guess is within 1% of the previous guess, and outputs the answer as a double .
Topic attachments
I Attachment History Action Size Date Who Comment
PowerPointppt ch2.3-5.ppt r1 manage 2068.0 K 2017-01-26 - 03:00 JimSkon  
Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r3 - 2017-09-07 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback