Question

In: Computer Science

In java run through eclipse /**    *    * Given a square array, determines if...

In java run through eclipse

/**
   *
   * Given a square array, determines if it is diagonal
   * That is, returns true if all values in the array are 0
   * except the main diagonal. The main diagonal is entries of the form
   * data[i][j] where i == j. Elements on the main
   * diagonal can be 0 or any other number.
   *
   * Examples:
   * {{1,0,0},
   * {0,2,0}
   * {0,0,3}} yields true
   *
   * {{1,0,9},
   * {0,2,0},
   * {0,0,3}} yields false because 0,2 is nonzero
   *
   * {{0,0,0},
   * {0,0,0},
   * {0,0,3}} yields true because there can be 0
   * entries on the diagonal if desired
   * @param data input array
   * @return true if it is diagonal, false otherwise
   */
   public static boolean isDiagonal(int[][] data) {
      
       return false;
   }

Solutions

Expert Solution

public class IsDiagonal {


    /**
     * Given a square array, determines if it is diagonal
     * That is, returns true if all values in the array are 0
     * except the main diagonal. The main diagonal is entries of the form
     * data[i][j] where i == j. Elements on the main
     * diagonal can be 0 or any other number.
     * <p>
     * Examples:
     * {{1,0,0},
     * {0,2,0}
     * {0,0,3}} yields true
     * <p>
     * {{1,0,9},
     * {0,2,0},
     * {0,0,3}} yields false because 0,2 is nonzero
     * <p>
     * {{0,0,0},
     * {0,0,0},
     * {0,0,3}} yields true because there can be 0
     * entries on the diagonal if desired
     *
     * @param data input array
     * @return true if it is diagonal, false otherwise
     */
    public static boolean isDiagonal(int[][] data) {
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (i != j && data[i][j] != 0) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isDiagonal(new int[][]{{1, 0, 0}, {0, 2, 0}, {0, 0, 3}}));
        System.out.println(isDiagonal(new int[][]{{1, 0, 9}, {0, 2, 0}, {0, 0, 3}}));
        System.out.println(isDiagonal(new int[][]{{0, 0, 0}, {0, 0, 0}, {0, 0, 3}}));
    }
}


Related Solutions

Please use Java eclipse Find pair in an array with given sum Given an array of...
Please use Java eclipse Find pair in an array with given sum Given an array of integers A and an integer S, determines whether there exist two elements in the array whose sum is exactly equal to S or not. Display 1 a pair is found in an array with matching sum S else 0. Input     6     5     1 -2 3 8 7     Where, First line represents integer S. Second line represents the size of an array. Third line represents...
Please use Java Eclipse and show code/output Please create a program that determines when a good...
Please use Java Eclipse and show code/output Please create a program that determines when a good day to go to the beach is. Please use the users input and its returning output. If the weather is 70 degree or greater, the program should say yes it is a good day to go If the weather is less than 70 degrees to say no the weather is not a good day to go
given an array, write code to scan the array for a particular purpose . use java
given an array, write code to scan the array for a particular purpose . use java
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with...
IN java Create a New Java Project called LastNameDuplicate. //Given an array of N elements with each element between 1 and N, write a program to determine whether there are any duplicates. //You must prompt the user for the array elements. //Display the contents of the array, along with the values that are duplicated and how many times they appeared in the array. //NOTE: N should be at least 15. Input Validation: Verify that each element entered has a value...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove,...
THIS IS FOR JAVA Given an oversize array of size words and a word to remove, write a method that returns the array with each occurrence of the given word removed. Shift the remaining words in the nonempty part of the array to the left so that each occurrence of the given word is overwritten. (Leave the words in the empty part of the array unchanged.) Hint: To understand the test cases, note that the size (but not the capacity)...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
In JAVA Directions: You must trace through the code and determine the status of the array....
In JAVA Directions: You must trace through the code and determine the status of the array. The code can be found on the second page of this packet. Assume Array A = Index 0 1 2 3 4 5 6 7 8 9 10 11 12 Value 33 12 39 6 -2 30 15 11 55 100 40 39 1 How many elements does A have? ______________ What is the start index? _______________ (assume this value is stored in a...
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a...
In Java Describe an algorithm that given a matrix described below determines whether matrix contains a value k. The input matrix A[1...n, 1...n] has all rows and columns arranged in an non-descending order A[i, j] < A[i, j+1], A[j, i] < A[j + 1, i] for all 1 < i < n and 1 < j < n
Write a program in Java with a Scanner. Given an array and a number k where...
Write a program in Java with a Scanner. Given an array and a number k where k is smaller than the size of the array, write a program to find the k'th smallest element in the given array. It is given that all array elements are distinct. Example: Input: arr[] = {7,10,4,3,20,15} k = 3 Output: 7
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT