Chap5

# Chap5 - Chapter 5 Abstraction 5.1 Computing Square Roots...

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

Chapter 5 Abstraction 5.1 Computing Square Roots Packaging an Idea 5.2 Ellipse Perimeter Clarity versus E ciency 5.3 The Betsy Ross Problem Design Parameters and Hierarchy There are a number of reasons why the built-in sin function is so handy. To begin with, it enables us to compute sines without having a clue about the method used. Thedes igno fanaccurateande cient sine function is somewhat involved but by taking the “black box” approach, we are able to be e f ective sin -users while being blissfully unaware of how the built-in function works. All we need to know is that sin expects a real input value and that it returns the sine of that value interpreted in radians. Another advantage of sin can be measured in keystrokes and program read- ability. Instead of disrupting the “real business” of a program with lengthy compute- the-sine fragments, we merely invoke sin as required. The resulting program is shorter and reads more like traditional mathematics. Being able to write e f ective functions is central to the problem solving process. It supports the top-down methodology, enabling us to abstract away lower-level details why we work on higher-level software design issues. We start out in § 5.1 developing a function for computing square roots that is based on a rectangle averaging process. The fact that sqrt is a built-in function gives us a standard against which we can measure the quality of our implemen- tation. In § 5.2 we deal with a situation where there are two reasonable function- writing possibilities. The dilemma is resolved by looking at some e ciency issues. The programming strategy known as “top-down development is presented in § 5.3 where we derive a graphics function that displays a colonial f ag. It relies on two other functions and by carefully sequencing the writing of these procedures we can e f ectively deal with the overall complexity. 1

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

View Full Document
2 Chapter 5. Abstraction 5.1 Computing Square Roots Problem Statement The act of computing the square root of a positive number A is the act of “building” a square with area A . Thinking along these geometric lines, we make a pair of observations. First, if a square with side A has the same area as an L -by- W rectangle, then A is in between L and W . See Figure 5.1. Second, we can make Figure 5.1. A Square and Rectangle with the Same Area any given rectangle “more square” by replacing L with L new =( L + W ) / 2and W with W new = A/L new . The process can obviously be repeated. See Figure 5.2. Write a function s = MySqrt(A) that produces an estimate of A by repeating the rectangle averaging process a su cient number of times. The relative error in the value MySqrt(A) should be less than 10 12 , i.e., MySqrt(A) should have 12 correct signi f cant digits.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/30/2008 for the course CS 100 taught by Professor Fan/vanloan during the Spring '07 term at Cornell.

### Page1 / 24

Chap5 - Chapter 5 Abstraction 5.1 Computing Square Roots...

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

View Full Document
Ask a homework question - tutors are online