Java Programming. Write your own source code with comments. (Latin square) A Lat
ID: 3787132 • Letter: J
Question
Java Programming. Write your own source code with comments.
(Latin square) A Latin square is an n-by-n array filled with n different Latin letters, each occurring exactly once in each row and once in each column. Write a program that prompts the user to enter the number n and the array of characters, as shown in the sample output, and checks if the input array is a Latin square.
The characters are the first n characters starting from A .
Enter number n: 4
Enter 4 rows of letters separated by spaces:
A B C D
B A D C
C D B A
D C A B
The input array is a Latin square
Enter number n: 3
Enter 3 rows of letters separated by spaces:
A F D
Wrong input: the letters must be from A to C
Explanation / Answer
Solution for Problem(in Java) :-
Program :-
import java.util.Scanner;
class LatinLetter
{
public static void main(String args[])
{
int n,m,flag=1;
Scanner in=new Scanner(System.in);
System.out.println("Enter number n :- ");
n=in.nextInt();
char array[][]=new char[n][n];
System.out.println("Enter "+n+" rows of letters starting from A ");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
array[i][j]=in.next().charAt(0);
}
}
System.out.println("Enter input array size m :- ");
m=in.nextInt();
char inputArray[]=new char[m];
System.out.println("Enter "+m+" letters :-");
for(int i=0;i<m;i++)
{
inputArray[i]=in.next().charAt(0);
}
int letterSize='A'+m-1;
char last=(char) letterSize;
for(int i=0;i<m;i++)
{
if(inputArray[i]>='A' && inputArray[i]<=last)
{
//System.out.println(inputArray[i]);
}
else
{
flag=0;
break;
}
}
if(flag==0)
System.out.println("Wrong input: the letters must be from A to "+last);
else
System.out.println("Right input: the word is Lattin Square");
}
}
Sample Output 1;-
Enter number n :-
4
Enter 4 rows of letters starting from A
A B C D
B C D A
C D A B
D A B C
Enter input array size m :-
3
Enter 3 letters :-
A F D
Wrong input: the letters must be from A to C
Sample Output 2 :-
Enter number n :-
3
Enter 3 rows of letters starting from A
A B C
B C A
C A B
Enter input array size m :-
3
Enter 3 letters :-
A B C
Right input: the word is Lattin Square
Note :- As per your question checks if the input array is a Latin square. As per your output it printf Wrong input: the letters must be from A to n. Else the program prints Right input: the word is Lattin Square
Thank you!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.