Answered step by step
Verified Expert Solution
Link Copied!

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.

image text in transcribedimage text in transcribedimage text in transcribed

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 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(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: Reading data from inputs.txt file in current directory **** Matrix A **** 55 55 55 66 20 12 77 15 25 88 12 13 **** Matrix B **** 1 3 2 2 3 3 56 67 78 89 NN WN 4 2 3 4 14 25 36 47 45 **** Matrix C **** 50 12 75 55 24 24 33 34 35 44 46 **** Matrix D **** 19 46 14 54 81 46 60 95 52 74 95 91 61 34 40 31 *** Computing S = (A+B) * Transpose(C) A) + D *** **** Matrix T1 = (A+B) **** 56 57 58 60 68 22 14 69 80 18 28 81 92 16 17 93 **** Matrix T2 = Transpose(C) **** 50 55 33 44 12 24 34 45 75 24 35 46 14 25 36 47 **** Matrix 13 = (A+B) * transpose(C) **** 8674 7340 7976 10517 5680 6329 5966 7869 7450 7529 7148 9425 7369 8177 7523 9921 Maximum Element in S = 10522

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

What is the difference between a joint product and a by-product.

Answered: 1 week ago

Question

=+7 What is the overall cost of the international assignment?

Answered: 1 week ago