Ajlex
Ajlex

Reputation: 21

Looping through a 2D array (diagonal)?

I have a 6x6 array and want to get always the four next values. As an example:

0-----
-1----
--2---
---3--
----4-

So I want to get the sum of (0+1+2+3) and the sum of (1+2+3+4) and this for all diagonals.

I tried something, but unfortunately I only get the first diagonal (from the top left corner, downstairs):

for (int i = 0; i < 3; i++) {
            for (int j = 4; j >= 0; j--) {
                    dosomething..(array[j][i]);
                    dosomething..(array[i + 1][j + 1]);
                    dosomething..(array[i + 2][j + 2]);
                    dosomething..(array[i + 3][j + 3]);
            }
        }

Also I need to get the next four values for all diagonals, which goes from the buttom upstaris too and I have no clue how to make it..

Edit: here is another example:

A B C D E F
G H I J K L
M N O P Q R
S T U V W X
Y Z 1 2 3 4
6 7 8 9 10 11

What I want now is the following:

I want to get:

(A, H, O, V)

(H, O, V, 3)

(O, V, 3, 11)

(C, J, Q, X)

(B, I, P, W)

(I, P, W, 4)

and the same thing for all diagonals which go from the buttom upstairs.

Upvotes: 2

Views: 6094

Answers (5)

Jeremy Whitcher
Jeremy Whitcher

Reputation: 721

Here's how I would approach the problem.

I would divide the problem into two parts. The first is to get a value from the array. The second is to loop 4 times getting values in increasing order.

part 1 getting a value from the array.

char getValue(int row, int col)
{
   // add code here to range check the row and col
   return this.myArray[row, col];
}

part 2 looping 4 times to get the values in increasing or decreasing order.

char[] getValues(int startRow, int startCol, boolean goUp)
{
   char[] values = new int[4];
   int row = startRow; 
   int col = startCol;
   // get the 4 values
   for (i = 0; i < values.length(); i++)
   {
      values[i] = getValue(row, col);
      // check if going upstairs or downstairs
      if (goUp)
      {
         row = row + 1;
         col = col + 1;
      }
      else
      {
         row = row - 1;
         col = col - 1;
      }
   }
}

Getting the values is now simple. Just call getValues() with the starting row and column.

For example:

char values[] = getValues(0, 0, False);

Upvotes: 1

Shivam Mishra
Shivam Mishra

Reputation: 11

There seems a problem in the for loop statements, if you try to run this loop and print values of j and i, you will see the pairs to be something like this.

  • 4 0
  • 3 0
  • 2 0
  • 1 0
  • 0 0
  • 4 1
  • 3 1 and so on.

So whenever you try to access array[j][i], you never get a diagonal element. Use the fact that i and j values for diagonal elements are same.

    for (i = 0; i < 4; i++) {
                s1=s1+array[i][i];
                s2=s2+array[i + 1][i + 1];
                s3=s3+array[i + 2][i + 2];}  

try something like this. The loop goes for 3 turns, since for a 6*6 array the number of 4 consecutive element diagonals are 3 if going downward starting from 0,0. s1, s2, s3 is used to save the sum at every turn. Hope it helps.

Upvotes: 1

Nikolas
Nikolas

Reputation: 44486

Try this in case the array is square:

for (int i=0; i<array[0].length; i++) {
    dosomething..(array[i][i]);
}

And this in case the array is not square:

int i=0;
while (i<Math.min(array[0].length,array.length)) {
    System.out.println(array[i][i]);
    i++;
}

Upvotes: 2

Lachezar Todorov
Lachezar Todorov

Reputation: 923

This is a sample code just to illustrate the algorithm. I'm not java developer so you should fix some things related to the language.

int table[][];

public int diagonalSum(int index, int max, bool down) {
    if (index <= max) {
        int nextIndex = index + (down ? -1 : 1);
        return table[index][index] + diagonalSum(nextIndex, max, down);
    }
}

int firstDown = diagonalSum(0, table.length, true);
int secondDown = diagonalSum(1, table.length, true);

int firstUp = diagonalSum(table.length, table.length, false);

Upvotes: 1

Andre Ahmed
Andre Ahmed

Reputation: 1889

your matrix indieces are 00, 11,22,33, then you need something like that

for (int i=0; i<array[0].length; i++) {
    dosomething..(array[i][i]);
}

Upvotes: 1

Related Questions