Starting from:
$30

$24

CSC3100 Assignment 4 Solved

Problem Description:

Write a program to read two sparse matrices from two input files, add the two matrices, and write the result matrix into the output file. The input and output files have the following formats.


3, 2

1 1:7 2:9

    2 :

    3 2:-5


Here is an illustration of the file.

    • The first line of the file (“3, 2”) represents the size of the matrix is 3 rows and 2 columns.

    • The second line starts with 1, which means the 1st row of the matrix. “1:7” means the 1st column of the row is 7; “2:9” means the 2nd column of the row is 9.
    • The third line starts with “2”, which means the 2nd row of the matrix. There are no other elements except a “:”, which means all the columns of the row are “0”.

    • The fourth line starts with “3”, which means the 3rd row of the matrix. “2:-5” means the 2nd column of the row is -5.

    • All elements that are not listed in the file are treated as 0.
Therefore, the file represents the following
7
9


matrix:


�0
0


0
−5

Functional Requirement

Write a program called AddSparseMatrix.java that reads two matrices from two input files, add the two matrices, and write the result into the output file.
Note that in our tests, the input matrices are sparse. Most matrix elements (around 95% in tests A and B, around 99% in test C) are zero. All non-zero elements are integers.


java AddSparseMatrix input1.txt input2.txt output.txt


In this specific example, the arguments “input1.txt” and “input2.txt” denotes two files that give the two input matrices. You can assume the two input matrices are always of the same size. The argument “output.txt” denotes the name of the file to write the result matrix.

Program Template:

There is no program template for this assignment.

Appendix

    1. input1.txt

    2. input2.txt

    3. output.txt

More products