# Diagonal Difference Calculator

Let’s consider a square matrix of n x n. (n columns and n rows). The diagonal difference of a square matrix is the absolute difference between the sums of its diagonal.

Let’s look at an example based on the following 3 x 3 square matrix:

Here is how we would calculate the diagonal difference for this matrix:

#### Python Challenge

We have created a Python script that asks the user to input the dimension n of a square matrix (any positive integer value). Our python scripts generates a n x n matrix using random values and displays it on screen as a 2D grid.

Your task is to complete this script to automatically calculate the diagonal difference of the given matrix. (Make sure this value is always a positive value!)

#### Python Code

Your first extension task is to create an additional function to calculate the sum of the 4 corner values of the square matrix:

Your second extension task is create an additional function to calculate the average value of all values stored in the matrix.

[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ display_to=”not_subscribed” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

#### Python Code – Diagonal Difference

```# Diagonal Difference Calculator - http://www.101computing.net/diagonal-difference-calculator/

from random import randint

#A procedure to generate a random square n x n matrix
def initialiseMatrix(matrix,n):
for row in range(0,n):
matrix.append([])
for col in range(0,n):
value = randint(0,99)
matrix[row].append(value)

#A procedure to display a square matrix as a 2D grid
def displayMatrix(matrix):
n = len(matrix)
for row in range(0,n):
st="| "
for col in range(0,n):
if matrix[row][col]<10:
st = st + str(matrix[row][col]) + "  | "
else:
st = st + str(matrix[row][col]) + " | "
print(st)

def diagonalDifference(matrix):
sum1 = 0
sum2 = 0
n = len(matrix)
for i in range(0,n):
sum1 = sum1 + matrix[i][i]
sum2 = sum2 + matrix[n-i-1][i]
if sum2>sum1:
return sum2 - sum1
else:
return sum1 - sum2

### Main Program Starts Here ###
n = int(input("Enter the dimension n of your square matrix. (e.g. 3): "))

matrix = []
initialiseMatrix(matrix,n)
displayMatrix(matrix)
difference = diagonalDifference(matrix)
print("The diagonal difference for this matrix is: " + str(difference))
```

#### Extension Task 1: The sum of all four corners

Add the following code to the initial code…

```def addCorners(matrix):
n = len(matrix)
sum = matrix[0][0] + matrix[0][n-1] + matrix[n-1][0] + matrix[n-1][n-1]
return sum

print("The sum of all four corners is: " + str(corners))
```

#### Extension Task 2: Mean Value

Add the following code to the initial code…

```def meanValue(matrix):
n = len(matrix)
sum = 0
for row in range(0,n):
for col in range(0,n):
sum = sum + matrix[row][col]
mean = sum / (n**2)
return mean

mean = meanValue(matrix)
print("The average/mean value is: " + str(mean))
```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 3.8 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

As you found this challenge interesting...