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
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:
Extension Task #2
Your second extension task is create an additional function to calculate the average value of all values stored in the matrix.
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area