Design a Latin square in C-Programming:
So first of all we have to know what is a Latin square? By definition a Latin square is an arrangement of letters or symbols that occur n times, in a 2-dimensional array of n2 compartments so that no letters or numbers appears twice in the same row or column. For more information about Latin square visit here. An example of 3x3 Latin square is given below.
Important Tools:
1. for-loop.2. Array.
Related Link:
1. How to print a matrix.2. Sum of two matrix.
Code:
#include<stdio.h>
int main()
{
int n,a[10][10],i,j;
printf("Enter the value of n:");
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=(i+j)%n+1;
printf("The Latin for %d will be\n",n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
}
Output:
The output is given below:Explanation:
1. We use header package <stdio.h>.2. Therefore we introduced 4 int variable n, a[10][10], i, j. Where a[10][10] is array type variable.
3. First of all we used n for store the order of Latin square.
4. Therefore we used for-loop to evaluate all the value for each compartment of that Latin square.
5. Meaning of
for(i=0;i<n;i++)If we consider for the value n=3 then we have the value in a[0][0] is {(0+0)%3+1}=(0+1)=1. Again for a[0][1] we have the value {(0+1)%3+1}=(1+1)=2, for a[0][2] we have {(0+2)%3+1}=(2+1)=3. and so on it will give the value of a[1][0], a[1][1], a[1][2], a[2][0], a[2][1] and a[2][2] are 2,3,1,3,1 and 2 respectively.
for(j=0;j<n;j++)
a[i][j]=(i+j)%n+1;
6. Then we again use for-loop to print the value of the Latin square. It works similar as we already mention on this Explanation no 6.
7. You can see that we use "%3d" to print the numbers of the Latin square. The reason for using "%3d" is, it will print the numbers giving two space between every to consecutive number.
You can also see:
1. Find the largest number in C-Programming.2. Find the terms of Fibonacci sequence in C-Programming (Using For-loop)
3. Sum of n real numbers using array
This comment has been removed by a blog administrator.
ReplyDelete