Answered step by step
Verified Expert Solution
Link Copied!

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 image text in transcribed
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.
image text in transcribed
image text in transcribed
image text in transcribed
M THIL 4 | 1. EET | Main Mprnanin Mehnapahars , 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 Cfrom inputs.txt file and write them into console. A fourth matrix Dis generated randomly and printed. A partial program code is given 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: Reading data from inputs.txt file in current directory 56 67 78 89 12 4 2 3 4 *** Matrix A **** 55 55 55 66 20 12 77 15 25 88 13 **** Matrix B **** 1 2 3 2 2 2 3 3 3 4 4 4 **** Matrix C **** 5e 12 75 55 24 24 33 34 35 44 45 46 **** Matrix D **** 19 46 14 54 81 91 46 95 52 74 95 14 25 36 47 61 34 40 31 60 *** 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 T3 =(A+B) transpose(C) **** 8674 7340 7976 19517 5689 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

101 Database Exercises Text Workbook

Authors: McGraw-Hill

2nd Edition

0028007484, 978-0028007489

More Books

Students also viewed these Databases questions