The next program computes the determinant of an n x n matrix using Laplace expansion on the first row. It uses recursive calls to compute minors. The code itself is probably really bad, but that's where you guys step in and suggest improvements.
This one does not have a GUI, you have to manually insert the matrix as an multidimensional array (int[][]). If you have any suggestions on how the GUI should look like, let me know.
Here's the code:
I only ask because your generateMinor and matrixTostring methods won't work with a nonsquare matrix.
And just a personal preference of mine, but there is no need to assign the length of the array to a variable
is better than
because it's obvious what the delimiter of your for loop is. You don't have to find out where size is set and it also avoids the possibility of it being accidentally set to the wrong value.
Joanne
the determinant of a matrix is only defined when the matrix is square, so the number of rows must match the number of columns.
I use instead of because the length of a matrix is calculated only once whereas in your case, the matrix length will be calculated in every iteration.
Thank you for you comment. Do you have any idea on how to set the GUI?
Ted Scofield wrote:
...
the length of a matrix is calculated only once whereas in your case, the matrix length will be calculated in every iteration.
...
I'm not so sure that applies here, it is my understanding that length is not a function but a property of the arrray, a field in which a value is stored. and therefore it is not recalculated with each iteration of the loop.
something like string.length() or ArrayList.size() perhaps is a different story cause length() and size() are functions.
instead of looping explixitly on the array indexes, you can also specify an array loop as follows for (int item : numbers), where numbers is an array of type int[]. Not sure if this method will work here though.
here is a reference for this other type of for loop.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/for.html
When the array is presented to your constructor, iterate through it and check that the length of each element is the same. If you need a square array, that would require an additional check for the first element, then all the elements must have the same length. Beware in case you ever encounter a 0length array!
Arrays, Strings (which actually hide an array in their depths) and Lists all have a field (directly or indirectly) which measures how many elements they contain. In the case of arrays and Strings, it never changes, but in Lists it does change. The String#length() and List#size() methods simply return that field, without having to do any more calculations. The List size field is recalculated whenever elements are added or removed.
Campbell Ritchie wrote:If you are using matrices, it is worthwhile checking that the array is "rectangular".
When the array is presented to your constructor, iterate through it and check that the length of each element is the same. If you need a square array, that would require an additional check for the first element, then all the elements must have the same length. Beware in case you ever encounter a 0length array!
Arrays, Strings (which actually hide an array in their depths) and Lists all have a field (directly or indirectly) which measures how many elements they contain. In the case of arrays and Strings, it never changes, but in Lists it does change. The String#length() and List#size() methods simply return that field, without having to do any more calculations. The List size field is recalculated whenever elements are added or removed.
Campbell, I'm learning here, so I don't want to come across as argumentative, but you have commented "If you are using matrices, it is worthwhile checking that the array is "rectangular" First of all I am not sure what you mean by "make sure each element has the same length" Since matrix is defined as int[][], wouldn't that automatically guarantee that the array is rectangular, ie all rows have the same length and all cols have the same length? Am i missing something?
Also, in your list paragraph, when you speak of Lists, does that apply equally to the ArrayList object, or are you referring to some other kind of construct.
regards
Fred.
Fred Hamilton wrote:Since matrix is defined as int[][], wouldn't that automatically guarantee that the array is rectangular, ie all rows have the same length and all cols have the same length? Am i missing something?
There is no such thing as a multidimensional array in Java, just an Array of Arrays. That being said with a definition of int[][] you could assign an int array of size 10 to element 0 and an int array of size 200 to element 1 and so on. As long as each array is an array of ints then it can be assigned to an element in an array of int arrays. Basically you are creating an standard array which holds integer arrays, each integer array does not need to be the same size.
SCJA
~Currently preparing for SCJP6
Brian Legg wrote:
Fred Hamilton wrote:Since matrix is defined as int[][], wouldn't that automatically guarantee that the array is rectangular, ie all rows have the same length and all cols have the same length? Am i missing something?
There is no such thing as a multidimensional array in Java, just an Array of Arrays. That being said with a definition of int[][] you could assign an int array of size 10 to element 0 and an int array of size 200 to element 1 and so on. As long as each array is an array of ints then it can be assigned to an element in an array of int arrays. Basically you are creating an standard array which holds integer arrays, each integer array does not need to be the same size.
wow, ok learned something there, I can see I have some research to do. I assume when you use the word element then, you are referring to a row or a column in the case if int[][]. That I did not pick up on at first, I assumed incorrectly that an element would be that which had both a row and a column coordinate.
SCJA
~Currently preparing for SCJP6
. . . and about Lists, have a look in the Java™ Tutorials, particularly the bits about "List interface" and "List implementations".
These are the worst of times and these are the best of times. And this is the best tiny ad:
Rocket Oven Kickstarter  from the trailboss
https://coderanch.com/t/695773/RocketOvenKickstartertrailboss
