Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PLEASE SOLVE BY PYTHON. THE OUTPUT MUST BE THE SAME AS THE OUTPUT GIVEN. In this question, you are going to write methods that operate
PLEASE SOLVE BY PYTHON. THE OUTPUT MUST BE THE SAME AS THE OUTPUT GIVEN.
In this question, you are going to write methods that operate on matrices. The program reads values of matrices A, B, and 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 20 12 67 77 15 25 78 88 12 13 89 44 1 2 3 4 2222 3333 4444 44 50 12 75 14 55 24 24 25 33 34 35 36 44 45 46 47 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(number of Columns): 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, number of Columns): matrix = [ [ for i in range(numberOfRows) ] for j in range(numberOfColumns) ] return matrix def addMatrix(A,B): C = generateZeroMatrix (len(A), len(A[@])) for row in range (len (A)): for column in range(len(A[row])): C[row][column] = A[row][column] + B[row][column] return # Redirect standard output device (console) to output.txt file # print statements will write into output. txt file sys.stdout = open('output.txt', 'w') print(" Reading data from inputs.txt file in current directory ") f = open("inputs.txt","r") # Read Matrix A line = f.readline() numberOfRows , numberOfColumns = [int (x) for x in line.split(' ')] A = readMatrix(numberOfRows, numberOfColumns, f) print(" **** Matrix A **** ") printMatrix(A) # Read Matrix B line - f.readline() number of Rows, number of Columns - [int(x) for x in line.split(' ')] B = readMatrix(numberOfRows, numberOfColumns, f) print(" **** Matrix B ****") printMatrix(B) # Read Matrix C line = f.readline() numberOfRows, number of Columns = [int(x) for x in line.split(' ')] C = readMatrix(numberOfRows, numberOfColumns, f) print(" **** Matrix C **** ") printMatrix(C) # Generate 4x4 matrix from random numbers. D = fillMatrixRandomly(numberOfRows, numberOfColumns) print(" **** Matrix D **** ") printMatrix(D) # Compute S = (A+B) Transpose(C) + D-A print(" *** Computing S = (A+B) Transpose(C) + D) - A *** ") # T1 = A + B T1 - addMatrix(A,B) print(" **** Matrix T1 - (A+B) ****") printMatrix(11) # Write the rest of code main() You are going to write the code for the following methods and the missing code in main(). def multiplyMatrix(A, B) def transpose(A) def maxOfElements(A) det 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:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started