Back

Activity 5.4 — Parallel Arrays

divider

Introduction

Activity 5.4

Parallel Arrays

The Big Idea

Parallel arrays store related information in separate arrays. The relationship is maintained by using the same index.

// Parallel arrays = same index = same "record"
names[i] <-> scores[i]

Think Like a “Record”

  • If names[2] is "Sam"...
  • ...then scores[2] is Sam's score.
  • Same index = same person / same item.

Traversal Must Stay in Sync

When you loop, you loop over the indexes and use both arrays.

// Traverse IN SYNC
for (int i = 0; i < names.length; i++)
{
System.out.println(names[i] + ": " + scores[i]);
}

Common Algorithm: “Best So Far”

Often you don't store the best value — you store the index of the best value. Then you can use that index in both arrays.

// Find the index of the best score, then use it
int bestIndex = 0;
for (int i = 1; i < scores.length; i++)
{
if (scores[i] > scores[bestIndex])
{
bestIndex = i;
}
}
// names[bestIndex] matches scores[bestIndex]

Common Algorithm: Search by Name

To find a student, you search the names array. If you find it, you use the same index to get the score.

// Linear search across a parallel array
int index = -1;
for (int i = 0; i < names.length; i++)
{
if (names[i].equals(targetName))
{
index = i;
}
}
  • If the name is not found, keep index as -1.

Today's Two Apps

  • Numeric App: Score Analyzer (average, max, passing count, lookup)
  • Quiz App: Questions + Answers using parallel arrays

Success Criteria

  • You can explain what “same index” means.
  • Your loops keep arrays aligned.
  • You can find and use the correct index for calculations and search.

'F' → Fullscreen

Objectives

  • icon Explain how parallel arrays store related data using matching indexes.
  • icon Traverse two arrays in sync to print, compute, and analyze data.
  • icon Use linear search on one array and use the found index to access the other.
  • icon Build two small apps that use parallel arrays: a numeric analyzer and a quiz.
divider

Activity Tasks

  • icon Create a new project named Activity5-4-ParallelArrays.
  • icon Complete both tasks.

Task 1: Numeric App — Student Score Analyzer

  • icon Print a roster (name + score) using one loop.
  • icon Compute and print the class average.
  • icon Find and print the top student's name and score.
  • icon Count and print how many students are passing (>= 70).
  • icon Ask the user for a name and print that student's score (or “Not found”).
Task 1 — Score Analyzer
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
// Task 1 — Numeric App: Student Score Analyzer (Parallel Arrays)
//
// You are given two parallel arrays:
// - names[i] goes with scores[i]
//
// Your job:
// 1) Print a roster (each student + score)
// 2) Compute and print the class average
// 3) Find and print the highest score AND the student's name
// 4) Count and print how many students are passing (>= 70)
// 5) Ask the user for a name and print that student's score (or "Not found")
String[] names = {"Ava", "Ben", "Chris", "Dina", "Eli"};
int[] scores = {88, 73, 95, 67, 81};
// 1) Print roster
System.out.println("Roster:");
for (int i = 0; i < names.length; i++)
{
System.out.println(names[i] + ": " + scores[i]);
}
// 2) Average
int sum = 0;
for (int i = 0; i < scores.length; i++)
{
sum += scores[i];
}
double avg = (double) sum / scores.length;
System.out.println("Average: " + avg);
// 3) Highest score + name
int bestIndex = 0;
for (int i = 1; i < scores.length; i++)
{
if (scores[i] > scores[bestIndex])
{
bestIndex = i;
}
}
System.out.println("Top student: " + names[bestIndex]);
System.out.println("Top score: " + scores[bestIndex]);
// 4) Count passing (>= 70)
int passing = 0;
for (int i = 0; i < scores.length; i++)
{
if (scores[i] >= 70)
{
passing++;
}
}
System.out.println("Passing count: " + passing);
// 5) Search by name
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a student name to look up: ");
String targetName = scanner.nextLine();
int index = -1;
for (int i = 0; i < names.length; i++)
{
if (names[i].equals(targetName))
{
index = i;
}
}
if (index == -1)
{
System.out.println("Not found.");
}
else
{
System.out.println(targetName + "'s score: " + scores[index]);
}
scanner.close();
}
}

Task 2: Quiz App — Questions + Answers

  • icon Ask each question in a loop.
  • icon Compare answers using equalsIgnoreCase.
  • icon Track a score counter and print the final score.
  • icon Print feedback after each question.
Task 2 — Quiz App
import java.util.Scanner;
public class Program
{
public static void main(String[] args)
{
// Task 2 — Quiz App: Question + Answer Parallel Arrays
//
// You are given two parallel arrays:
// - questions[i] goes with answers[i]
//
// Your job:
// 1) Ask each question
// 2) Compare the user's answer to the correct answer (case-insensitive)
// 3) Keep score and print the final result
//
// Notes:
// - Use equalsIgnoreCase(...) for comparison
// - Print "Correct!" or "Incorrect. The answer was ___"
String[] questions =
{
"What is 2 + 2?",
"What keyword starts a for loop in Java?",
"What is the first index in an array?",
"True/False: Arrays can change size after creation."
};
String[] answers =
{
"4",
"for",
"0",
"false"
};
Scanner scanner = new Scanner(System.in);
int correct = 0;
System.out.println("Quiz Time!");
System.out.println("----------");
for (int i = 0; i < questions.length; i++)
{
System.out.println("Q" + (i + 1) + ": " + questions[i]);
System.out.print("Your answer: ");
String user = scanner.nextLine();
if (user.equalsIgnoreCase(answers[i]))
{
System.out.println("Correct!");
correct++;
}
else
{
System.out.println("Incorrect. The answer was: " + answers[i]);
}
System.out.println();
}
System.out.println("Final Score: " + correct + " / " + questions.length);
scanner.close();
}
}
divider

Sample Output

Your program output should something similar to the sample output below.

Sample Output
Roster:
Ava: 88
Ben: 73
Chris: 95
Dina: 67
Eli: 81
Average: 80.8
Top student: Chris
Top score: 95
Passing count: 4
Enter a student name to look up: Dina
Dina's score: 67
Quiz Time!
----------
Q1: What is 2 + 2?
Your answer: 4
Correct!
Q2: What keyword starts a for loop in Java?
Your answer: for
Correct!
Q3: What is the first index in an array?
Your answer: 1
Incorrect. The answer was: 0
Q4: True/False: Arrays can change size after creation.
Your answer: false
Correct!
Final Score: 3 / 4
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. In your own words, what does “same index = same record” mean?
  2. Why is it safer to store bestIndex instead of storing the best score directly?
  3. What does the value -1 mean in a search algorithm?
  4. What could go wrong if two parallel arrays are not the same length?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete