# How to form a determinant of any size

As you know, matrices are a good way to solve certain problems such as simultaneous equations. You may have seen numerous equations with determinants in them but how do you make a determinant of any size?
There are patterns. First of all, let's look at a determinant:
 a11 a12 a21 a22

So for each element, the first number is the row and the second is the column.
Write down the number 1 to the amount of columns there are (in this case, 2) then all the permutations for these numbers. Later on I will show you a very simple computer program to generate these.
For this size of determinant, the permutations are:
1,2
2,1

Write out each element with an increasing row and column, multiplied together:
a11× a22
then underneath, swap the sign and change the row number aRow, column with the next permutation (in this case 2,1):
a11× a22
- a21× a12
All very easy so far so the determinant is a11× a22 - a21× a12

## Extending to 3×3

 a11 a12 a13 a21 a22 a23 a31 a32 a33

Write down the number 1 to the amount of columns there are (in this case, 3) then all the permutations for these numbers:
1,2,3
2,1,3
3,1,2
1,3,2
2,3,1
3,2,1
Note that there is one pair of numbers that swapped over on each line.
The number of permutations is the factorial of the amount of rows you have. This means larger determinants are best calculated with a computer program or a graphical calculator.

Write out each element with an increasing row and column, multiplied together:
a11× a22× a33
then underneath, swap the sign and change the number aRow, column with the next permutation (notice there is a swap in sign only when there is one pair of row numbers that have swapped). Leave the column number unchanged:

 a11× a22× a33 - a21× a12× a33 a31× a12× a23 - a11× a32× a23 a21× a32× a13 - a31× a22× a13

So the determinant is a11× a22× a33 - a21× a12× a33 + a31× a12× a23 - a11× a32× a23 + a21× a32× a13 - a31× a22× a13

## Source code

Here is a simple program for generating all the permutations, written in Javascript. If you want to adapt it for C++, you need to put the HeapPerm after the functions.

<script type="text/javascript">
var RowList=[1,2,3];
HeapPerm(3); // this starts the program.
/*
If you change the number, make sure you change RowList with all the elements too for example, if you enter HeapPerm(5) then you need to set RowList to RowList=[1,2,3,4,5];
I don't recommend a number higher than 8 because it can take quite a while for the browser to generate all the elements
*/

function Swap(row1,row2)
{
var spare;
spare=RowList[row1];
RowList[row1]=RowList[row2];
RowList[row2]=spare;
}

function HeapPerm(n)
{
if(n<2)
{
document.write(RowList,"<br>");
return;
}

for(var i=0;i<n;i++)
{
HeapPerm(n-1);
if(n&1) Swap(0,n-1);
else Swap(i,n-1);
}
}
</script>
Note the var in for(var i=0;i<n;i++) - this is required because it makes the variable i a local one in Javascript. If you left var out then it becomes global to further calls to HeapPerm() and the program will not work properly.

This program generates:
1,2,3
2,1,3
3,1,2
1,3,2
2,3,1
3,2,1