Answered step by step
Verified Expert Solution
Question
1 Approved Answer
using python Question III: In this question, you are going to write methods that operate on matrices. The program reads values of matrices A, B,
using python
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.
import random import sys def readMatrix(number of Rows , number of Columns, file): matrix - 0 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(natrix): for row in range(len(matrix)): for column in range(len(matrix[row])): print(format(natrix[row][column],"d"), end = -) print() # Print new Line def fillMatrixRandomly(number OfRows, number of Columns ): matrix - 1) * Create an empty list for row in range(numberOfRows): matrix.append(11) Add an empty new row for column in range(numberOfColumns): matrix[row].append(randon.randint(0, 99) return matrix def generatezeroMatrix(numberOfrous, number of Columns): natrix for in range(numberOfRows) ] for sin range(numberofcolumns) return matrix def addMatrix(A,B): eneratezeromatrix (len(A), len(Aten for row in range(len(A)) for column in range(len(A[row])); Crow][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 in") f.open("inputs.txt","r") * Read More line - f.readline) number of rows , numberOfColumns - [int (x) for in line.split(""}} A readMatrix(number of rows , number of Columns, f) print(" **** Matrix A*** printMatrix(A) Read Matrix line - f.readline() number of rows, number of Columns (int(x) for in line.split() B. readMatrix(number of Rows, number of Columns, ) print(" **** Matrix) printMatrix() Read Matrix line - f.readLine() number of rows, nunberorColumns [Int(x) for xin line.split CreadMatrix(number of Rows, number of Colunns, ) print(" **** Matrix ****) printMatrix(c) # Generate 4x matrix from randombers Df111 MatrixRandomly(number of rows, number of Columns) print(" **** Matrix D ***** printMatrix(D) Compute (AB). Transpose(C) D-A print(" *** Computing 5 (A+B) Transpose()) - A ") T1 - addMatrix(A,B) print(" *** Matrix Ti - (A) **** printMatrix(T1) #write the rest of code wain() 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 naxofElements(A) def subtractMatri(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