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 381: 11.1

Answer

code

Work Step by Step

# 11.1 (Sum elements column by column) Write a function that returns the sum of all the # elements in a specified column in a matrix using the following header: # def sumColumn(m, columnIndex): # Write a test program that reads a 3*4 matrix and displays the sum of each column. def sumColumn(m, columnIndex): sum = 0 for row in range(len(m)): sum += m[row][columnIndex] return sum m = [] for i in range(3): row = input("Enter a 3-by-4 matrix row for row " + str(i)+": ").split() m.append([float(x) for x in row]) for i in range(4): s = sumColumn(m, i) print("Sum of the elements for column", i, 'is', s)
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.