210k views
3 votes
You are to write a program name matrix.java that multiplies two matrices. 1. Your program should prompt the user for the dimensions of the two squares matrices, making sure that the user input is greater than or equal to 4. [ yes, an example would be a 4X4 matrix] 2. If the above is not met, prompt the user for a new value. 3. Now generate random integer numbers to fill both matrices. 4. Display these two matrices on the screen. 5. Multiply the two matrices and display the result on the screen.

1 Answer

6 votes

Answer:

// Matrix.java

// package

import java.util.*;

// class definition

class Matrix

{

// main method of the class

public static void main (String[] args) throws java.lang.Exception

{

try{

// object to read the input

Scanner scr=new Scanner(System.in);

// object to generate random Number

Random rand = new Random();

// Part 1

// variable

int n;

System.out.print("Enter the size of square matrix:");

// read the size of matrix

n=scr.nextInt();

// check size is less than 4

while(n<4)

{

System.out.println("Size of array must be greater or equal to 4:");

// ask again to enter the size

// Part 2

System.out.print("Enter the size again:");

// read the size again

n=scr.nextInt();

}

// matrix of size n

int arr1[][]=new int[n][n];

int arr2[][]=new int[n][n];

int arr3[][]=new int[n][n];

//part 3

//fill the matrix with Random Number

for(int x=0;x<n;x++)

{

for(int y=0;y<n;y++)

{

arr1[x][y]=rand.nextInt(10);

}

}

for(int x=0;x<n;x++)

{

for(int y=0;y<n;y++)

{

arr2[x][y]=rand.nextInt(10);

}

}

// part 4

// print the matrix Elements

System.out.println("Elements of the first array:");

for(int x=0;x<n;x++)

{

for(int y=0;y<n;y++)

{

System.out.print(arr1[x][y]+" ");

}

System.out.println();

}

System.out.println("Elements of the second array:");

for(int x=0;x<n;x++)

{

for(int y=0;y<n;y++)

{

System.out.print(arr2[x][y]+" ");

}

System.out.println();

}

// part 4

// matrix multiplication and print Elements

System.out.println("Array after multiplication:");

for(int i=0;i<n;i++){

for(int j=0;j<n;j++){

arr3[i][j]=0;

for(int k=0;k<n;k++)

{

arr3[i][j]+=arr1[i][k]*arr2[k][j];

}

System.out.print(arr3[i][j]+" ");

}

System.out.println();

}

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

In part 1, ask user to enter the size of square matrix.check the size is

less than 4 or not.In part 2, if the size is less than 4 then ask again to

enter the size.In part 3, fill the matrix with random numbers.In part 4,

print both the matrix elements.In part 5, perform matrix multiplication and

store the elements in third matrix.Then print the matrix elements of third

matrix.

Output:

Enter the size of square matrix:3

Size of array must be greater or equal to 4:

Enter the size again:4

Elements of the first array:

2 8 0 8

8 6 5 2

6 7 6 7

8 1 0 8

Elements of the second array:

2 1 6 0

1 4 8 6

6 4 1 6

6 3 6 9

Array after multiplication:

60 58 124 120

64 58 113 84

97 79 140 141

65 36 104 78

User Vidya
by
5.3k points