Date Assigned: Monday, February 18, or Tuesday, February 19, 2008.
Due Date: Monday, February 25, or Tuesday, February 26, 2008, before the beginning of your lab section.
Objective: The main objective of this assignment is to practice using loops.
Programming assignment: When we use integers for our arithmetic calculations, the results are exact. This may not be true when using real numbers because of the way they are represented in the computer. Real numbers are represented approximately by using a finite number of digits. Because of the approximate nature of computer representations, the result of computations with real numbers is also only approximate. In particular, the computed value of a sum of several real numbers may depend on the order in which the computer adds these numbers, especially if the numbers differ greatly in magnitude. This dependence is a particular case of what is called cancellation error.
Part 1. Write a program that computes the sum of the following series, first computing from left to right, and then from right to left:
1 + 1/2+ 1/3 + 1/4 + 1/5 ……+ 1/n
For n = 3, display both results and the difference between them. If you notice that there is no difference, increase n to 9 and execute the program again. If still no difference appears, increase n to 15.
Part 2. Determine the value of n for which the difference between the two sums is greater than or equal to 10-17. Modify the program you wrote for Part 1 by adding the code necessary to repeat the whole process using an outer loop, checking the two sums each time to determine if the difference is large enough. Start with n = 3 and increase by one each time until the difference becomes large enough. (Hint: Use Math.abs() to obtain the absolute value of the difference.) When the difference is large enough, display the following statements as output:
“The sum from 1 to 1/ n is: ” (substitute the final value of n and fill in the sum computed left to right)
“The sum from 1/ n to 1 is: ” (substitute the final value of n and fill in the sum computed right to left)
“The difference between the sums is: ” (fill in the absolute value of the difference)
Homework assignment: on a separate sheet of paper, solve Ex. 6, 10, 18, and 24 at the end of Chapter 5.
Deliverables: as instructed by your TA.