Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Latin square problem help

Greenhorn
Posts: 27
I need to get number n from the user(n must be greater than 2, and odd) and arrange into n-by-n array so no column or row contains the same two numbers, which is a latin square. Ok ive got the poplating of the matrix problem taken care of, but the latin square part is giving me a little trouble.

This is an example of a latin square:
1 2 3
2 3 1
3 1 2

So anyway ive got to create a latinSqaure method to make sure no row/column contains the same number. I dont really know how I can do this though, heh I know im gonna need a few loops to check, but just kind of lost...any help pointing me in the right direction or maybe a small example of how this can be done would be greatly appreciated. Thanks in advance

[ March 30, 2006: Message edited by: Steve Shipman ]
[ March 30, 2006: Message edited by: Steve Shipman ]

Sheriff
Posts: 14691
16
Look at the order of those numbers :
1 -> 2 -> 3
2 -> 3 -> 1
3 -> 1 -> 2

Do you see anything in common ?

Steve Shipman
Greenhorn
Posts: 27
heh I dont think im seeing anything....they are all in a different order

Ranch Hand
Posts: 196
Okay to reiterate Satou kurinosuke

Look at the order of those numbers :

Do you see anything in common ?

maybe the repositioning has made it easier to see.

And another with more numbers:

[ March 30, 2006: Message edited by: Martin Simons ]

Steve Shipman
Greenhorn
Posts: 27
like I think I know what to do, I just dont know how to express it into the code...

1 2 3
2 3 1
3 1 2

if thats the latin square, I take the 1st number in the 1st col(1), and I check it with the other 2 elements in the first col, then check it with the other 2 elements in the first row. Move onto the 2nd number in the 1st col(2), check it with the last number of the first col, and the other two numbers in the 2nd col. Then take the last number in the first col(3) and check it with the other two numbers in the third row. I think works, im not sure. Heh I know I need to check, check, check, and then check some more. I know I need to use a loop, but I dont know where to go from there

Steve Shipman
Greenhorn
Posts: 27
heh somthing to do with the numbers lining up, but I am so not seeing what you guys want me to see

Steve Shipman
Greenhorn
Posts: 27
WAIT!
1 -> 2 -> 3
2 -> 3 -> 1
3 -> 1 -> 2

the 2 and 3 line up, and the 1 and 2 line up....only 1 number is different in each of the columns
[ March 30, 2006: Message edited by: Steve Shipman ]

Martin Simons
Ranch Hand
Posts: 196
In otherwords, the numbers are all in the same order,
they are just shifted one position to the left.

so, write your array out once, then take the first character
and put it at the end of the array shifting all the other
characters to the left and print again. repeat until done.

Steve Shipman
Greenhorn
Posts: 27

In otherwords, the numbers are all in the same order,
they are just shifted one position to the left.

so, write your array out once, then take the first character
and put it at the end of the array shifting all the other
characters to the left and print again. repeat until done.

I dont need to create the latin square though, the user is going to input a number for n(which is greater than 2 and odd) then it will create a
n-by-n array. So I cant shift any of the numbers, I just then need to check and see if that array that was created is a latin square
[ March 30, 2006: Message edited by: Steve Shipman ]

Martin Simons
Ranch Hand
Posts: 196
Is your assignment to create a latin square proofer, or a program
that creates a latin square from a number that the user gives you.

If it is the second, then simply create the matrix as described
above, and there will be no reason to proof it.

Christophe Verré
Sheriff
Posts: 14691
16

, I just then need to check and see if that array that was created is a latin square

Don't need to check anything You didn't carefully see the order of the numbers. Don't get confused about "latin square". It's a scary word, but the thing behind it is very simple.

Look again :

1 -> 2 -> 3 -> 4

2 -> 3 -> 4 -> 1

3 -> 4 -> 1 -> 2

4 -> 1 -> 2 -> 3

If the user input "4", can you create each of the above line in a loop ?

Steve Shipman
Greenhorn
Posts: 27
Ok looks like you guys are right about not checking anything, the teacher helped to start the class out on it by having a populate method, and then checkLatin method. But the first part of the directions say "Using only the integers 1 through n (Where n > 2 and odd.), arrange n sets of these numbers into n-by-n array so no column or row contains the same two numbers. The result is known as a Latin square." so it is to just create latin squares, no need to check if they are. Ok well I know I need to shift the number to to the left to form the next line and with a loop it would look somthing like

I think that is where the code goes to move each number to the left, but not sure how to do this in code
[ March 30, 2006: Message edited by: Steve Shipman ]

Christophe Verré
Sheriff
Posts: 14691
16
If you need to create a method to check if the matrix is a latin square, forget about what I've said

Steve Shipman
Greenhorn
Posts: 27
"Using only the integers 1 through n (Where n > 2 and odd.), arrange n sets of these numbers into n-by-n array so no column or row contains the same two numbers. The result is known as a Latin square."

thats the directions on the project, doesnt say anything about checking to see, just sets to create an n-by-n array which is a latin square, where n is a number the user inputs. The having a method to check and see if its a latin square is just somthing the teacher wrote on the board, but the directions say nothing about that....so im thinking this way is how its supposed to be done

Martin Simons
Ranch Hand
Posts: 196

try this routine out manually (on paper or visualizing) yourself
to truely understand what is happening.

Steve Shipman
Greenhorn
Posts: 27
In order to use the array[i][j] I need to define my array of course...but what I had was causing problems

I dont know what to put in the [][] brackets when defining array....I cant use i and j bc they are in the loop, but if I put other variables in there such as x, y.....what do I define x and y as
[ March 30, 2006: Message edited by: Steve Shipman ]

Martin Simons
Ranch Hand
Posts: 196
for the initial definition of your array it should of course
use the number typed by your user

Don't forget that this will initialize every "cell" with the value 0.

And this is of course done before starting your looping.
[ March 30, 2006: Message edited by: Martin Simons ]

Steve Shipman
Greenhorn
Posts: 27
bah, lol thats what I was trying to do all along but I kept getting an error, just tried it now and it worked though

Steve Shipman
Greenhorn
Posts: 27
thanks guys, there is a second part but its 5am so im headed to bed, but thanks for all of your help!!!

[ March 30, 2006: Message edited by: Steve Shipman ]
[ March 30, 2006: Message edited by: Steve Shipman ]

Martin Simons
Ranch Hand
Posts: 196
Good luck to you on the rest. It's only about noon where I am.

Steve Shipman
Greenhorn
Posts: 27
Ok, up and ready for round 2...

"allLatinSquare method will contain all of the code to create and display all of the possible Latin Squares given L(which the user enters)"

he only wants us to go up to 3 though. So the method needs to work when the user enters 1, 2, or 3. There are 2 latin squares for 2, but there are 12 latin sqaures for the number 3 though...so im thinking my method will need to be void, since I need to return more than one array and just print them instead of returning.

I do see a pattern here though....these are the 1st 4 of the 12 matrix's for 3:

1-2-3 // 1-2-3 // 1-3-2 // 1-3-2
2-3-1 // 3-1-2 // 2-1-3 // 3-2-1
3-1-2 // 2-3-1 // 3-2-1 // 2-1-3

In the first 2 matrix's two of the rows are the same " 1 2 3" and "3 1 2", and in the second 2, two of the matrix's are the same "2 1 3" and "1 3 2". This stays true to the other 8 as well, every 2 sets has 2 of the same rows. Ok but my question is, how do I get my method to generate more than 1 array for array[L][L](I need 12) where each one is different. Im thinking the pattern I saw will come in handy for generating all 12.
[ March 30, 2006: Message edited by: Steve Shipman ]

Steve Shipman
Greenhorn
Posts: 27
Im thinking in the method I can have somthing like

That would prob help, but still unsure of how to generate all 12 squares

Ranch Hand
Posts: 50
this may not be a complete soluttion but i guess it should work when you implement it for matrix

int a[]=new int[n];

for(int i=0;i<n;i++)
a[i]=i=1;

for(int i=0;i<n;i++)
{

for(int j=0;j<n;j++)
{
int k=i+j;
if(k==n)
{
int t=0;
while(j<n)
{
System.out.print(a[t]);
t++;
j++;
}
}
if(i==n) break;

System.out.println(a[k]);
}
System.out.println();
}

This should work..... the number to be accepted canbe done by passing an argument while running this program. That number should be converted to an integer using parseint method of Integer.

sai donthneni
Ranch Hand
Posts: 50
sorry, typo..

int a[i]=i+1;
and not a[i]=i=1;

Martin Simons
Ranch Hand
Posts: 196
Sorry Steve,

I would help, but am unable to today, or over the weekend.

 That new kid is a freak. Show him this tiny ad: The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS