## PIC 10A

(Sec. 3a-3d)

C++ Programming and Problem Solving

## Instructor: Ivo Dinov, Asst. Prof.

 http://www.math.ucla.edu/~dinov/10a.3.01s/

Due Date:

# Monday, 8:00 PM, May 07, 2001

Please, submit your homework electronically using the "submit" system. Please try to follow the "good programming" conventions we discussed in class. Write modular code and include all of your functions in the same CPP file as your main.

• (HW_4_1.cpp/exe/txt) A person invests \$P dollars in a savings account yielding r1 percent monthly interest. Assuming that all interest is left on deposit in the account, calculate and print the amount of money in the account at the end of n months. Write a program that collects the values of P, the initial principal, r1, the monthly interest and n, the number of months and computes A,the final amount in the account. Use the following formula for determining the compound interest: A =P (1 +r1)n, where A is the final amount, P is the initial principal, r1 is the monthly interest and n is the number of months the money is invested. In addition, suppose the same amount of money is invested in a second account at r2 APR (annual percentage rate) interest, r2=r1 . 12. Using the formula for APR interest (A = P + P.r1.n = P + P.r2.n/12) compute the amount in the second account at the end of the n months of investment. Let us assume 0<=n<=12. Entering initial principal of \$0, completes the program. Explain the differences between the two final amounts. Are they the same? Why?
Sample Run:
%> Enter P, the initial principal in dollars (\$) [Enter \$0 to exit]:
%> 100.00
%> Enter r, the monthly interest rate (as double):
%> 0.01
%> Enter n, the number of months the money is invested, (0<=n<=12)
%> 12
%> Final amount, using the compound interest: \$112.68
%> Final amount, using APR interest: \$112.00
%> Enter P, the initial principal in dollars (\$) [Enter \$0 to exit]:
...

• (HW_4_2.cpp/exe) Pythagorean Triples. Consider right triangles that have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the third one (hypotenuse). Write a program that collects 3 integers form the user and tests/reports whether the numbers form a Pythagorean Triple. The program should run iteratively until the user enters 0 0 0.[Hint: Always first determine the largest of the 3 numbers entered and check if its square equals the sum of the squares of the other 2 sides - this uses the fact that the hypotenuse is the largest side of the right triangle.]
Sample Run:
%> This is a program that checks if three numbers form a Pythagorean Triple
%> Enter the 3 integer sizes of the triangle
%> 3 5 4
%> This is right triangle with hypotenuse of size 5; Pythagorean Triple(3, 4, 5)
%> Enter the 3 integer sizes of the triangle
%> 2 5 4
%> This is NOT a right triangle - not a Pythagorean Triple
...

• (HW_4_3.cpp/exe) The values of P ( = Pi ~ 3.1415...) may be calculated from the infinite series:
P = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ... + (-1)n4/(2n+1) + ...

where (-1)n4/(2n+1) is the n-th term, n = 0, 1, 2, 3, .... . Write a program that asks the user for an integer, n, representing the number of terms of the series to be used to approximate P, and then reports the approximation value. For example, if n = 0, then the approximation will be: P ~= 4; and if n=2, then P ~= 4 - 4/3 + 4/5. As usual, run the computation in a loop until a negative number (n < 0) is entered. You should test run your program with increasing values of n and convince yourself the approximations improve and converge as n increases.
Sample Run:
%> This is a program that approximates P (Pi) with arbitrary accuracy
%> Enter the number of terms to be used in the approximation (integer)
%> 0
%> The 0-term approximation of P (Pi) is: 4
%> Enter the number of terms to be used in the approximation (integer)
%> 1
%> The 1-term approximation of P (Pi) is: 2.66666
%> Enter the number of terms to be used in the approximation (integer)
%> 2
%> The 2-term approximation of P (Pi) is: 3.46666
...

\Ivo D. Dinov, Ph.D., Department of Neurology and Program in Computing, UCLA School of Medicine/