Find Element

Due in class, January 22, 2007

Please print out your code and output and give it to me by the end of class. Don't forget your name.

The goal

Your goal in this lab is to look for two specific numbers within an array of numbers. For each number, you must print out the number of comparisons your program performs when looking for that number. For example, if the search target number is the first number in the array, your program would perform exactly one comparison. If the target number, on the other hand, is the last element, your program would perform n comparisons where n is the number of elements in the array.

Look for 665 and then 4508.

The output should look like:

Found 665: i comparisions
Found 4508: j comparisions

where i and j would be the actual number of comparisons computed by your progam.

Support code

Cut and paste the following array of numbers into your main program as a field of the surrounding class. Inside your main(), write a loop that accesses numbers to find the element and compute the number of comparisons.

static int[100] numbers = {
  3553, 4508, 3635, 2274, 474, 1900, 640, 596, 3176, 3555, 
  406, 2718, 3541, 3390, 3876, 3286, 1589, 2846, 4976, 3990, 
  94, 3730, 3779, 37, 3094, 1970, 3599, 4598, 3806, 1635, 
  4688, 973, 903, 2250, 1492, 1428, 59, 1341, 1397, 4294, 
  4249, 3292, 3360, 30, 2450, 3839, 296, 2440, 876, 714, 
  3249, 1352, 1591, 439, 1821, 339, 3656, 1722, 3716, 900, 
  1728, 4115, 3379, 3157, 4141, 3173, 945, 1047, 3949, 3430, 
  3281, 122, 4508, 3647, 4844, 2113, 4971, 4521, 905, 2847, 
  2479, 1829, 1338, 2840, 2829, 3742, 1244, 1074, 1495, 3205, 
  3051, 1141, 86, 2089, 3269, 908, 622, 665, 4310, 1320
};

Resources

If you are having trouble executing your Java code, see Compiling, Executing, and Jar'ing Java Code