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:
sqaure-matrix
Here is how we would calculate the diagonal difference for this matrix:
diagonal-difference

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

Extension Task #1

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

Extension Task #2

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

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 14

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

As you found this challenge interesting...

Follow us on social media!

Tagged with: