Developing Algorithms Using Arrays

AP Computer Science A· difficulty 4/5

String[] names = {"Ann", "Bob", "Cy", "Dan"};
int[] scores = {70, 85, 90, 60};
int idx = -1;
int max = -1;
for (int i = 0; i < scores.length; i++) {
  if (scores[i] > max) {
    max = scores[i];
    idx = i;
  }
}
System.out.println(names[idx]);

What is printed?

  • A

    Dan

  • B

    Bob

  • C

    Ann

  • D

    Cy

    check_circle

Explanation

The maximum score is 90 at index 2. names[2] is "Cy". Parallel arrays let us correlate the index across both arrays.

Want 10 more like this — adaptive to your weak spots?

Related questions