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

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

View Full Document Right Arrow Icon
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; } }
Background image of page 1

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

View Full DocumentRight Arrow Icon
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);
Background image of page 2
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
Background image of page 3
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 denition 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 denition 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 denition 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

This note was uploaded on 10/22/2010 for the course COMPSCI CS201 taught by Professor Sorenson during the Spring '10 term at Wisconsin Milwaukee.

Page1 / 3

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online