Question

Write a method that checks whether all elements in a two-dimensional array are identical. This is...

Write a method that checks whether all elements in a two-dimensional array are identical. This is what I have so far.

int[][] arr1 = {{2, 2, 8}, {1, 6, 4}, {3, 9, 8}, {5, 6, 1}};
       int[][] arr2 = {{7, 7, 7}, {7, 7, 7}, {7, 7, 7}, {7, 7, 7}};
      
       if(matchingVal(array1)) {
           System.out.println("Values in arr1 are the same.");
       }
      
       if(matchingVal(array2)) {
           System.out.println("Values in arr2 are the same.");
       }

Homework Answers

Answer #1

Solution :

Explanation : Traverse all the elements of the array and check if every element is equal to the first element i.e the element array[0][0].

Following is the Java code for the same :

public class Main {
    public static boolean matchingVal(int[][] arr) {
  //Assuming all elements are equal to arr[0][0]
  int same = arr[0][0];
  //traverse the 2-d array     
  for (int i=0;i<arr.length;i++)
    for (int j=0;j<arr[0].length;j++)
    
      if(arr[i][j] != same)
        return false;

  return true;
}
    
    public static void main(String args[]) {
       int[][] arr1 = {{2, 2, 8}, {1, 6, 4}, {3, 9, 8}, {5, 6, 1}};
       int[][] arr2 = {{7, 7, 7}, {7, 7, 7}, {7, 7, 7}, {7, 7, 7}};
      
       if(matchingVal(arr1)) {
           System.out.println("Values in arr1 are the same.\n");
       }
       else System.out.println("Values in arr1 are not same.\n");
      
       if(matchingVal(arr2)) {
           System.out.println("Values in arr2 are the same.\n");
       }
       else System.out.println("Values in arr2 are not same.\n");
    }
}

Code demo :

Output :

Know the answer?
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
Similar Questions
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop...
C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop to initialize the elements (do not initialize the array in the declaration): 10  9   8 7   6   5 4 3   2
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
Write a program to determine the minimum element in an array of ten elements. The program...
Write a program to determine the minimum element in an array of ten elements. The program should have the following: 1. Class Name as ArrayProcessing. The main method should create an array of size 10 2. There should be two methods besides the main method in the class namely inputArray and MinimumElement 3. InputArray method should assign the ten elements in the array. Use scanner to input these elements. The array of 10 numbers in the method "InputArray" should be...
/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays...
/** * 1. Write the method plusTwo(). * * Your method will take 2 int arrays as parameters. * Each will have a length of 2. Your method must * create and return a new array with a length of 4, * containing all their elements. * * Here are some examples: * plusTwo({1, 2}, {3, 4}) returns {1, 2, 3, 4} * plusTwo({4, 4}, {2, 2}) returns {4, 4, 2, 2} * plusTwo({9, 2}, {3, 4}) returns {9, 2,...
Write a method that returns the sum of all the elements in a specified column in...
Write a method that returns the sum of all the elements in a specified column in a 3 x 4 matrix using the following header: public static double sumColumn(double[][] m, int columnIndex) The program should be broken down into methods, menu-driven, and check for proper input, etc. The problem I'm having is I'm trying to get my menu to execute the runProgram method. I'm not sure what should be in the parentheses to direct choice "1" to the method. I'm...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
(2nd Problem) From Chapter 15 on page 1080, do problem #5. Recursive function that computes the...
(2nd Problem) From Chapter 15 on page 1080, do problem #5. Recursive function that computes the same of values in an array. To test this function in the main, create the following array: int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; #15. Write a recursive function that finds and returns the sum of the elements of an int array. Also, write a program to test your function.
In Java is there a way I can put a value in a linkList = array[...
In Java is there a way I can put a value in a linkList = array[ ] string (I don't know how to word it here what I have so far with my code) * Example object 51 I want to equal Array [0]; // Project code below public String getCardRank() { String values[] = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; return "RRR"; } // this part is in another .java file private...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1,...
Declare an integer array of size 10 with values initialized as follows. int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Compute each item of a new array of same size derived from the above array by: adding first item (intArray[0]) of the array with 3rd, 2nd with 4th, 3rd with 5th and so on. For the last-but-one item intArray[8], add it with first item and for the last item (intArray[9]) add it with 2nd item...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...