Two dimensional arrays are very useful for organizing large amounts of data. For
ID: 3540162 • Letter: T
Question
Two dimensional arrays are very useful for organizing large amounts of data. For example, suppose you have the following data on the number of points scored by each player on a basketball team over each game of a seven-game series:
Player Game 0 Game 1 Game 2 Game 3 Game 4 Game 5 Game 6
Tim 20 27 16 23 20 27 18
Dora 8 18 14 17 9 12 0
Catherine 38 19 25 22 19 25 31
Suleiman 17 8 11 21 15 0 9
Top 2 1 3 0 10 2 4
This information can be stored in a 2-D array, where each row of the array represents a player and each column represents a game. Index [i][j] of this array is the number of points scored by player i in game j ( where both I and j start counting from 0) %u2013 for example, index [0][5] would be the number of points scored by Tim in Game 5. Write the following methods to process data stored in this format:
1. Public static double averagePPG(int[][] scores, int p)
This should return the average points per game of player p, based on the data in the array scores.
2. Public static int singleGameScore(int[][] scores, int g)
This should return the total points scored (by the entire team) in game g, based on the data in the array scores.
3. Public static double averageGameScore(int[][] scores)
This should return the average total points scored (by the entire team) per game, based on the data in the array scores.
4. Public static int singleGameTopScoringPlayer(int[][] scores, int g)
This should return the index of the highest-scoring player in game g, based on the data in the array scores.
5. Public static void main(String[] args)
This main method should declare and instantiate a 2-D array containing the above data, then call your methods on it to verify they%u2019re working properly. Remember that you can use shorthand notation with curly braces to quickly instantiate a 2-D array with specific values.
Explanation / Answer
Your Code
public class BasketballGame {
public static double averagePPG(int[][] scores, int p) {
int rowNumber = p;
double totalPointsScored = 0;
for (int i = 0; i < scores[rowNumber].length; i++) {
totalPointsScored = totalPointsScored + scores[rowNumber][i];
}
double averageOfPlayer = (totalPointsScored / scores[rowNumber].length);
return averageOfPlayer;
}
public static int singleGameScore(int[][] scores, int g) {
int rownumber = 0;
int totalPointScored = 0;
while (rownumber < scores.length) {
totalPointScored = totalPointScored + scores[rownumber][g];
rownumber++;
}
return totalPointScored;
}
public static double averageGameScore(int[][] scores) {
int rowNumber = 0;
int columnNumber = 0;
int totalInGame = 0;
int averageTotalOfAllGames = 0;
while (columnNumber < scores[0].length) {
while (rowNumber < scores.length) {
totalInGame = totalInGame + scores[rowNumber][columnNumber];
rowNumber++;
}
averageTotalOfAllGames = averageTotalOfAllGames + totalInGame;
totalInGame = 0;
rowNumber = 0;
columnNumber++;
}
averageTotalOfAllGames = (averageTotalOfAllGames / (scores[0].length));
return averageTotalOfAllGames;
}
public static int singleGameTopScoringPlayer(int[][] scores, int g) {
int indexOfHighestScoringPlayer = 0;
int rowNumber = 0;
int maximumValue = scores[rowNumber][g];
int nextrowNumber = rowNumber + 1;
while (nextrowNumber < scores.length)
{
if (maximumValue < scores[nextrowNumber][g]) {
maximumValue = scores[nextrowNumber][g];
indexOfHighestScoringPlayer = nextrowNumber;
}
nextrowNumber++;
}
return indexOfHighestScoringPlayer;
}
public static void main(String args[]) {
int basketBallStats[][] = { { 20, 27, 16, 23, 20, 27, 18 },
{ 8, 18, 14, 17, 9, 12, 0 }, { 38, 19, 25, 22, 19, 25, 31 },
{ 17, 8, 11, 21, 15, 0, 9 }, { 2, 1, 3, 0, 10, 2, 4 } };
int i = 0;
while (i < basketBallStats.length) {
for (int j = 0; j < basketBallStats[i].length; j++) {
System.out.print(basketBallStats[i][j] + " ");
}
System.out.println();
i++;
}
System.out.println(averagePPG(basketBallStats, 3));
System.out.println(singleGameScore(basketBallStats, 2));
System.out.println(averageGameScore(basketBallStats));
System.out.println(singleGameTopScoringPlayer(basketBallStats, 2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.