C++ Tutorial: 3 x 3 Matrix determinant

By @acetix2/24/2018programming

Hey!
In this tutorial I will show you how to make a C++ program to calculate 3 x 3 determinants.

Programs used:

Let's start!
At first we want to be able to put in our variables so that they look like this:
https://i.imgur.com/it29CCf.png

Therefor we need a 2-dimensional array of the type double.
Each dimension has a length of 3, because we need to put in 9 variables.
https://i.imgur.com/8DunSCP.png

For the input of each field, we need 2 for-loops to go through all fields.
The for-loops musst have the length of 3, because each dimension of our 2d-array has the length of 3.

Start with the first loop for the rows:
https://i.imgur.com/fTLmW2G.png
Inside there we are going to write the second loop for the columns.
https://i.imgur.com/jB65AOA.png
Now we can put in our variables with " cin " for each row and column.
https://i.imgur.com/TA1YY7o.png

It is usefull to have a debug message to see if we have done everything right.
We do this almost the same way as with the input.
Instead of " cin ", we use " cout " as output.
https://i.imgur.com/zFSACMU.png
It should look like this in the console:
https://i.imgur.com/fJK1UxM.png

Let's go over to the calculation.
This is now a bit more complicated.
We want to calculate this:
https://i.imgur.com/1NNMgty.png
(ARRAYS START FROM 0).
The first operation is to multiply parameter " a " with parameter " e ".
" a " has the row position 0 and column position 0
" e " has the row position 1 and column position 1.
Written in code it would be:
https://i.imgur.com/A5CIQqQ.png
And this goes on and on... until there are no parameters left.
https://i.imgur.com/LhYhhyb.png
This looks a bit weird, but if you compare it to the image where the calculation is described, you can see that it is completly right.
We can now assign a variable to this calculation and print it in the console.

It should then look like this in the console:
https://i.imgur.com/LgjRB26.png
In this case -43 is our determinant.

That's it! Hope you like it :)

2

comments