Post

Created by @nathanedwards
 at November 1st 2023, 3:47:24 pm.

Question:

The school's soccer team is keeping track of the goals scored by each player in different matches using a multidimensional array in their program. The team manager wants to implement a function that takes the player's ID and returns the total number of goals scored by that player throughout all the matches.

Implement the getTotalGoals(int[][] goals, int playerID) function that takes a 2D integer array goals and an integer playerID as parameters. The goals array holds the number of goals scored by each player in different matches, where the first index corresponds to the player's ID and the second index represents the match number. The playerID parameter represents the player's ID for which the total number of goals is to be calculated.

The function should return an integer representing the total number of goals scored by the given player across all matches. If the player ID is not found or if the goals array is empty, return 0.

For example, consider the following goals array:

int[][] goals = {
  {4, 2, 1},
  {3, 0, 2},
  {1, 1, 2}
};

In this array, the first index represents the player ID, and the second index represents the match number. So, if we call getTotalGoals(goals, 1), it should return 3 as player 1 scored a total of 3 goals across all matches.

Implement the getTotalGoals function:

public static int getTotalGoals(int[][] goals, int playerID) {
    // your code here
}

Example Usage:

int[][] goals = {
  {4, 2, 1},
  {3, 0, 2},
  {1, 1, 2}
};

System.out.println(getTotalGoals(goals, 1)); // Output: 3
System.out.println(getTotalGoals(goals, 2)); // Output: 4
System.out.println(getTotalGoals(goals, 3)); // Output: 4
System.out.println(getTotalGoals(goals, 4)); // Output: 0

Explanation:

To solve this problem, we will iterate through the goals array and sum the goals of the specified playerID. We will use a nested loop to iterate through each player's goals in all matches.

Here is the step-by-step explanation:

  1. Initialize a variable totalGoals to store the total goals scored by the player and set it to 0.
  2. Iterate through the goals array using the outer loop.
  3. Inside the outer loop, iterate through the inner array using the inner loop.
  4. Within the inner loop, check if the current playerID matches the player's ID at the outer loop index. We can compare playerID with the goals array index, i.e., goals[i][0].
  5. If the playerID matches, add the current element (goals[i][j]) to totalGoals.
  6. After both loops complete, return totalGoals.

Here is the implementation of the getTotalGoals function in Java:

public static int getTotalGoals(int[][] goals, int playerID) {
    int totalGoals = 0;
    
    for (int i = 0; i < goals.length; i++) {
        if (goals[i][0] == playerID) {
            for (int j = 1; j < goals[i].length; j++) {
                totalGoals += goals[i][j];
            }
        }
    }
    
    return totalGoals;
}

Now let's test our function using the provided example usage code:

int[][] goals = {
  {4, 2, 1},
  {3, 0, 2},
  {1, 1, 2}
};

System.out.println(getTotalGoals(goals, 1)); // Output: 3
System.out.println(getTotalGoals(goals, 2)); // Output: 4
System.out.println(getTotalGoals(goals, 3)); // Output: 4
System.out.println(getTotalGoals(goals, 4)); // Output: 0

The first getTotalGoals(goals, 1) call calculates the total goals scored by player 1, which results in 3. Similarly, the other function calls return the expected results.

Hence, the implementation of the getTotalGoals function is correct.