0

I'm trying to finish an AP CS FRQ question. I wrote the code, but it doesn't work. Where have I messed up?

Write a static method rowSums that calculates the sums of each of the rows in a given twodimensional array and returns these sums in a one-dimensional array. The method has one parameter, a twodimensional array arr2D of int values. The array is in row-major order: arr2D[r][c] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.

`   public static int[] rowSums(int[][] arr2D){
        int total2 = 0;
        int a[] = new int[arr2D.length];
        for(int x=0; x<arr2D.length; x++){
            for(int n=0; n<arr2D[x].length;n++){
                arr2D[x][n] = total2;
                a[x] = a[x] + total2;
            }
        }
        return a;
    }`

4 Answers 4

0

You need to reset total2 within the outer loop, and set the value after the end of the inner loop

    int a[] = new int[arr2D.length];
    for(int x=0; x<arr2D.length; x++){
        int total2 = 0;
        for(int n=0; n<arr2D[x].length;n++){
              total2 += arr2D [x][n];
        }
        a[x] = total2;
    }

If total2 is not going to be re-used this can be shortened to

for (int x=0; x < arr2D.length; x++) {
    for (int n=0; n<arr2D[x].length; n++) {
        a[x] = a[x] + arr2D[x][n];
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your assignment is backwards, you should be storing each element of the 2D array using this:

total2 = arr2D[x][n];

not this:

arr2D[x][n] = total2;

Full code:

for (int x=0; x < arr2D.length; x++) {
    for (int n=0; n < arr2D[x].length; n++) {
        total2 = arr2D[x][n];
        a[x] = a[x] + total2;
    }
}

Comments

0

Writing good code includes good comments and good variable name choices. Let's start by first just commenting through your code line by line so you can better see what's going on:

    public static int[] rowSums(int[][] arr2D){

        // A variable which is always 0
        int total2 = 0;

        // The actual output:
        int a[] = new int[arr2D.length];

        // For each row..
        for(int x=0; x<arr2D.length; x++){

            // For each column..
            for(int n=0; n<arr2D[x].length;n++){

                // Put 0 into the 2D array (this line is backwards):
                arr2D[x][n] = total2;

                // Add the 'total' (always 0) into the current output
                a[x] = a[x] + total2;
            }
        }

        // Return the output
        return a;
    }

Total2 is never set

Ok, so hopefully it's a little clearer that one of your lines is backwards (and you have some poor variable naming choices). Something better looks more like this:

    public static int[] rowSums(int[][] arr2D){

        // The actual output:
        int totals[] = new int[arr2D.length];

        // For each row..
        for(int row=0; row<arr2D.length; row++){

            // For each column..
            for(int col=0; col<arr2D[row].length;col++){

                // Get the column value:
                int columnValue = arr2D[row][col];

                // Add the column amount into the total:
                totals[row] = totals[row] + columnValue;
            }
        }

        // Return the output
        return totals;
    }

As the variables are now much clearer, we can remove the redundant comments into just this:

    public static int[] rowSums(int[][] arr2D){

        int totals[] = new int[arr2D.length];

        for(int row=0; row<arr2D.length; row++){
            for(int col=0; col<arr2D[row].length;col++){
                int columnValue = arr2D[row][col];
                totals[row] = totals[row] + columnValue;
            }
        }

        return totals;
    }

5 Comments

Some would argue that good code should not even require comments :-) (maybe Javadoc is OK though).
@TimBiegeleisen I entirely agree - for someone starting out though, they make everything considerably easier to read (and help with those naming choices too).
hmm, a little bit too verbose for me
@ScaryWombat As the OP has beginner scoping issues as well as getting lines backwards, I think they need as much clarity as possible to really understand what their code is doing.
@LukeBriggs and a rubber duck too
-1

arr2D[x][n] = total2; // you are assigning 0 to arr2D[x][n]

change it to total2= arr2D[x][n] ;

and it will work!!

1 Comment

@ Luke no you dont bcz it will assign new value to total2 every time. Please try it !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.