- You should implement a simple matrix calculator.
- The maximum score you can get from this project is
200 points, if you implement all the requirements.
- Do everything by your own exertion!
- You should implement the following functions:
– matrix creation
– matrix deletion
– matrix transpose
– matrix addition
– matrix multiplication
– matrix scalar multiplication
– to save/load/show matrices
- When you create a matrix, make sure that your matrix variable is created in the heap space. That is,
you should use “malloc” or “new” to create a matrix. All the matrices in this project should be
- You should implement 3 different creation functions:
create and fill all the elements in the matrix with 0.
create and fill all the elements in the matrix with 1.
create and fill all the elements in the matrix with randomly generated numbers.
A = zeros(3, 5); // A is a matrix with 3 rows and 5 columns
B = rand(4, 4); // B is a matrix with 4 rows and 4 columns
- Since your matrix variable is in the heap space, you should
be in charge of deleting it when the variable is no more needed. Make sure that the memory space for the input
- The output of the transpose function should be a newly allocated matrix,
i.e., you should create a new matrix inside the transpose function and return the matrix whose elements are filled with those of transpose of the matrix A.
- You should make sure that the other functions like addition and multiplication to be implemented in this way
transpose given matrix variable.
R = transpose(A)
- Matrix addition function will take two or three inputs(bonus points)
and add them together. Make sure that the size of the input matrices are equal. Otherwise the return value should be NULL.
add all the given matrices.
R = add(A, B)
R = add(A, B, C)
- Matrix multiplication function will take two or three inputs(bonus
points) and multiply them together. Make sure that the size of the input matrices are valid to be computed. Otherwise the return value should be NULL.
multiply all the given matrices.
R = multiply(A, B)
Matrix Scalar Multiplication
- Matrix scalar multiplication function will take a matrix
variable and a scalar variable, and multiply them.
- multiply (use function overloading)
multiply all the given matrices.
R = multiply(A, c) // A is a matrix and c is a scalar
- As the name suggests, matrix save/load function will save/load the computed a
matrix into/from a formatted file. There’s no specific format for these functions. You may define the format by yourself.
load a matrix with the given filename.
save a matrix into a file with the given filename.
load(“A.txt”) // load the matrix A from the file “A.txt”
save(A, “A.txt”) // save the matrix A into the file “A.txt”
Displaying a Matrix
- You should implement a function to show a matrix.
show the given matrix. There should be 3 spaces between each element.
show(A) // show the matrix A
1 2 3 // result of show function with
// 2 by 3 matrix A (Row-major matrix)
Creation Deletion Transpose Addition with two inputs
Multiplication with two inputs
20 pts 10 pts 10 pts 10 pts 15 ptsMultiplication Save Load Show
5 pts 10 pts 10 pts 10 pts
Binary file save/load
Implement bsave/bload for binary file to be saved/loaded. (You should use ios::binary option)
Addition/Multiplication with three inputs
Implement addition/multiplication with three inputs.
Efficient multiplication with three inputs
Considering the order of multiplication of given three matrices, you should implement the multiplication with three inputs efficiently.
For example, A is a 100000 x 5 matrix, B is a 5 x 5 matrix, and C is a
5×1 matrix. In this case, (A*(B*C)) will be much more efficient than
Implement all the functions specified with sparse matrix form. If you don’t know what sparse matrix is, you can refer to the Wikipedia
( https://en.wikipedia.org/wiki/Sparse_matrix )
skeleton code will be given. It consists of three files: main.cpp,
matrix.h, and matrix.cpp
You can test the functions which you implemented in the main function. The sample test code is given.
Do not touch this file!!
You should implement the functions in this file.
- Submit matrix.cpp only!