Suppose that you want to construct a circular pond in which you want to keep a number of fish. Having looked into the matter, you know that you must allow 2 square feet of surface area on the pond for every 6 inches of fish length. You need to figure out the diameter of the pond that will keep the fish happy. Here’s how you can do it: // Program 2.6 Sizing a pond for happy fish #include int main() { const double fish_factor = 2.0/0.5; // Area per unit length of fish const double inches_per_foot = 12.0; const double pi = 3.14159265; double fish_count = 0.0; // Number of fish double fish_length = 0.0; // Average length of fish cout << "Enter the number of fish you want to keep: "; cin >> fish_count; cout << "Enter the average fish length in inches: "; cin >> fish_length; fish_length = fish_length/inches_per_foot; // Convert to feet // Calculate the required surface area double pond_area = fish_count * fish_length * fish_factor; // Calculate the pond diameter from the area double pond_diameter = 2.0 * sqrt(pond_area/pi); cout << "nPond diameter required for " << fish_count << " fish is " << pond_diameter << " feet.n"; return 0; } (Continued) With input values of 20 fish with an average length of 9 inches, this example produces the following output: Enter the number of fish you want to keep: 20 Enter the average fish length in inches: 9 Pond diameter required for 20 fish is 8.74039 feet. You first declare threeconstvariables that you’ll use in the calculation: const double fish_factor = 2.0/0.5; // Area per unit length of fish const double inches_per_foot = 12.0; const double pi = 3.14159265; Notice the use of a constant expression to specify the value forfish_factor. You can use any expression that produces a result of the appropriate type to define an initializing value for a variable. You have declaredfish_factor,inches_per_foot, andpiasconstbecause you don’t want to allow them to be altered. Next, you declare variables in which you’ll store the user input: double fish_count = 0.0; // Number of fish double fish_length = 0.0; // Average length of fish You don’t have to initialize these, but it’s good practice to do so. Because the input for the fish length is in inches, you need to convert it to feet before you use it in the calculation for the pond: fish_length = fish_length/inches_per_foot; // Convert to feet This stores the converted value back in the original variable. You get the required area for the pond with the following statement: double pond_area = fish_count * fish_length * fish_factor; The product offish_countandfish_lengthgives the total length of all the fish, and multiplying this byfish_factorgives the required area. The area of any circle is given by the formula p r2, where r is the radius. You can therefore calculate the radius of the pond as the square root of the area divided by p. The diameter is then twice the radius, and the whole calculation is carried out by this statement: pond_diameter = 2.0 * sqrt(pond_area / pi); 74 You obtain the square root using a function that’s declared in the standard header The last step before exitingmain()is to output the result: cout << "nPond diameter required for " << fish_count << " fish is" << pond_diameter << " feet.n"; This outputs the pond diameter required for the number of fish specified. Working with Floating-Point Values For most computations using floating-point values, you’ll find that typedoubleis more than adequate. However, you need to be aware of the limitations and pitfalls of working with floating-point variables. If you’re not careful, your results may be inaccurate, or even incorrect. The following are common sources of errors when using floating-point values:
Let’s see how these errors can manifest themselves in practice, albeit in a somewhat artificial situation. Try It Out: Errors in Floating-Point CalculationsHere’s an example contrived to illustrate how the first two points can combine to produce errors: // Program 2.7 Floating point errors
The value displayed should be zero, but on my computer this program produces the following: 7.45058e-009 The reason for the error is that none of the numerical values is stored exactly. If you add code to output the values ofvalue1andvalue2after they’ve been modified, you should see a discrepancy between them. Of course, the final difference between the values ofvalue1andvalue2is a very small number, but you could be using this totally spurious value in other calculations in which the error could be amplified. If you multiply this result by 1010, say, you’ll get an answer around 7.45, when the result should really be zero. Similarly, if you compare these two values, expecting them to be equal, you don’t get the result you expect. CAUTION Never rely on an exact floating-point representation of a decimal value in your program code. Tweaking the Output The previous program outputs the floating-point value in a very sensible fashion. It gave you 5 decimal places, and it used scientific notation (that is, a mantissa and an exponent). However, you could have chosen to have the output displayed using “normal” decimal notation by employing some more output manipulators.
blog comments powered by Disqus | |||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|