Question: Question III: In this question, you are going to write methods that operate on matrices. The program reads values of matrices A, B, and C
Question III:
In this question, you are going to write methods that operate on matrices. The program reads values of matrices A, B, and C stored in a file called inputs.txt. This file should be placed under current directory where you have the program. The first line before each matrix contains the number of rows and the number of columns as shown below.
44 55 55 55 56 66 66 66 67 77 77 77 78 88 88 88 89 44 1234 2222 3333 4444 44 11 12 13 14 22 24 24 25 33 34 35 36 44 45 46 47
As a first step, the program reads data for matrices A, B and C from inputs.txt file and write them into console. A fourth matrix D is generated randomly and printed. A partial program code is given below.
| import random import sys def readMatrix(numberOfRows , numberOfColumns, file): matrix = [] # Create an empty list | |||||||||||||||||||
| for row in range(numberOfRows): matrix.append([]) # Add an empty new row line = file.readline() | |||||||||||||||||||
| rowdata = [int(x) for x in line.split(' ')] for column in range(numberOfColumns): matrix[row].append(rowdata[column]) | |||||||||||||||||||
| return matrix def printMatrix(matrix): | |||||||||||||||||||
| for row in range(len(matrix)): for column in range(len(matrix[row])): print(format(matrix[row][column],"5d"), end = " ") print() # Print a new line def fillMatrixRandomly(numberOfRows,numberOfColumns ): matrix = [] # Create an empty list for row in range(numberOfRows): matrix.append([]) # Add an empty new row for column in range(numberOfColumns): | |||||||||||||||||||
| matrix[row].append(random.randint(0, 99)) return matrix def generateZeroMatrix(numberOfRows,numberOfColumns):
def multiplyMatrix(A, B) def transpose(A) def maxOfElements(A) def subtractMatrix(A, B) The program will calculate S = (A+B) * Transpose(C) - A) + D and find the maximum element in S. Complete the code given above so that it will produce an output as follows:
in python and mention the points that I can change. and please paste the code as it should be written in python |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
