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_matr(list_): trace = 0 for i in range(len(list_)): trace += list_[i][i] return trace

Questions about programming?Chat with your personal AI assistant