{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

study guide, final

# study guide, final - index is equal to the key value(if one...

This preview shows pages 1–3. Sign up to view the full content.

1. Produce a ”run” of the following complete program? public class Practice { public static void main(String[] args) { int[] vals = {2, 4, 4, 6, 8, 9}; int loc; loc = fun(vals, 0, 5, 2); System.out.println("loc = " + loc); loc = fun(vals, 0, 5, 7); System.out.println("loc = " + loc); } public static int fun(int[] nums, int s, int e, int key) { while (s <= e) { int p = (s + e) / 2; if (nums[p] == key) return p; else if (nums[p] < key) s = p + 1; else e = p - 1; } return -1; } }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2. Produce a ”run” of the following segment of code: double[] nums = new double[6]; int i = 0; do { nums[i] = 2*i + 1; ++i; } while (i < nums.length); int ind = 0; while (ind < nums.length) { System.out.println("nums[" + ind + "] = " + nums[ind]); ind = ind + 1; } double sum = 0.0; for (i = 0; i < nums.length; ++i) sum += nums[i]; System.out.println("sum = " + sum);
3. Write the definition for a method that takes an array of doubles and a key value as its parameters, and returns the (smallest) index such that the array element at that
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: index is equal to the key value (if one exists) and returns -1 otherwise. 4. Write the deﬁnition for a method that takes an array of doubles and a key value as its parameters, and returns the (largest) index such that the array element at that index is equal to the key value (if one exists) and returns -1 otherwise. 5. Write the deﬁnition for a method that takes an array of doubles as its only parameter and returns an index such that the array elements at all other indecies have a value that is greater than or equal to the value of the element at that index. 6. Write the deﬁnition for a method that takes an array of doubles as its only parameter and returns an index such that the array elements at all other indecies have a value that is less than or equal to the value of the element at that index....
View Full Document

{[ snackBarMessage ]}

### Page1 / 3

study guide, final - index is equal to the key value(if one...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online