Question: # -------------------------------------- # -------------------------------------- # Do not change anything below this line # -------------------------------------- def print_header(columns): line = + for i in range(columns): line +=
# -------------------------------------- # -------------------------------------- # Do not change anything below this line # -------------------------------------- def print_header(columns): line = "+" for i in range(columns): line += "---+" print(line) # -------------------------------------- def read_matrix(input_file): matrix = {} line = input_file.readline().split() while line[0] != "stop": row = int(line[0]) column = int(line[1]) value = int(line[2]) matrix[(row, column)] = value line = input_file.readline().split() return matrix # -------------------------------------- def main (file_name): input_file = open(file_name, "r") line = input_file.readline().split() rows = int(line[0]) columns = int(line[1]) matrix_1 = read_matrix(input_file) print_matrix(matrix_1, rows, columns, "Matrix 1") matrix_2 = read_matrix(input_file) print_matrix(matrix_2, rows, columns, "Matrix 2") matrix_3 = subtract_matrices(matrix_1, matrix_2, rows, columns) print_matrix(matrix_3, rows, columns, "Matrix 1 - Matrix 2") print("Matrix 3 =", matrix_3) # -------------------------------------- main ("sparse-matrix.txt") Take the program above and modify it by adding the two missing functions. When the program is run on the output file above, it should produce this output.
Matrix 1 +---+---+---+---+ | 1| 2| 0| 0| +---+---+---+---+ | 0| 3| 4| 0| +---+---+---+---+ | 0| 0| 5| 6| +---+---+---+---+ Matrix 2 +---+---+---+---+ | 0| 1| 2| 0| +---+---+---+---+ | 0| 0| 3| 4| +---+---+---+---+ | 6| 0| 0| 6| +---+---+---+---+ Matrix 1 - Matrix 2 +---+---+---+---+ | 1| 1| -2| 0| +---+---+---+---+ | 0| 3| 1| -4| +---+---+---+---+ | -6| 0| 5| 0| +---+---+---+---+ Matrix 3 = {(0, 0): 1, (0, 1): 1, (0, 2): -2, (1, 1): 3, (1, 2): 1, (1, 3): -4, (2, 0): -6, (2, 2): 5} Using this txt file.
3 4 2 2 5 1 2 4 0 1 2 0 0 1 1 1 3 2 3 6 stop 0 1 1 0 2 2 1 2 3 1 3 4 2 0 6 2 3 6 stop
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
