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

Solutions

Expert Solution

Multiplication.java


package multiplication;

import java.util.Scanner;


public class Multiplication {

  
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int size_A;//size of the array
System.out.println("Enter the size of matrix");
size_A=in.nextInt();

int[][] A=new int[size_A][size_A];
for(int i=0;i<size_A;i++)
{
A[i]=new int[size_A];
}
System.out.println("Enter Matrix");
for(int i=0;i<size_A;i++)
for(int j=0;j<size_A;j++)
{
A[i][j]=in.nextInt();
}
int n;
System.out.println("Enter the N");
n=in.nextInt();
int[][] result=new int[size_A][size_A];
  
for(int i=0;i<size_A;i++)
for(int j=0;j<size_A;j++)
{
result[i][j]=A[i][j];
}
  
int[][] temp_result=new int[size_A][size_A];
for(int i=0;i<size_A;i++)
for(int j=0;j<size_A;j++)
{
temp_result[i][j]=0;
}

for(int count=0;count<n-1;count++)
{
for(int i=0;i<size_A;i++)
for(int j=0;j<size_A;j++)
for(int k=0;k<size_A;k++)
temp_result[i][j]+=A[i][k]*result[k][j];

for(int i=0;i<size_A;i++)
for(int j=0;j<size_A;j++)
{
result[i][j]=temp_result[i][j];
}
for(int i=0;i<size_A;i++)
for(int j=0;j<size_A;j++)
{
temp_result[i][j]=0;
}

}
System.out.println("A^n");
for(int i=0;i<size_A;i++)
{
for(int j=0;j<size_A;j++)
{
System.out.print(result[i][j]+"\t");
}
System.out.println();
}
}
  
}

CODE SCREENSHOTS:


OUTPUT:

Enter the size of matrix
4
Enter Matrix
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Enter the N
6
A^n
32   0   0   32  
0   32   32   0  
0   32   32   0  
32   0   0   32


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 Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
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.
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 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...
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).
write a c++ program to read two matrices with any size. Your program should have at...
write a c++ program to read two matrices with any size. Your program should have at least the following functions Main() Read a Matrix Add two matrices Subtract two matrices multiply two matrices display a matrice
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT