This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Learning Project 2

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, me again.

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:

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will your matrix always be n x n (i.e. will it always be a square matrix) ?
I only ask because your generateMinor and matrixTostring methods won't work with a non-square 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.
 
Ted Scofield
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi 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?

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 78431
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 0-length 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.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 0-length 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.

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 multi-dimensional 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.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 multi-dimensional 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 co-ordinate.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I try not to think of it in terms of rows and columns. When I say "element" what I am refering to depends on the context. An element of an int[][] would be an int array (aka int[]). An element of that int[] would be an int. An element was just my way of refering to the smallest container or bucket for that particular array. If I am refering to an int[][] it has no int elements, just arrays of ints. This is the distinction when it comes to other popular languages. Long live OOP ;)
 
Campbell Ritchie
Marshal
Posts: 78431
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian's answers are always helpful

. . . and about Lists, have a look in the Java™ Tutorials, particularly the bits about "List interface" and "List implementations".
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks man, I'll add lists to my list. Right now I have about 3 years worth of stuff to research, at least.
 
Campbell Ritchie
Marshal
Posts: 78431
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome I expect you to have read all that lot by next week
 
Hold that thought. Tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic