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):

matrix = [ [ 0 for i in range(numberOfRows) ] for j in range(numberOfColumns) ] return matrix

def addMatrix(A,B):

C = generateZeroMatrix (len(A),len(A[0])) for row in range(len(A)):

for column in range(len(A[row])):

C[row][column] = A[row][column] + B[row][column] return C

# 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() numberOfRows, numberOfColumns = [int(x) for x in line.split(' ')] B = readMatrix(numberOfRows, numberOfColumns, f) print(" **** Matrix B **** ") printMatrix(B)

# Read Matrix C 

line = f.readline() numberOfRows, numberOfColumns = [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(T1) 
# 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) 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:

Reading data from inputs.txt file in current directory 

**** Matrix A **** 55 55 55 56 66 20 12 67 77 15 25 78 88 12 13 89

**** Matrix B **** 

1234 2222 3333 4444

**** Matrix C **** 50 12 75 14 55 24 24 25 33 34 35 36 44 45 46 47

**** Matrix D **** 19 46 14 61

54 81 91 34 46 60 95 40 52 74 95 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 T3 =(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 

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!