Khusbu Sinha

Ranch Hand

Posts: 117

1

posted 10 months ago

I am writing the java code to find the sum of the elements in the left and the right diagonals of a square matrix. My code is able to calculate the sum of the left diagonal elements correctly but it fails to calculate the correct sum of the right diagonal elements.

Following is a test-case of this program:

Input:

Enter the size of the square matrix:

3

1 2 3

4 5 6

7 8 9

Output:

Left Diagonal Sum =15

Right Diagonal Sum =10

In this case, it ignores the element `5` while calculating the right diagonal sum.

Kindly review my code and suggest the improvements it needs. Thanks.

Following is a test-case of this program:

Input:

Enter the size of the square matrix:

3

1 2 3

4 5 6

7 8 9

Output:

Left Diagonal Sum =15

Right Diagonal Sum =10

In this case, it ignores the element `5` while calculating the right diagonal sum.

Kindly review my code and suggest the improvements it needs. Thanks.

Norman Radder

Ranch Hand

Posts: 146

4

posted 10 months ago

Some suggestions:

use the variable names row and column instead of i and j to make the code easier to understand.

Write down the row,column values that need to be added to sum the right diagonal elements.

Add comments to the code saying what tests need to be made to determine if an element needs to be added to a sum.

use the variable names row and column instead of i and j to make the code easier to understand.

Write down the row,column values that need to be added to sum the right diagonal elements.

Add comments to the code saying what tests need to be made to determine if an element needs to be added to a sum.

Norman Radder

Ranch Hand

Posts: 146

4

Khusbu Sinha

Ranch Hand

Posts: 117

1

Fred Kleinschmidt

Bartender

Posts: 571

9

posted 10 months ago

- 1

Why have two sets of nested for-loops? Just place both if-statements in the same inner loop (i.e., remove the word "else" from the "else if" your first code, and eliminate the unnecessary final else clause.

posted 10 months ago

- 1

You don't need a nested loop for these calculations.

Given and 3x3 matrix

m[0][0]

m[1][1]

m[2][2]

Given the same matrix, to get what you call the right diagonal sum, you need to add these elements:

m[0][2]

m[1][1]

m[2][0]

You can already identify a definite pattern of change with the above examples but if not, listing out the elements involved in calculating diagonal sums for 4x4, 5x5, etc. matrices should help you discover what the pattern is.

you should know that it's legal to have multiple loop variable in a for-loop. Try this:

The increment clause of the for-loop header can have different expressions that change the loop variables independently. If you use this technique, you won't need a nested for-loop or an if-statement.

EDIT: in fact, if you're dealing with a

Given and 3x3 matrix

`m`, to get what you call the left diagonal sum, you need to add these elements:m[0][0]

m[1][1]

m[2][2]

Given the same matrix, to get what you call the right diagonal sum, you need to add these elements:

m[0][2]

m[1][1]

m[2][0]

You can already identify a definite pattern of change with the above examples but if not, listing out the elements involved in calculating diagonal sums for 4x4, 5x5, etc. matrices should help you discover what the pattern is.

you should know that it's legal to have multiple loop variable in a for-loop. Try this:

The increment clause of the for-loop header can have different expressions that change the loop variables independently. If you use this technique, you won't need a nested for-loop or an if-statement.

EDIT: in fact, if you're dealing with a

*square*matrix that has the same number of rows as columns, you won't even need multiple loop variables. You already have an idea of the relationship between the row and column indices for the right diagonal sum. The left diagonal sum is more straightforward since the row and column indices of the diagonal elements in a square matrix are always the same.*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

posted 10 months ago

- 1

Also, you don't need nested for loops to calculate those sums.

Your code iterates over all elements in the matrix, checks whether they are on one of diagonals and adds those values to appropriate sums.

What if your code iterated only over elements on the diagonal in the first place?

Your code iterates over all elements in the matrix, checks whether they are on one of diagonals and adds those values to appropriate sums.

What if your code iterated only over elements on the diagonal in the first place?

Khusbu Sinha

Ranch Hand

Posts: 117

1

Khusbu Sinha

Ranch Hand

Posts: 117

1

posted 10 months ago

I have a doubt. Can I use this technique for iterating over 2D arrays with different sizes of rows and columns and in `initializing` the elements of a 2D array?

Also, is it legal to use two boolean conditions in the `for` loop?

Eg: for (i=0,j=0; i<10, j<20; i++,j++)

Is this`for` statement correct?

Junilu Lacar wrote:

You should know that it's legal to have multiple loop variable in a for-loop. Try this:

The increment clause of the for-loop header can have different expressions that change the loop variables independently. If you use this technique, you won't need a nested for-loop or an if-statement.

I have a doubt. Can I use this technique for iterating over 2D arrays with different sizes of rows and columns and in `initializing` the elements of a 2D array?

Also, is it legal to use two boolean conditions in the `for` loop?

Eg: for (i=0,j=0; i<10, j<20; i++,j++)

Is this`for` statement correct?

Campbell Ritchie

Marshal

Posts: 56529

172

posted 10 months ago

There is no such thing as a 2D array in Java®; what you are seeing is arrays of arrays. You can try that technique, but you will not iterate every element of every array. If you are going to iterate arrays of arrays, it is better to nest loops. If the included arrays differ greatly in size, there is a risk of an out of bounds exception.Khusbu Sinha wrote:. . . Can I use this technique for iterating over 2D arrays . . .

No. You can only have one continuation condition. You can of course combine those two tests into one, however:-Also, is it legal to use two boolean conditions in the `for` loop?

Eg: for (i=0,j=0; i<10, j<20; i++,j++)

Is this`for` statement correct?

`. . . i < 10`

__&&__j < 20 . . .