You can also use CodeCrunch system to
submit and self-grade your work. Note that CodeCrunch is only used
for introductory and take-home labs. Sit-it labs will be graded by lab TAs. Test your programs thoroughly with your own
input data before submitting to CodeCrunch. Do not
use CodeCrunch as a debugging tool. For
this lab, the number of allowed submissions is set to 100.
Please post any questions you have on IVLE forum, under Labs heading.
1. There was a bug when using IE to
submit via CodeCrunch. If you encounter that, try
using Firefox or Google Chrome instead.
. You can
still work on the task before you get the account to submit your solution.
2. Although in practice, it's
good to have an input prompt to let user know what to enter, please do not use it for the lab tasks. Your programs
will be tested by online systems and not human, so the input prompt will become
part of the output and your results will be wrong.
For example, in the program
on slide 42 of lecture notes cs1010e-01.pdf, please only use scanf to read the input
without the previous printf:
printf("Input
two positive numbers : ");
scanf("%d
%d", &a, &b);
It is very important to note that your output must have exactly the same format
as given in the sample run of each task (see below). Otherwise your program
will fail the test cases.
3. You may want to refer to the
C online reference here.
The determinant is a scalar value computed from an n x n square matrix.
It is used in engineering applications where solving systems of linear
equations is involved. Here, we shall see how the determinant of a 2 x 2 matrix
can be computed using a formulaic approach.
The determinant of a 2 x 2 matrix,
, is given as
.
Given the floating point entries of a 2 x 2 matrix, in the order a, b, c
and d from standard input, print to standard output the determinant in floating
point format rounded to 3 decimal places.
For visual purpose, user's
inputs are shown in blue and outputs are shown in bold purple.
|
1 2 3 4 |
|
-2.3 4.5 -6.7 8.9 |
|
0.8147 0.1270 0.9058 0.9134 |
The ↵ denotes an invisible new line character. This means the output
is terminated by a new line character.
You should download the
skeleton file here and write your code in that file. Note
that in sit-in labs, you will also be given a skeleton file with similar header
that you need to fill in. In those sessions, if you do not write your code in
the given file, there will be a 50% mark penalty.
A good practice for coding is
not to write the whole program at once. You should write it incrementally, guaranteeing that the code after each
important stage is correct.
One way is to write the overall layout of the program before actually
implementing any main algorithm.
You
may follow the coding cycle below:
1. Writing the first
draft
As you have learnt in lecture
2, a simple program should have the following structure
|
//include
the headers, e.g. stdio.h |
An example can be seen in slide 42 of lecture notes cs1010e-01.pdf.
As mentioned above, you do not need to write all the 4 parts in main() at once. You may
start by declaring a variable for the input number (part 1) and use scanf to read it (part
2).
If you are unsure whether you have used scanf to read in the
number correctly, you may then use a debugging printf to print it out.
In fact, if you are unsure of your program correctness at any point,
you may check the involved variables by including your own printf at that point.
2. Indentation
After
writing the first draft, you can indent your program automatically in vim by switching to Command Mode and type gg=G. If some line(s) is(are) not indented correctly, it means there is(are) some
syntax error(s) in your program. Note that the other way round is untrue. If gg=G indents your
program correctly, it doesn't mean your program has no error.
3. Compilation
If all the lines are indented correctly, you can quit vim and type gcc
-Wall det2.c -o det2.exe to compile your program.
-Wall (Warnings all) option enables all the warnings
that may occur with your program.
-o det2.exe creates the executable file
named det2.exe. If -o is not used, the
default executable file created in Cygwin is a.exe.
Do you see any error or warning when compiling your program?
If there is, look at the line number to know where it is.
4. Testing
If there is no error or warning, you can run the executable file by typing
./det2.exe to see if the input number
is read correctly.
Computing the n x n cofactor matrix, cof(A), of an n x n square matrix A, is required as an intermediate step
towards calculating the determinant det(A) and matrix inverse A-1. Finding the matrix
inverse is a critical step towards solving systems of linear equations in
several engineering disciplines. More often than not, algorithms are limited by
the bottleneck introduced by matrix inversion as it is computationally
intensive.
Computing cof(A) is a two-step
process. First, the n x n minor matrix of A,
minor(A), is
computed. After which the entries of minor(A) are sign corrected by a checkerboard
scheme to give cof(A). We shall use a 5 x 5 matrix to illustrate the process.
Step 1:
Computing the minor matrix.

To find, say the entry c12 of minor(B), we cross
out all the entries in B sharing the same
row and/or same column as b12. That is to say the
following elements are deleted: {b10, b11, b12,
b13, b14, b02, b22, b32,
b42}. After which we rewrite B
omitting the deleted elements. Observe that the rewritten matrix has been
reduced to dimensions 4 x 4. Entry c12 is then the determinant of
this reduced matrix. Repeat the process for the rest of minor(B). A pictorial representation of how c12
is computed can be found below.

The minor of an n x n matrix can be expressed in terms of the
determinants of (n-1) x (n-1) matrices.
In summary, the above method is concisely described by the following
algorithm:
1.
Choose an entry bij from
the matrix.
2.
Cross out the entries that lie in
the corresponding column i and
row j.
3.
Rewrite the matrix without the marked entries.
4.
Obtain the determinant cij of
this new matrix. cij is termed the minor for entry bij.
[Source: http://en.wikipedia.org/wiki/Cofactor_(linear_algebra)]
Step 2: Sign
correcting the minor matrix.
, in the order a, b, c, d, e, f, g, h and i
from standard input, compute the 3 x 3 cofactor matrix
, and print to standard output the entries, in the order a’,
b’, c’, d’, e’, f’, g’, h’ and i', each entry on a
line of its own. Round to 3 decimal places in floating point
format.For visual purpose, user's
inputs are shown in blue and outputs are shown in bold purple.
|
1 2 3 4 5 6 7 8 9 6.000↵ -3.000↵ 6.000↵ -12.000↵ 6.000↵ -3.000↵ 6.000↵ -3.000↵ |
|
0.49 0.42 0.96 0.80 0.92 0.66 0.14 0.79 0.04 0.060↵ 0.503↵ 0.742↵ -0.115↵ -0.328↵ -0.606↵ 0.445↵ 0.115↵ |
The ↵ denotes an invisible new line character. This means the output
is terminated by a new line character.
You should download the skeleton
file here and write your code in that file. Note that
in sit-in labs, you will also be given a skeleton file with similar header that
you need to fill in. In those sessions, if you do not write your code in the
given file, there will be a 50% mark penalty.
As the algorithm statement is
lengthy, use a pen and paper to convince yourself the
steps in realizing the algorithm.
The problem uses 5 x 5
matrices. How do you generalize the problem to a 3 x 3 matrix? Use an example
matrix, write it down on paper and see how to adapt the algorithm.