Question

In: Computer Science

Write a Java program to 1. read in the size of a square boolean matrix A...

Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.

Solutions

Expert Solution

Answer:-

Hello, I have written the required program in JAVA. Please find the code below.

JAVA CODE:-

import java.util.Scanner;

class Solution
{
   public static void main(String [] args)
   {
      
       Scanner in = new Scanner(System.in);
       /* Taking the size of square matrix in input from user*/
       System.out.println("Enter the size of Square Matrix");
       int size=in.nextInt();
       int [][] A =new int[size][size];
       int [][] temp =new int[size][size];
       int [][] ans= new int[size][size];
       /*Taking the elements of square matrix in input from user*/
System.out.println("Enter the elements of Matrix");
       for(int i=0;i<size;i++)
       {
           for(int j=0;j<size;j++)
           {
               System.out.print("Enter A["+i+"]["+j+"]");
               A[i][j]=in.nextInt();
               /* Checking the validity of given input*/
               if(A[i][j]!=0&&A[i][j]!=1)
               {
                   System.out.println("Invalid element, only 1 and 0 are allowed. Please re-enter it");
                   j--;
               }
           }
       }
       for(int i=0;i<size;i++)
       {
           for(int j=0;j<size;j++)
           {
               temp[i][j]=A[i][j];
           }
       }
       /* Taking the value of n as input from user*/
System.out.println("Enter the value of n");
   int n = in.nextInt();

/* Logic for calculating A^n*/
for(int count =1;count<n;count++)
{
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
ans[i][j]=0;
for(int k=0;k<size;k++)
{
ans[i][j]+=temp[i][k]*A[k][j];
}
  
}
}

for(int i=0;i<size;i++)
       {
           for(int j=0;j<size;j++)
           {
               temp[i][j]=ans[i][j];
           }
       }

}
/* Printing the required matrix A^n*/
System.out.println("The required Matrix A^"+n+" is as below");
for(int i=0;i<size;i++)
       {
           for(int j=0;j<size;j++)
           {
               System.out.print(ans[i][j]+" ");
           }
           System.out.print("\n");
       }

   }
}

Screenshots of the code:-

Please to the screenshots of the code for properly understanding the indentation of the code.

Screenshot 1:-

Screenshot 2:-

Screenshot 3:-

OUTPUT:-

In this java program, we have also handled the invalid input. If the user enters an invalid element (other than 0 or 1) for the matrix, it will be handled as shown in the below output.

I hope it would help.

Thanks!


Related Solutions

Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
A JAVA program that will read a boolean matrix corresponding to a relation R and output...
A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive. Input to the program will be the size n of an n x n boolean matrix followed by the matrix elements. Document your program nicely. NOTE: The program must output a reason in the case that an input relation fails to have a certain property.
A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive.
A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive. Input to the program will be the size n of an n x n boolean matrix followed by the matrix elements. Document your program nicely.NOTE: The program must output a reason in the case that an input relation fails to have a certain property.
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Write a Java program that defines two boolean variables a and b and shows that regardless...
Write a Java program that defines two boolean variables a and b and shows that regardless of the values of a and b, the expressions (a && (a || b)) and a are always the same.
Write a C# console program that fills the right to left diagonal of a square matrix...
Write a C# console program that fills the right to left diagonal of a square matrix with zeros, the lower-right triangle with -1s, and the upper left triangle with +1s. Let the user enter the size of the matrix up to size 21. Use a constant and error check. Output the formatted square matrix with appropriate values. Refer to the sample output below. Sample Run: *** Start of Matrix *** Enter the size of square (<= 21): 5 1 1...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
JAVA Write a program that prompts the user to enter a matrix number of rows and...
JAVA Write a program that prompts the user to enter a matrix number of rows and number of columns. In main method create 2D matrix based on the number of rows and columns input by the user; randomly fills the matrix with 0s and 1s and prints it. Create method sumColumns that takes only the matrix you created in main method and find the sum of each column with even index and prints it. Do not use global variables. Here...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Write a java program that read a line of input as a sentence and display: ...
Write a java program that read a line of input as a sentence and display:  Only the uppercase letters in the sentence.  The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT