Sample Sample Final Exam COMP2521
[Instructions] [C language]
[Q1] [Q2] [Q3] [Q4] [Q5] [Q6] [Q7] [Q8]

Question 1

In the q1 directory is an implementation of a simple Matrix ADT, along with a main program to test it. The ADT contains functions to create, read, and remove matrices, as well as addition and multiplication functions to combine pairs of compatible matrices.

Your task for this question is to implement the multiply() function, which is currently incomplete. For two matrices X (with dimensions M×N) and Y (with dimensions N×P), multiplication produces a matrix Z (with dimensions M×P) whose elements are defined as follows:

Z=X*Y, where Z[i,j] = Sum{k=0..N-1}(X[i][k] * Y[k][j])

The multiply() function takes two matrices as parameters, checks their compatibility, and then multiples the two matrices, storing the result in a newly-created matrix, and returning the new matrix as the result.

The overall structure of the multiply() function is somewhat like the structure of the existing add() function. But note that the compatibility check for matrix multiplication is different to the compatibility check for matrix addition. Note also that all output, including error messages, should be written to the standard output.

The q1 directory contains the following:

The Makefile compiles the Matrix ADT and the main program in main.c to produce an executable program called q1. This program has three command-line arguments: an operation (add or mul), and the names of two files, where each file contains data for one matrix.

As supplied, the q1 program can perform matrix addition on two matrices which have the same number of rows and columns. A simple example of using this program would be:

$ ./q1 add m0 m0
Matrix X
[     1     2 ]
[     3     4 ]
Matrix Y
[     1     2 ]
[     3     4 ]
[     2     4 ]
[     6     8 ]

You compile and test the q1 program using the following commands:

$ make q1   # build the q1 program
$ check q1  # apply the tests in the tests directory to the q1 program

You can find out more about the behaviour of the q1 program by looking at the files in the tests directory. The files named tX are test scripts. Each test script has a corresponding file tX.exp which contains the expected output from running that test. If you want to run the tests individually, use commands like:

$ sh tests/t1

Note that test t3 is supposed to produce an error; it is trying to multiply incompatible matrices.

After you run the tests, additional files will appear in the tests directory: tX.out contains the output of running test tX.

You can add debugging code to main.c or Matrix.c if you want, but make sure that you remove it before testing and submitting, otherwise your output won't match the expected output and you'll fail all of the tests. You can also add any auxiliary functions to Matrix.c that you think are necessary.

Once you are satisfied with your program, submit it using the command:

$ submit q1

This will make a copy of the Matrix.c file from the q1 directory as your answer for this question. You can run the submit command as many times as you like, but make sure that your final submission compiles without any errors or warnings. Test your program thoroughly, possibly using test cases additional to those supplied. Your program will be tested using additional test cases to the examples in the q1/tests directory.