Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 11 - Multidimensional Lists - Programming Exercises - Page 382: 11.2

Answer

code

Work Step by Step

# 11.2 (Sum the major diagonal in a matrix) Write a function that sums all the numbers # of the major diagonal in an matrix of integers using the following header: # def sumMajorDiagonal(m): # The major diagonal is the diagonal that runs from the top left corner to the bottom # right corner in the square matrix. Write a test program that reads a matrix and # displays the sum of all its elements on the major diagonal. def sumMajorDiagonal(m): sum = 0 for i in range(len(m)): sum += m[i][i] return sum m = [] for i in range(4): row = input("Enter a 4-by-4 matrix row for row " + str(i + 1) + ": ").split() m.append([float(x) for x in row]) print("Sum of the elements in the major diagonal is", sumMajorDiagonal(m))
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.