You are back to working on your own.
Math class in any way. In fact,
from the time that power is called to the time that
power returns, it should execute only code that you
write.
Your method should have four cases, three of which make recursive calls. The four cases should be based on the following facts, which apply to integer exponents n:
power(..., 2),
where "..." just stands for the first actual parameter of
the call. Instead, use the time-honored technique of squaring a
number by simply multiplying it by itself.
I have supplied a short driver program, Power.java, that allows you to test
your method power. Copy it, and fill in the missing
method. The driver compares your result to the result computed by the
builtin method Math.pow. Don't worry about how the
result is formatted; let Java's toString method format it
in the way it thinks best.
Don't forget to test all the cases in your method, i.e., negative
values of x and n, etc. What can you say about the
efficiency of your power method? How many
multiplications does your method power use to compute the
value of x512? Write your response to this
question to the bottom of your listing for this program.
Explain how you got it (analyzed it, used a counter in your program,
ran the debugger and counted, etc.).
Turn in test runs that show all the cases at work.
We then separately consider each of the three "corner" triangles (i.e., the three triangles not in the middle), and we further subdivide each corner triangle until the triangle gets really small.
Your task is to use barycentric subdivision within an applet that takes the coordinates of the three corners of a triangle as parameters and creates the following sort of pattern:
I have provided most of the applet, which you can run here. Click three points, and a triangle is barycentrically subdivided. If you drag one of the corners of the triangle, the triangle is updated, along with its barycentric subdivision. Click three new points, and you get a new triangle and its barycentric subdivision.
Start with Barycentric.java.
You have to fill in the body of the method
drawBarycentric. This method should be recursive.
The base case of the recursion is when any of the edges of the triangle is shorter than 10 pixels. In this case the recursive method should return without subdividing further.
Here are a couple of simple geometric facts that you might find helpful:
p and q
is the square root of ((p.x –
q.x)2 + (p.y –
q.y)2). The distance method,
which I have provided, returns this value.
p and q is the
point ((p.x + q.x)/2, (p.y + q.y)/2).
Hand in window snapshots from two different triangles that your program subdivides.
doubles.
An easily-described way to find the kth order-statistic is to sort the array into increasing order, and return the element in the (k–1)st position. (We have to subtract 1 because arrays have 0-origin indexing.) The problem with this method is that we have to sort, and we will see later in this course that sorting n elements takes time proportional to n log2 n. (Actually, we will only see that we can achieve a time of n log2 n; you'll need to take CS 25 to see that in fact n log2 n is the best we can do unless we know something else about the values in the array.)
But there is another way to find the kth order statistic, and on average, it takes time proportional to n. (Proving this running time requires mathematics beyond the scope of CS 5.) This method saves a factor of log2 n over the sorting-based method. This faster method is recursive. It has the header
kth smallest element of
the subarray values[p..r], that is, the subarray of
values starting at index p and ending at
index r. (This subarray has r–p+1
elements.) In the initial call to this method, p is 0
and r is n–1, so that
values[p..r] is the entire array. The subarray under
consideration shrinks in each recursive call, i.e., the difference
between p and r is smaller in each recursive
call.
A given call works as follows:
k is 1. We don't worry about what
to do if k is not 1.)
values[p..q–1] and
values[q+1..r] such that every element in
values[p..q–1] is less than or equal
to values[q], which is less than every
element in values[q+1..r]. We'll see in a
moment how to perform this partitioning.
values[p..q] of "smaller" values. Let's
call this size z.
z equals k, then
values[q] must be the kth
smallest element of values[p..r]. We
return this value.
z is greater than
k, then we know that the kth
smallest element of values[p..r] must be in
the subarray values[p..q–1]. In
fact, it's the kth smallest element of this
subarray. We find it recursively, and then we return
it.
z is less than k.
There are fewer than k elements in the
subarray values[p..q], and so the
kth smallest element of
values[p..r] must be in the subarray
values[q+1..r]. But it is not the
kth smallest element of
values[q+1..r], since we have already ruled
out the z elements of
values[p..q]. What we want is the
(k–z)th smallest element of
values[q+1..r]. We find it recursively,
and then we return it.
p, q, and r
are indices into the array values.
p and r specify the
starting and ending indices, respectively, of the subarray of
values under consideration.
k specifies which order statistic
within the subarray values[p..r] we want.
z of the subarray
values[p..q], you will need to use both
p and q.
p, which is not
necessarily index 0.
I have supplied a driver program, Select.java, that allows you to test
your method select. Copy it, and fill in the missing
method. The driver asks for an array size, randomly fills the array
with doubles between 0 and 1000, prints out the array,
calls select, and checks the answer.
Select.java also supplies a method
a[p..r] into the subarrays a[p..q–1] and
a[q+1..r], and it returns the index q that
marks the partition. If you look at the code, you'll see that it uses
some randomization in deciding how to partition. We'll examine this
method later in the course, when we study the quicksort sorting
algorithm.
Your job is to fill in the body of the method select and
submit three test runs that show it works. Make one test run be on a
short array (10 elements or less), one on a medium-sized array
(between 10 and 100 elements), and one on a large array (over 100
elements).
The main method checks your answer, so that you'll see a
complaint in your output if you got the wrong answer.
You'll also need to add FieldFormat.java to your project.
power method to compute
x512.
CS 5 Lab Assignment #4
CS 5 Lab Assignment #4 v2The "v2" will indicate that it's the second version. And if you need to go to a third version, use v3. And if you need to go to a fourth version, then you're sending too many versions, and you need to think more carefully before Blitzing!!
It is your responsibility to round up the envelope before the assignment is due.
partition correctly?
Infinite recursion problems?
Calls itself at most once in each case?
Output correct?