generate functionWed, 08 Feb 2023

The trace of a square matrix is the sum of the elements of the main diagonal. Write a program that prints the trace of a given square matrix. Input data format The input to the program is a natural number. n is the number of rows and columns in the matrix, then the matrix elements (integers) line by line, separated by a space.

def trace(n: int, mat: list) -> int: sum = 0 for i in range(0, n): sum += mat[i][i] return sum

Questions about programming?Chat with your personal AI assistant