CS60 Homework 12 ------------------------- Due Mon Nov 10th The Matrix ------------------------- In honor of the last in this fine series of science fiction films, we will be writing a simple matrix class that stores arbitrary sized 2d matrices and can perform addition, multiplication, and assignment operations via overloaded operators. For a nice tutorial on matrix operations, consult the following URL: http://www.mai.liu.se/~halun/matrix/matrix.html The task is to write a matrix class with the =, *, and + operators overloaded, as well as any constructors, destructors, accessor or mutator member functions that you feel are required. The matrix class must support arbitrary sized 2d matrices, which can be set upon declaration (using a constructor). The matrix class must also provide some way of initializing the matrix, how to initialize the data in a matrix class is up to you but should allow the user of the program to input numbers from the keyboard. Matrix Addition -------------------- C = A + B Requirement: both matrices A and B must have the same dimensions. The resulting matrix C has the same dimensions as both A and B. To perform matrix addition, each element of matrix A is added to the corresponding element of matrix B and the result is stored in the corresponding element of matrix C. For example: A = 1 2 3 4 B = 5 6 7 8 A + B = 6 8 10 12 or A = 1 2 3 4 5 6 7 8 B = 4 3 2 1 8 7 6 5 A + B = 5 5 5 5 13 13 13 13 Matrix Multiplication ------------------------ A * B = C Requirement: the number of COLUMNS of A must match the number of ROWS of B. The resulting matrix C has the number of ROWS of A, and the number of COLUMNS of B. A = 1 2 3 4 5 6 B = 7 8 C = 1*5 + 2*6 + 3*7 + 4*8 = 70 1 2 3 4 A = 5 6 7 8 1 2 3 B = 4 5 6 7 8 9 10 11 12 C = 70 80 90 158 184 210 Turnin ----------------------- The turnin tage for this homework is 'hw12' and must include your program code and header files, a makefile, a typescript showing compilation and execution of the program, and an optional README with special instructions for the grader. Sample Output ----------------------- Note that your sample output need not look exactly like this, depending on how you ask the user to initialize your matrices. However, you must show addition and multiplication of the same matrices shown here and as examples above. [nurmi@localhost hw12]$ make clean rm -f *.o *.a *~* hw12 [nurmi@localhost hw12]$ make g++ -I./ -ansi -pedantic -c -o matrix.o matrix.cpp making libmatrix ar rs libmatrix.a matrix.o ar: creating archive libmatrix.a g++ -I./ -ansi -pedantic -c -o hw12.o hw12.cpp making all g++ hw12.o -L./ -lmatrix -o hw12 [nurmi@localhost hw12]$ ./hw12 enter row number of matrix A: 2 enter col number of matrix A: 4 Enter 0,0 value for matrix A: 1 Enter 0,1 value for matrix A: 2 Enter 0,2 value for matrix A: 3 Enter 0,3 value for matrix A: 4 Enter 1,0 value for matrix A: 5 Enter 1,1 value for matrix A: 6 Enter 1,2 value for matrix A: 7 Enter 1,3 value for matrix A: 8 enter row number of matrix B: 2 enter row number of matrix B: 4 Enter 0,0 value for matrix B: 4 Enter 0,1 value for matrix B: 3 Enter 0,2 value for matrix B: 2 Enter 0,3 value for matrix B: 1 Enter 1,0 value for matrix B: 8 Enter 1,1 value for matrix B: 7 Enter 1,2 value for matrix B: 6 Enter 1,3 value for matrix B: 5 enter row number of matrix C: 4 enter row number of matrix C: 3 Enter 0,0 value for matrix C: 1 Enter 0,1 value for matrix C: 2 Enter 0,2 value for matrix C: 3 Enter 1,0 value for matrix C: 4 Enter 1,1 value for matrix C: 5 Enter 1,2 value for matrix C: 6 Enter 2,0 value for matrix C: 7 Enter 2,1 value for matrix C: 8 Enter 2,2 value for matrix C: 9 Enter 3,0 value for matrix C: 10 Enter 3,1 value for matrix C: 11 Enter 3,2 value for matrix C: 12 result of a + b --------------- 5 5 5 5 13 13 13 13 result of a * c --------------- 70 80 90 158 184 210 [nurmi@localhost hw12]$