CIS 071
Sections 1 and 2
Fall
2003
Two Short Exercises - Data
Approximation and While Loops
Exercise 1. Write a function that uses a
while loop to add the value 0.1 (representing 10 cents – one thin dime) to
itself n times where n is an input argument to your function. Except for the
data used to control your while loop (such as n and some counter that goes up
to n), represent all data in type float. Then redo the problem representing all
data as type double. Once you are sure your program is working, print only the
initial value and the final result. Is the result what you expected? Explain.
Write a driver program to
test your function for n = 1000 and then for n = 100000. The value of n (1000 or 100000 or some other
value of your choice) should be read in before your add_dimes function is called.
When you are done, insert
into your driver program (at the top) some comments (your observations) on the
results you obtained. You should have a
set of 4 results to comment on: 1) n = 1000 run with data of type float; 2) n =
100000 run with data of type float; 3)
n = 1000 run with data of type double; 4) n = 100000 run with data of type
double. You can use the same driver program
for all four tests.
Exercise 2: Write a function that uses a
while loop to compute and print successive powers of the fraction 1/2, starting
with 1/2 raised to the zero power (mathematically defined to be 1) and
continuing until 1/2 to the 999th power is computed OR until some power k is
reached such that 1/2 to the kth power equals zero. Again, keep all data in
type float. Do not bother to redo using
type double. (All you mathematicians in the house -- is the result what you
expected? Explain.)
Again, write a driver program to test your function. You can most easily accomplish the requested task by starting with the value 1.0 and repeatedly (in your loop) multiplying 1 by 0.5 (1/2 expressed in decimal). You function should return an integer value indicating how many times you multiplied 1.0 by 0.5 The function will have no arguments. The result returned will either be 999 or some smaller number k for which (1/2) raised to the k power becomes 0.
When you are done, insert into your driver program (at the top) some comments (your observations) on the results you obtained.