Question: Hey there! I've already written the codes, and I need help with the RED MARKED place. I don't know how to deal with the def

Hey there! I've already written the codes, and I need help with the RED MARKED place.

I don't know how to deal with the def main() part and argv with the sys.exit()

Please help me to fix the code and make it fit the description.

Thank you!

Hey there! I've already written the codes, and I need help with

******Below my codes with some errors******

# Header comment section (typical assignment info)

import sys

#The matrices A and B must be declared as empty lists: A = [] and B = []

A = []

B = []

# read_matrices is a function that takes two parameters A and B

# it reads the file and store the matrix values in it

# it return A and B m,n,p values

print("Program #3, csscxxx, xxx")

# This function reads m, n, and p from the datafile.

# Then matrices A and B are filled from the datafile.

# Matrix C is then allocated size m x p.

# The values for m, n, and p are local values filled in by this function

# PARAMETERS in order are:

# list matrix A

# list matrix B

# RETURN matrix C

#

def read_matrices(A,B):

filename = sys.argv[-1]

file1 = open(filename, 'r')

Lines = file1.readlines()

m = int(Lines[0])

n = int(Lines[1])

p = int(Lines[2])

for i in range(3,m+3):

# add the values to A

A.append(list(map(int,(Lines[i].split()))))

for i in range(m+3,n+m+3):

# get data to B

B.append(list(map(int,(Lines[i].split()))))

return A,B,m,n,p

# The two matrices A and B are multiplied, and matrix C contains the

# result.

# PARAMETERS in order are:

# list Matrix A

# list Matrix B

# list Matrix C (all zeros at this point)

#

def mult_matrices(A,B,C):

for i in range(m):

for j in range(p):

C[i][j] = 0

# iterate + compute the sum

for k in range(n):

C[i][j]+=A[i][k]*B[k][j]

return C

# This function prints a matrix. Rows and columns should be preserved.

# PARAMETERS in order are:

# list The matrix to print

#

def print_matrix(arr):

for i in range(len(arr)):

for j in range(len(arr[i])):

print("{:>5}".format(arr[i][j]),end="")

print()

# This function begins execution of program.

# Verify data input filename provided on command line: len(sys.argv)

# If error, output message for user: Usage: p3.py dataFileName'

# and quit, using sys.exit()

#

# Declare A, B, call read_matrices to initialize A, B, and store

# return value as C

#

# Print A and B contents

#

# Call mult_matrices

#

# Print result contents

#

# Main function

if __name__=="__main__": filename = sys.argv[-1] """ In place of If/else codition we can also use try/except block in python to raise an expected Error. """

if filename==len(sys.argv): continue else: print("Error: - Usages: p3.py DataFileName") sys.exit() ### In place of sys.exit() we can also terminate the program using break statement. A,B,m,n,p = read_matrices(A,B) C = [[0]*p for i in range(m)] mult_matrices(A,B,C) print("Matrix A contents: ") print_matrix(A) print("Matrix B contents: ") print_matrix(B) print("Matrix A * B is: ") print_matrix(C)

The Program: For this assignment, you will multiply two matrices and print the result. If A is an mx n matrix, and B is an n xp matrix, then the product matrix C, which is A x B, is defined to be the m xp matrix whose entry in the ith row and the jih column is the sum of the products of corresponding entries of the ith row of A and the jih column of B. A standard algorithm is: for (int i=0; i

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!