?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

2-Dimensional Array of Bowler Scores: Find Average

APCSA-EXDWTV

The following method should return the average score for a specific bowler on your team. The 2-dimensional array named teamScores is organized as follows: 1) each row is a single bowler on your team, 2) each column is a single game bowled (game 1 is column 0, game 2 is column 1, etc.), and 3) an element of 0 means the bowler did not bowl that game.

// Pre-condition: teamsScores is a 2-dim array of bowling scores
//                bowlers is a list of bowler names.  The position of a 
//                bowler corresponds to the row of that bowlers scores in
//                teamScores name is the name of the bowler to compute 
//                the average scores for
public static double findAvgBowlerScore(int[][] teamScores, 
                                ArrayList< String > bowlers, String name)
{
    double sum=0, count=0;

    // insert your code here

    if (count > 0)
        return sum/count;
    return 0;
}

Which of the following should replace //insert your code here such that the method returns the average score for a specific bowler?

A
for (int i=0; i<bowlers.size(); i++)
{
    for (int g=0; g<teamScores[i].length; g++)
    {
        if (teamScores[i].equals(name) && teamScores[i][g]>0)
        {
            sum += teamScores[i][g];
            count++;                        
        }
    }
}
B
for (int i=0; i<bowlers.size(); i++)
{
    if (bowlers.get(i).equals(name))
    {
        for (int g=0; g<teamScores.length; g++)
        {
            if (bowlers.get(i).equals(name) && 
                teamScores[i][g]>0)
            {
                sum += teamScores[i][g];
                count++;                        
            }
        }
    }
}
C
for (int i=0; i<bowlers.size(); i++)
{
    for (int g=0; g<teamScores[i].length; g++)
    {
        if (bowlers.get(g).equals(name) && teamScores[i][g]>0)
        {
            sum += teamScores[i][g];
            count++;                        
        }
    }
}
D
for (int i=0; i<bowlers.size(); i++)
{
    if (bowlers.get(i).equals(name))
    {
        for (int g=0; g<teamScores[i].length; g++)
        {
            if (teamScores[i][g]>0)
            {
                sum += teamScores[i][g];
                count++;                        
            }
        }
    }
}
E
for (int i=0; i<bowlers.size(); i++)
{
    if (bowlers.get(i).equals(name))
    {
        if (teamScores[i]>0)
        {
            sum += teamScores[i];
            count++;
        }                        
    }
}