How to add integers from one two dimensional array and create a new two dimensional array

newtoMobileDev :

I am trying to solve a problem where one two dimensional array(A) is

1 1 0
1 1 0
1 1 0

and the resulting two dimensional array(B) is

1 2 2
2 4 4
3 6 6

Each point in B is sum of corresponding entry in A, plus all entries to the left and above it.

Here is what I have done so far

public static void main(String[] args) 
    { 

        int[][] arrA = { { 1, 1,0 }, { 1, 1,0 },{ 1, 1,0 } }; 
        int[][] arrB = new int [3] [3]; 

        for (int i = 0; i < arrA.length; i++) { 
            for (int j = 0; j < arrA.length; j++) { 
                System.out.print(arrA[i][j] + " "); 
            } 

            System.out.println(); 
        } 

        System.out.println(); 

    for (int r=0; r<arrB.length; r++) {

        for(int c=0; c<arrB[r].length; c++) {

            arrB[r][c] = r * c; 
        }

    }

    for(int r=0; r < arrB.length; r++) {
        for(int c=0; c < arrB[r].length; c++) {

        //  if((r==0 || r==(arrA.length-1)) || (c==0 || c==(arrA.length-1)) )
            arrB[r][c] = r+c;
            System.out.print(arrB[r][c]+" ");
        }

        System.out.println();
    }


} 
}

The answer I get is

0 1 2 
1 2 3 
2 3 4 

The question I have is how can I get the correct numbers in array B. What code have I missed?

Prerna Gupta :

Each value of B can be be calculated as :

B(i,j) = B(i-1,j-1) + B((i-1,j) - (i-1,j-1)) + A(i,j) + B((i,j-1)-(i-1,j-1))

This is because:

Diagonal value (i-1,j-1) is sum of all left and top value of (i,j) excluding immediate top and left values.

Left value (i, j-1) is sum of immediate left values + diagonal value.

Top value (i-1,j) is sum of immediate top values + diagonal values.

so, by combining these 3 values and the current element we get the value of B(i,j)

Here is the updated code :

public static void main(String args[])  {
         int[][] arrA = { { 1, 1,0 }, { 1, 1,0 },{ 1, 1,0 } }; 
         int [][]B = new int [3][3];
         int diag, i, j,top,left;
         int len = arrA.length;
         int bredth = arrA[0].length;
         for( i=0;i<len;i++){
             for( j=0;j<bredth;j++) {
                 diag = 0; top=0; left=0;
                 if(i-1>=0 && j-1>=0) {
                     diag = B[i-1][j-1];
                 }
                 if(i-1>=0) {
                     top = B[i-1][j];
                 }
                 if(j-1>=0) {
                     left = B[i][j-1];
                 }
                 B[i][j] = diag + arrA[i][j] + (top-diag) + (left-diag);
             }
         }
         for(i=0;i<3;i++) {
             for(j=0;j<3;j++) {
                 System.out.print(B[i][j]);
             }
             System.out.println();
         }
    }

Issue with your code is you are just adding indexes and not taking into consideration values. But as per your question each value of B should be equal to sum of corresponding entry in A, plus all entries to the left and above it

for(int r=0; r < arrB.length; r++) {
        for(int c=0; c < arrB[r].length; c++) {

        //  if((r==0 || r==(arrA.length-1)) || (c==0 || c==(arrA.length-1)) )
            arrB[r][c] = r+c; //Here `r` and `c` are just indexes value not actual value
            System.out.print(arrB[r][c]+" "); 
        }

        System.out.println();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31379&siteId=1