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.5

Answer

code

Work Step by Step

# 11.5 (Algebra: add two matrices) Write a function to add two matrices. The header of # the function is: # def addMatrix(a, b): # In order to be added, the two matrices must have the same dimensions and the # same or compatible types of elements. Let c be the resulting matrix. Each element # cij is aij + bij. l1 = input("Enter matrix 1: ").split() l2 = input("Enter matrix 2: ").split() # shape the input into a 3*3 matrix m1 = [] m2 = [] counter = 0 for i in range(3): m1.append([0] * 3) m2.append([0] * 3) for j in range(3): m1[i][j] = float(l1[counter]) m2[i][j] = float(l2[counter]) counter += 1 # add the matrices and store result in sum sum = [] for i in range(3): sum.append([0] * 3) for j in range(3): sum[i][j] = m1[i][j] + m2[i][j] for i in range(3): print(m1[i], end=' ') if i == 1: print(' +', end='') print(' ', end='') else: print('\t', end='') print(m2[i], end=' ') if i == 1: print(' =', end='') print(' ', end='') else: print('\t', end='') print(sum[i], end=' ') print()
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.