1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
VLD [36.1K]
3 years ago
5

You are to write a program name matrix.java that multiplies two matrices. 1. Your program should prompt the user for the dimensi

ons 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.
Computers and Technology
1 answer:
raketka [301]3 years ago
6 0

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;}

}

}

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

You might be interested in
Write a program that reads and unspecified number of integers, determines how many positive and negative values have been read,
KonstantinChe [14]

Answer:

The program was wrote in the version 3.9.5 of python and it's in the homework.txt file.

Download txt
6 0
3 years ago
Three reasons why users attach speakers to their computer
Alla [95]
For media sound
For the game's multimedia sound
For essential system sound
5 0
3 years ago
On a digital clock the displayed time changes constantly (True or False)
Vlada [557]
The answer is true as it will change on its own
8 0
4 years ago
Most search engines provide specific pages on which you can search for____ and
vazorg [7]

Answer: c

Explanation: a

7 0
3 years ago
Read 2 more answers
Which safety issue is considered to be a data safety issue?
Andrews [41]
<span>Computer and technology-related dangers to an individual's personal, financial, emotional or physical well-being are considered as data safety issues.
There are several things you should do to prevent computer dangers: </span><span>make regular backups of files, protecting yourself against viruses (use an anti-virus software) , <span>use a  passwords so that access to data is restricted, use a firewall , </span></span>
4 0
4 years ago
Other questions:
  • You receive a file named Project4.xlsx as an attachment to an email message. What do you expect the file to contain?
    8·1 answer
  • Which kind of file would be hurt most by lossy compression algorithm
    8·2 answers
  • If Nancy receives an encrypted message from Matthew, which key does she use to read it? Nancy’s private key Nancy’s public key M
    8·1 answer
  • Describe data center technology and its relevance to modern-day cloud computing
    14·1 answer
  • What is word length in computer​
    10·2 answers
  • Cómo se hacían antes las cosas que hoy en día se hacen apoyadas en aplicaciones y software?
    10·1 answer
  • Identify what algorithm or concept the following code relates to. void Sort(int array[], int tempArray[], int left, int right) {
    6·1 answer
  • Data is communicated through various input devices true or false​
    8·1 answer
  • Which statement best describes how the programming layer of abstraction in
    10·1 answer
  • How do you answer other peoples questions on Brainly? I know how to ask them but not answer hmm...
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!