+ 1

Iteration question - C++

Here I posted a code where I'm iterating a matrix M (nxn) so it's reduced to a matrix A (3x3). I want to use each of the various matrices A generated during the reducing phase, and I'm wondering how to do this. I essentially am trying to apply Laplace's theorem, so I need to iterate multiple times. Starting from a matrix nxn we get a matrix (n-1)x(n-1), then we should use this to calculate the determinant of the matrix nxn. Sadly if n-1 > 3, we require again to reduce it to a matrix (n-2)x(n-2) until it effectively becomes a 3x3 matrix, and I can calculate the determinant of each matrix generated to finally get det(M), of the original matrix. If you're confused check Wikipedia, Laplace's theorem page: it might help. https://code.sololearn.com/cfcLnsbJAkxi

11th Aug 2022, 7:28 AM
Bonsai
Bonsai - avatar
2 Antworten
+ 1
Since the Laplace method of calculating the determinant is recursive, would you consider doing the calculation in a recursive function, instead of main()? That way new matrices declared during the various calls of the function will have a life of their own and could be "used" (as you put it), without any effort on your part. And since you're already using C++, would you consider using vector<vector<int>>M instead of int M[n][n]? (btw, since the matrix needs to be square, you should consider using only n instead of m,n for its dimensions). I wrote an example for you. Bc I didn't want to input the matrix manually, I generate it randomly - if you don't like that, change the FillMatrix() function to read instead. I also hardcoded its dimension, n, bc I actually don't like to enter ANY input on Sololearn - change that too if you want. Here it is: https://code.sololearn.com/cN6Uqiuq72VG
12th Aug 2022, 6:58 PM
lion
lion - avatar
+ 1
Thank you, lion.
15th Aug 2022, 8:30 PM
Bonsai
Bonsai - avatar