About searching:

What kind of search do you use: linear or binary for each scenario?

  1. Your cell phone is lost and you ping it to figure out which way to go
  2. Your keys are lost somewhere in the living room
  3. Look for a book in the library with the library classification number 808.1BN
  4. Figure out if any people in the room have the same birthday

 

Review questions:

What does the code do if int[ ] nums = { 7,26,45,189}?

public static void mystery (int nums[]){
    for (int i =0; i < nums.length; i += 2)
        nums[i] /= 10;
} What is the error in this code? String [] letters = {"F", "A", "V", "O", "R", "I", "T", "E"};

String what = scan.readLine();
int location = -1;
for (int i =0; i < letters.length; i++)
{
    if ( what == letters[i])
    {
        location = i;
        break;
    }
}
System.out.println( location );

Why does this code sometimes produce an error?

int flag = 0;
for (int i = 0; i < d.length; i ++)
{
    if (d[i] == value)
        flag = 1;
}

if (flag == 1)
    System.out.println("Value found at "+ i);
else
    System.out.println("Value not found");

 

Which produce an ArrayIndexOutOfBounds error for int[ ] d = { 1,2,4,5,8}

  1. d[0]
  2. d[-1]
  3. d[4]
  4. d[d[1]]
  5. d[d[3]]
  6. d[d[4]-d[2]]