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 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.
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 “*”.
Write a JAVA program read a number within the range of 1 through 1000 to display...
Write a JAVA program read a number within the range of 1 through 1000 to display the Roman numeral version of that number. Include screenshot of the output
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 to create an array of a specific size (which is an input...
Write a Java program to create an array of a specific size (which is an input from the user) and fill it with random numbers between 1 and 100. Then sort the array and count how many of these numbers are originally at sorted position. Display that original array, the sorted array, and the count (number of elements originally at sorted position).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT