lawtonhself
lawtonhself

Reputation: 41

how to add elements in a 2d array

I just want to know how this program works, and why the answer is 14. here is the code:

public class extra {
public static void main(String[] args){
    int[][] table = {{1,2,3},{4,5,6},{7,8,9}};
    int sum = 0;
    for( int i = 2; i > 0; i-- ) 
              sum += table[i][3-i];
              System.out.println(sum);

}

}

I understand the way the matrix is set up

123

456

789

but what is i in this problem, because I thought it was the number of rows, but since it is in a for loop, does it mean that i is the number in the matrix? Also how do the [i][3-i] come in to affect? The answer is 14, and I just want to know how it is 14.

Upvotes: 0

Views: 14631

Answers (6)

dawrutowicz
dawrutowicz

Reputation: 3020

your program takes table[2][1] (value of 8) and table[1][2] (value of 6) elements, sums them and prints as output (value of 14)

regarding your question in a title your main method should be more like this:

public static void main(String[] args) {
    int[][] table = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int sum = 0;

    System.out.println("Before\n");

    for (int i = 0; i < table.length; i++) {
        for (int j = 0; j < table[i].length; j++) {
            sum += table[i][j];
            System.out.printf("Sum after %d iteration: %d\n", i + j + 1, sum);
        }
    }

    System.out.println("\nIn total: " + sum);
}

i + j + 1 is a sum of current iteration which is sum of both axises, and since Java has 0-based indexed tables, it is increased by 1

Hope it helps!

Upvotes: 1

Naji Kadri
Naji Kadri

Reputation: 99

Here's how to add elements in a 2D array in a easy way. First when you initialize a 2D array think of the first brackets [ ] as a column and the second bracket [ ] as column rows. For example: int[][] num = new int[10][5] which means 10 columns and 5 rows.

If you want to fill all the elements in a 2D array you must use two for loops:

int[][] num = new int[10][5];

for (int i =0; i < num.length;i++ ) {
     for (int x=0; x < num[0].length;i++) { //we used num[0] because we need the length of the rows not the columns
           num[i][x] = //any value you want to assign
          }
 }

Upvotes: 0

btgui
btgui

Reputation: 7

for (int i = 2; i > 0; i--)

so starting at 2 it checks if i is greater than zero loops once then i-- subtracts 1 check again still greater than 0 loops again then subtracts 1 again checks if greater than 0 now 0 it is not greater than 0 so stops looping //thus loops 2 times

//[0] = 1st [1] = 2nd [2] = third ...

//counting in code starts at zero not 1 so and array of 3 counts a the spaces 0,1,2

int sum = 0;//sum starts at zero

//using the value of i translates as such

sum += table[2][3-2];//[2][1]this is 3rd group 2nd part so sum += 8
//then
sum += table[1][3-1];//[1][2]this is 2nd group 3rd part so sum += 6

0 + 8 + 6 = 14

Upvotes: 0

Martin M J
Martin M J

Reputation: 880

What the for loop does is as follows:

  1. i = 2. Enter loop.
  2. Add table[2][3-2] to sum. Sum is now 8, because table[2][1] = 8.
  3. Decrement i by 1.
  4. i = 1. Enter loop.
  5. Add table[1][3-1] to sum. Sum is now 14, because table[1][2] = 6.
  6. Decrement i by 1.
  7. i = 0. 0 is not greater than 0, so we exit the loop. The sum became 14.

Two-dimensional arrays like int[][] table have two indexes. One for the "outer" array (or rows), and one for the "inner" ones (columns).

Let's use int[][] table = {{1,2,3},{4,5,6},{7,8,9}}; from your code as an example:

table[1][2]: 1 means we should look in the array at index 1, which is {4,5,6}. 2 means we should look at {4,5,6}'s index 2, which is 6. In other words table[1][2] == 6.

table[2][0]: 2 means we should look in the array at index 2, which is {7,8,9}. 0 means we should look at {7,8,9}'s index 0, which is 7.

Upvotes: 0

Rhyzomatic
Rhyzomatic

Reputation: 101

i, in itself, does not correspond directly to anything in the matrix. It is just the name of the variable that the for loop changes each time it loops.

The [i][3-i] is how the i interacts with table. On the first round of the for loop, the i will be equal to 2. Thus, sum will be increased by table[2][1], which is the 3rd row and the 2nd column of the matrix, which has a value of 8.

On the second round of the for loop, the for loop, the i will be equal to 1. Thus, sum will be increased by table[1][2], which is the 2nd row and the 3rd column of the matrix, which has a value of 6.

Therefore, sum will be equal to 8+6=14.

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26175

It is only summing part of a diagonal, specifically table[2][1] which is 8, and table[1][2] which is 6.

The easiest way to see what is going on is to add an output statement in the loop:

for (int i = 2; i > 0; i--) {
  sum += table[i][3 - i];
  System.out.println(i + " " + (3 - i) + " " + table[i][3 - i]);
}

Upvotes: 1

Related Questions