Matrix Determinants SOlution



Given an N N square matrix Ai;j ; 1 i; j N , then the determinant can be defined by the Leibniz formula as


det(A) =

sgn( ) Ai; i :




where SN is the set of all permutations of f1; 2; ; N g, is one possible permutation in SN , and i is the ith element of the permutation . In Lab 5, the Pandita algorithm has been introduced. Given a permutation (m), the Pandita algorithm generates the next lexicographic permutation

(m+1) with (1) = f1; 2; ; N g. The function sgn( (m)) is defined as the following.

sgn( (m)) = {


if m = 1;


( 1)t sgn( (m 1));


where t is the number of swaps needed for the Pandita algorithm to generate the next permutation. In this assignment, you need to write a C program to calculate the determinant of an N N square matrix using Equations (7.1) and (7.2). Again, the size of the matrix N should be defined

as a macro.

#if !defined(N)

#define N 3


Twelve matrices with various dimensions have been provided for you to test your program. They are,, , You should open each file to find the dimension of the matrix and then compile your program with the right dimension as

  • gcc -DN=3 lab07.c

  • ./a.out <

The last line uses the unix input redirection method to read input directly from the file In this way, we do not need to retype the matrix every time we execute the program. Example program compilation and execution is shown below.

  • gcc -DN=3 lab07.c

  • ./a.out < Input matrix is

1 2 3

4 5 6

7 8 9 Det = 0


  1. Create a directory lab07 and use it as the working directory.

  1. Name your program source file as lab07.c.

  1. The first few lines of your program should be comments as the following.

/* EE231002 Lab07. Matrix Determinants ID, Name



  1. After you finish verifying your program, you can submit your source code by

$ ee231002/bin/submit lab07 lab07.c

If you see a ”submitted successfully” message, then you are done. In case you want to check which file and at what time you submitted your labs, you can type in the following command:

$ ee231002/bin/subrec lab07

It will show the last few submission records.