programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Finding the sum of right diagonal elements of a square matrix

Khusbu Sinha
Ranch Hand
Posts: 117
1
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.

Ranch Hand
Posts: 146
4
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.

Ranch Hand
Posts: 146
4
Also try debugging the code by printing out the values of the row and column in the ending else statement where the continue statement is.

fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
• 2
big hint...only ONE of the blocks in your if-else if - else condition will run for each i,j pair...

Khusbu Sinha
Ranch Hand
Posts: 117
1
fred rosenberger wrote:big hint...only ONE of the blocks in your if-else if - else condition will run for each i,j pair...

Thanks for the BIG HINT. It actually solved the problem.

Here is my final code:

">

Fred Kleinschmidt
Bartender
Posts: 571
9
• 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.

Junilu Lacar
Sheriff
Posts: 11477
180
• 1
You don't need a nested loop for these calculations.

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.

Paweł Baczyński
Bartender
Posts: 2077
44
• 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?

Khusbu Sinha
Ranch Hand
Posts: 117
1
Thanks a lot Fred, Junilu and Pawel for your valuable pieces of advice in simplifying my code. I learnt many useful tips from all of you.

Here is my updated code that runs successfully. Please do have a look.

Khusbu Sinha
Ranch Hand
Posts: 117
1
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
Khusbu Sinha wrote:. . . Can I use this technique for iterating over 2D arrays . . .
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.
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?
No. You can only have one continuation condition. You can of course combine those two tests into one, however:-
. . . i < 10 && j < 20 . . .

Khusbu Sinha
Ranch Hand
Posts: 117
1
Thanks Campbell for clarifying my doubts.

Campbell Ritchie
Marshal
Posts: 56529
172
My pleasure