1

I need this method to take the middle columns of the 2D array that was made/changed in other methods, compute the average of each row (excluding the 0 index and last index), place that average in the last column of the row, then go down to the next row and repeat until there are no more rows. Basically a setup of 2 rows and 3 columns will actually have 5 columns as seen below.

   Weight  Grade1  Grade2  Grade3  Average
  ----------------------------------------
1|  4.0     5.0     5.0     5.0    -999.0  
2|  5.0     10.0    10.0    10.0   -999.0  

the method needs to take the Grade scores, find their average, then replace the -999.0 with the average, and then do the same for the next rows that exist(size defined by user input in another method). Here is my code for the method, I know I am fairly close, but I am making a few mistakes somewhere.

public static double[][] computeCategoryAverages(double[][] scoreArray){
    for(int i = 0; i < scoreArray.length; i++){
        double sum = 0;
        int numOfAssign = 0;
        if(i == 0){
            System.out.println("The Average for the first category: ");
            for(int j = 1; j < scoreArray[0].length-1; j++){
                sum = sum + scoreArray[0][j];
                numOfAssign++;
            }
            double average = sum / numOfAssign;

            for(int k = 0; k < scoreArray.length; k++){
                //sum = sum + scoreArray[i][j];
                //noOfAssign++;     
                scoreArray[k][scoreArray[k].length-1] = average;  
            }
        }
        else{
            System.out.println("The Average for the next category: ");
            for(int j = 1; j < scoreArray[0].length-1; j++){
                sum = sum + scoreArray[i][j];
                numOfAssign++;
            }
            double average = sum / numOfAssign;
            for(int k = 0; k < scoreArray.length; k++){
                //sum = sum + scoreArray[i][j];
                //noOfAssign++;     
                scoreArray[k][scoreArray[k].length-1] = average;  
            }
        }
    }
    return scoreArray;
}
1
  • And what is the question now? Are there any errors/exceptions? Commented Nov 20, 2014 at 22:18

2 Answers 2

2

Try this:

public static double[][] computeCategoryAverages(double[][] scoreArray){
for(int i = 0; i < scoreArray.length; i++){
    double sum = 0;
    int numOfAssign = 0;
    double average=0.0;
    System.out.printf("The Average for category:%d:",i+1);
    for(int j = 1; j < scoreArray[i].length-1; j++){
       numOfAssign++;
       sum = sum + scoreArray[i][j];

      }
    double average = sum / numOfAssign;
    scoreArray[i][scoreArray[i].length-1] = average;  

}
return scoreArray;
}
Sign up to request clarification or add additional context in comments.

Comments

2

I can't exactly understand your code. It'd be great if you could comment it. But there I'll give you a pseudo code (ish) and I hope this helps in some way or another. Assuming: Your array is a double

double tempSum=0;

for (int x = 0; x < amount of rows; x++)
{
   tempSum=0;

   for(int y = 1(because excluding first index); y < amount of numbers - 1 (because excluding last; y++)
   {
       tempSum+= marks[x][y];
   }
   marks[x][length-1]=(tempSum/length-2) //setting the average to the last index, length -2 to account for index 0 and last one
}

then output as needed

your code seems a little too complicated for what is needed =p

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.