C++ Tutorial: 3 x 3 Matrix determinant

in #programming6 years ago (edited)

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:

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.

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:

Inside there we are going to write the second loop for the columns.

Now we can put in our variables with " cin " for each row and column.

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.

It should look like this in the console:

Let's go over to the calculation.
This is now a bit more complicated.
We want to calculate this:

(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:

And this goes on and on... until there are no parameters left.

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:

In this case -43 is our determinant.

That's it! Hope you like it :)

Coin Marketplace

STEEM 0.18
TRX 0.14
JST 0.030
BTC 58635.35
ETH 3152.96
USDT 1.00
SBD 2.44