Replace the Matrix Elements by Its Square in Java?


Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.

As per the problem statement the task is replace the matrix elements by its square.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

To show you some instances

Instance-1

Suppose the original matrix is {{8,3,2}, {12,7,9}, {6,4,10}};

After replacing the matrix element by its square, the result matrix will be

64 9 4
144 49 81
36 16 100

Instance-2

Suppose the original matrix is {{4,3,7}, {2,3,9}, {3,4,9}};

After replacing the matrix element by its square, the result matrix will be

16 9 49
4 9 81
9 16 81

Instance-3

Suppose the original matrix is {{1,2,3}, {2,1,3}, {3,2,1}};

After replacing the matrix element by its square, the result matrix will be

1 4 9
4 1 9
9 4 1

Algorithm

Step 1 − Initialise and declare the matrix.

Step 2 − Create another matrix to store the square value.

Step 3 − Multiply the matrix element by itself or use inbuilt pow() function to get the square.

Step 4 − Print the result.

Syntax

To get the power of any number raised to the power of another number in Java we have inbuilt java.lang.Math.pow() method.

Following is the syntax to get power of 2 by using the method-

double power = math.pow (inputValue,2)

Multiple Approaches

We have provided the solution in different approaches.

  • By Using Static Initialization of Matrix with pow() Function.

  • By Using User Defined Method without Inbuilt Function.

Let’s see the program along with its output one by one.

Approach-1: By Using Static Initialization of Matrix with pow() Function

In this approach, matrix elements will be initialized in the program. Then as per the algorithm replace the matrix elements by its square. Here we will make use of inbuilt Pow() function to get the square of an element.

Example

public class Main {

   //main method
   public static void main(String args[]){

      //Initialising the matrix
      int a[][]={{8,3,2},{12,7,9},{6,4,10}};

      //creating another matrix to store the square value
      int c[][]=new int[3][3];

      //Multiplying the matrix element by itself to get the square
      for(int i=0;i<3;i++){
         for(int j=0;j<3;j++){
         int element = a[i][j];
            c[i][j]+= Math.pow(element,2);

            //printing the result
            System.out.print(c[i][j]+" ");

         }//end of j loop

         //new line
         System.out.println();

      }//end of i loop
   }
}

Output

64 9 4 
144 49 81 
36 16 100

Approach-2: By Using User Defined Method without Inbuilt Function

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm replace the matrix elements by its square. Here, we will multiply the element with itself to get the square of an element.

Example

public class Main {
   //main method
   public static void main(String args[]){
     
     //Initialising the matrix
      int a[][]={{4,3,7},{2,3,9},{3,4,9}};
     
     //calling user defined method
      square(a);
   }

   //user defined method
   static void square(int a[][]) {

      //creating another matrix to store the square value
      int c[][]=new int[3][3];

      //Multiplying the matrix element by itself to get the square
      for(int i=0;i<3;i++){
         for(int j=0;j<3;j++){
            c[i][j]+=a[i][j] * a[i][j];

            //printing the result
            System.out.print(c[i][j]+" ");
         }//end of j loop
        
         //new line
         System.out.println();
      }//end of i loop
   }
}

Output

16 9 49 
4 9 81 
9 16 81

In this article, we explored different approaches to replacing matrix elements by its square by using Java programming language.

Updated on: 09-Mar-2023

514 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements