Latin Square - GeeksforGeeks
A Latin Square is a n x n grid filled by n distinct numbers each appearing exactly once in each row and column. Given an input n, we have to print a n x n matrix consisting of numbers from 1 to n each appearing exactly once in each row and each column.
Read full article from Latin Square - GeeksforGeeks
A Latin Square is a n x n grid filled by n distinct numbers each appearing exactly once in each row and column. Given an input n, we have to print a n x n matrix consisting of numbers from 1 to n each appearing exactly once in each row and each column.
- In the first row, the numbers are stored from 1 to n serially.
- the second row, the numbers are shifted to the right by one column. i.e, 1 is stored at 2nd column now and so on.
- In the third row, the numbers are shifted to the right by two columns. i.e, 1 is stored at 3rd column now and so on.
- We continue same way for remaining rows.
void
printLatin(
int
n)
{
// A variable to control the rotation
// point.
int
k = n+1;
// Loop to print rows
for
(
int
i=1; i<=n; i++)
{
// This loops runs only after first
// iteration of outer loop. It prints
// numbers from n to k
int
temp = k;
while
(temp <= n)
{
printf
(
"%d "
, temp);
temp++;
}
// This loop prints numbers from 1 to k-1.
for
(
int
j=1; j<k; j++)
printf
(
"%d "
, j);
k--;
printf
(
"\n"
);
}
}