Skip to content

Commit 43de7c4

Browse files
committed
Added more comments
Added more comments to explain the logic of the solution and also explain the method logic().
1 parent 2edfe6d commit 43de7c4

1 file changed

Lines changed: 14 additions & 9 deletions

File tree

GeeksForGeeks/diagonal.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,28 @@ void input ()throws IOException // A method to input the array order and elemen
5050
System.out.println("\n");
5151
}
5252
}
53-
void logic() // A method to print the diagonal elements..
53+
void logic() // A method to print the diagonal elements..
5454
{
55-
System.out.println("The diagonal elements of the matrix are");
56-
for(int i=0;i<p;i++)
57-
{ int j=i;
55+
/* The logic() method prints the elements on the diagonal one by one using their index positions
56+
Firstly starting with the number in the top most left corner. Then the number below it and the
57+
number diagonaly oposite to it is printed, thus moving diagonaly from left to right.
58+
*/
59+
60+
System.out.println("The diagonal elements of the matrix are");
61+
for(int i=0;i<p;i++) //This part of the for loop prints diagonal elements..
62+
{ int j=i; //until the principal diagonal(including elements on principal diagonal..
5863
for(int x=0;x<=i && x<q;x++,j--)
5964
{
60-
System.out.print(a[j][x]+"\t");
65+
System.out.print(a[j][x]+"\t");
6166
}
6267
System.out.println();
6368
}
64-
for( int i=1;i<q;i++)
69+
for( int i=1;i<q;i++) //This part is for the elements that are below the principal diagonal...
6570
{
6671
int j=p-1;
67-
for(int x=i;x<q;x++,j--)
68-
{
69-
System.out.print(a[j][x]+"\t");
72+
for(int x=i;x<q;x++,j--) //Doing a dry run of the code with some smaple inputs
73+
{ // is suggested for understanding the logic() method
74+
System.out.print(a[j][x]+"\t");
7075
}
7176
System.out.println();
7277
}

0 commit comments

Comments
 (0)