Question 3:
In this question you will develop a set of functions to manipulate 3x3 matrices in
various ways.
a) Write a function print_matrix(…) which takes as an input a two-dimensional
array of size nxn of type double representing a matrix, and an integer n
corresponding to the size of this array. The function should print the elements of this
array to the standard output displaying it in the form of a matrix. For example, we
expect:
1 0
0 1
for a 2x2 array a with elements a[0][0]=1, a[0][1]=0, a[1][0]=0,
a[1][1]=1. The function print_matrix(…) does not return any value.
(2 marks)
b) Write a function transpose(…) which takes as an input a 3x3 array a of type
double representing a matrix, and returns a transpose of this matrix stored in the
same input variable a. Note the transpose of a square matrix 𝑎𝑖𝑗 is defined as:
(𝑎)𝑖𝑗
𝑇 = 𝑎𝑗𝑖.
(2 marks)
c) Write a function det2(…) which takes as an input a 2x2 array a of type
double representing a matrix, and returns a variable of type double equal to the
determinant of the input matrix:
|𝑎| = |
𝑎00 𝑎01
𝑎10 𝑎11
| = 𝑎00𝑎11 − 𝑎01𝑎10.
(2 marks)
d) Write a function cofactor(…) which takes as an input a 3x3 array a of type
double representing a matrix, and two integers i and j, and returns a variable of
type double equal to the cofactor 𝑐𝑖𝑗 of the matrix a. The cofactor 𝑐𝑖𝑗 is defined as:
where 𝑚𝑖𝑗 is the minor of the element 𝑎𝑖𝑗 of the 3x3 matrix a, equal to the
determinant of the reduced 2x2 matrix obtained by removing all elements of the ith
row and jth column of the matrix a. Thus the notation 𝑎𝑘𝑙
(𝑖𝑗)
in the above equation
means the elements of the matrix remaining after removing the ith row and jth
column from the matrix a. Use function det2(…) developed above to simplify your
code.
(4 marks)
e) Write a function det3(…) which takes as an input a 3x3 array a of type
double representing a matrix, and returns a variable of type double equal to the
determinant of the input matrix a. To evaluate the determinant |𝑎| of a matrix 𝑎 use
the Laplace expansion method:
|𝑎| = ∑𝑎𝑘𝑖𝑐𝑘𝑖
2
𝑘=0
𝑖 = 0 𝑜𝑟 1 𝑜𝑟 2
where 𝑐𝑘𝑖 is the cofactor of the matrix 𝑎 as defined above. Note that the index i is
fixed and the result is independent of the choice of i. Use the function
cofactor(…) developed as part of the previous question to simplify your code.
(4 marks)
f) Write a function inverse(…) which takes as an input a 3x3 array a of type
double representing a matrix, and returns an inverse of this matrix stored in the
same input variable a. To calculate the inverse matrix 𝑎
−1 of a matrix 𝑎, note that the
elements of the inverse matrix can be expressed as:
where 𝑐𝑖𝑗 denotes the cofactor of the matrix a and |𝑎| is the determinant of the matrix
a. Use your functions det3(…) and cofactor(…) developed above to simplify
your code.
(4 marks)
g) Write a function multiply(…) which takes as an input two 3x3 arrays of type
double representing two matrices. The function should calculate the matrix product
of the two matrices and print it to the standard output using the function
print_matrix(…) developed above. The function multiply(…) does not
return any value.
(2 marks)
6
Example of incomplete function main() could look like this:
int main(void)
{
…
…
printf("\nOriginal matrix:\n");
print_matrix(3, a);
printf("Determinant of the matrix:\n");
printf("%f\n", det3(a));
printf("\nTranspose of the matrix:\n");
transpose(a);
print_matrix(3, a);
printf("Inverse of the matrix:\n");
transpose(a); // transpose back to obtain original
matrix
for(i=0; i