Step 1 Use two nested loops to iterate on matrix elements.
Step 2 If counters match add the value at the array index to first diagonal sum.
If first counter matches the operation below add the value at the array index to second diagonal sum.
Step 3 Find the difference between first diagonal and the second diagonal.
Step 4 Return the absolute value of the difference.
//Computes absolute diagonal difference
//Input: 2d array of integers, size of the square matrix
//Output: absolute diagonal differnce of the matrix a
for i <- 0 to size do
for j <- to size do
if (i == j)
first_diagonal <- first_diagonal + a[i][j]
if (i == size - j - 1)
second_diagonal <- second_diagonal + a[i][j]
difference = first_diagonal - second_diagonal
return abs(difference)
Step 1 Use one loop to iterate on the matrix elements.
Step 2 Knowing the indices of the first and second diagonal compute sum.
Step 3 Find the difference between first diagonal and the second diagonal.
Step 4 Return the absolute value of the difference.
//Computes absolute diagonal difference
//Input: 2d array of integers, size of the square matrix
//Output: absolute diagonal differnce of the matrix a
for i <- 0 to size do
first_diagonal <- first_diagonal + a[i][i]
second_diagonal <- second_diagonal + a[size - i - 1][i]
difference = first_diagonal - second_diagonal
return abs(difference)