Java Reference

In-Depth Information

1/(k+1)!
to be calculated using
tk
. Terminate the loop when
tk
< .
1*10
13
. How

many iterations does it take?

E14.
Write a loop (with initialization) that generates an approximation to pi, the

ratio of the diameter of a circle to its circumference. Do the work as in exercise

E13, but use this formula to calculate approximations:

pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - ...

Is this feasible? How many iterations does it take?

E15.
Write a loop (with initialization) that generates an approximation to pi, ratio

of the diameter of a circle to its circumference. Do the work as in exercise E14,

but use this formula to calculate approximations, where
c
is
2*sqrt(3)
:

pi = c - c/(3*3
1
) + c/(5*3
2
) - c/(7*3
3
) + c/(9*3
4
) - ...

Calculate c only once. Is this feasible —how many iterations does it take?

E16.
Here is another way to calculate pi. Throw random darts at a disk of radius

1
that is inscribed in a
2
x
2
square. The fraction hitting the disk should be the ratio

of the area of the circle, to the area of the square:
pi*r
2
/ (2r)
2
, or
pi/4
. To

throw a dart, calculate two random numbers
(x, y)
in the range
-2..2
. The dart

hits the disk if
x
2
+y
2
<= 1
. Write a loop (with initialization) that calculates an

approximation to pi by throwing random 10,000 darts.

E17.
Write a loop to count how many times the vowel
"a"
occurs in a string s.

E18.
Write a loop to count how many vowels a string s contains.

E19.
Write a loop to count how many pairs of adjacent equal characters are in a

string s. The string
"bbbccd"
contains three pairs of adjacent equal values.

Search WWH ::

Custom Search