Question

A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no dupli

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you have any doubts or you want me to modify the answer. And if you find this answer useful then don't forget to rate my answer as thumps up. Thank you! :)

public class LatinSquare {

    private static int numberOFElements;

    public LatinSquare() {

    }

    public static void main(String[] args) {
        int[][] arr2D1 = { { 10, 30, 20, 0 },
                { 0, 20, 30, 10 },
                { 30, 0, 10, 20 },
                {20, 10, 0, 30 } };
      

        printArray2D(arr2D1);
        if (isLatin(arr2D1))
            System.out.println("Yes, 2D array is a Latin Square");
        else
            System.out.println("NO, 2D array is NOT a Latin Square");
      
    }

    public static boolean isLatin(int[][] arr2d) {
        numberOFElements = arr2d[0].length;
        int[] storedValues = new int[numberOFElements];

        String val = "";

        for (int i = 0; i < arr2d[0].length; i++) {
            storedValues[i] = arr2d[0][i];
            val += " " + arr2d[0][i] + ",";
        }

        for (int i = arr2d.length - 1; i >= 0; i--) {
            boolean t = duplicate(arr2d[i]);
            if (t) {
                System.out.println("Row " + i + " has duplicates.");
                return false;
            } else {
                for (int q = 0; q < arr2d[i].length; q++) {
                    if (val.indexOf(" " + arr2d[i][q]) == -1) {
                        System.out.println("Row " + i + " does not have all row 0 elements");
                        return false;
                    }
                }
            }
        }

        int[] temp = new int[arr2d.length];

        for (int i = arr2d.length - 1; i >= 0; i--) {
            for (int j = 0; j < arr2d.length; j++) {
                temp[j] = arr2d[j][i];
            }
            boolean t = duplicate(temp);
            if (t) {
                System.out.println("Column " + i + " has duplicates.");
                return false;
            } else {
                for (int q = 0; q < temp.length; q++) {
                    if (val.indexOf(" " + temp[q]) == -1) {
                        System.out.println("Column " + i + " does not have all row 0 elements");
                        return false;
                    }
                }
            }
        }

        return true;
    }

    public static boolean duplicate(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    return true;
                }
            }
        }

        return false;

    }

    public static void printArray2D(int[][] arr2d) {
        for (int i = 0; i < arr2d.length; i++) {
            System.out.print("\n");
            for (int q = 0; q < arr2d[0].length; q++) {
                if (arr2d[i][q] < 10) {
                    System.out.print(" " + arr2d[i][q] + " ");
                } else {
                    System.out.print(arr2d[i][q] + " ");
                }
            }
        }System.out.print("\n");
    }
}



LatinSquare) - .sre/LatinSquare.java [LatinSquare] LatinSquare Project LatinSquare public class LatinSquare LatinSquare -/ide

Add a comment
Know the answer?
Add Answer to:
A two-dimensional square array of integers is a Latin square if the following conditions are true. The first row has no duplicate values. All values in the first row of the square appear in each row...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (10 pts) Declare a two-dimensional int array of size 3 x 4 (row x column) and...

    (10 pts) Declare a two-dimensional int array of size 3 x 4 (row x column) and initialize the element array[0][1] to 1 and all other elements to zeros. Use a shortest possible statement.

  • C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

    C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...

  • Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array...

    Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array is randomly initialized (using Math.random()). Write a method to find the maximum value in this two dimensional array; Write a method to find the average value in the array; Write a method to count how many elements are smaller than average; Write a method to copy contents in a two-dimensional array to a one-dimensional array. The method will return this one-dimensional array. Method is...

  • The last element in each array in a 2D array is incorrect. It’s your job to...

    The last element in each array in a 2D array is incorrect. It’s your job to fix each array so that the value 0 is changed to include the correct value. In the first array, the final value should be the length of the first array. In the second array, the final value should be the sum of the first value, and the second to last value in the array. In the third array, the final value should be the...

  • IN C#.Develop a program that prints out the location of the largest value in a two-dimensional...

    IN C#.Develop a program that prints out the location of the largest value in a two-dimensional array. The largest values may appear more than once in the array, so you must only print out the first instance. The program defines method locateLargest() that takes a two-dimensional array of integers and returns the location (row index and column index) of the first largest value as a single-dimensional array. The program main method prompts the user to enter a 3-by-4 matrix, prints...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a...

    Refer to the following array definition for questions 1 & 2 int numberArray[9)[11] 1. Write a statement that assigns 145 to the first column of the first row of this array. 2. Write a statement that assigns 18 to the last column of the last row of this array. values is a two-dimensional array of floats, with 10 rows and 20 columns. Write code that sums all of the elements in the array and stores the sum in the variable...

  • please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class...

    please answer in Java..all excercises. /** * YOUR NAME GOES HERE * 4/7/2017 */ public class Chapter9a_FillInTheCode { public static void main(String[] args) { String [][] geo = {{"MD", "NY", "NJ", "MA", "CA", "MI", "OR"}, {"Detroit", "Newark", "Boston", "Seattle"}}; // ------> exercise 9a1 // this code prints the element at row at index 1 and column at index 2 // your code goes here System.out.println("Done exercise 9a1.\n"); // ------> exercise 9a2 // this code prints all the states in the...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

  • Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D...

    Use C++ (2D Array) Write a program which: 1. Assigns data given below into the 2D array of integers which is 10x10. 2. Prints out the contents of the 2D array after assigning the data to make sure correct data was assigned. 3. Figures out and prints out the square root of the sum of ALL the elements in the 2D array. 4. Figures out and prints out the average of ALL THE ELEMENTS in the 2D array. 5. Figures...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT