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.
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...
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...
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 “*”.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT