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
Thumbnails in the picture gallery display the more common color saturation, color tone, and recolor adjustments. true or false.
jeka94
<span>Thumbnails in the Picture gallery display the more common color saturation, color tone, and recolor adjustments. TRUE. It is TRUE</span>
5 0
3 years ago
Which is the correct expansion of the term Internet? 
kirill115 [55]

Answer:

international network

Explanation:

8 0
4 years ago
Read 2 more answers
False when you tap or click the ‘decrease font size’ button, excel assigns the next lowest font size in the font size gallery to
castortr0y [4]
That is actually true. In order to format the font, margins and alignment of a form you need to s<span>elect the control that you want to format, then right-click the selection, and then click Format Control. What you do next is that on the Font tab you select the font type, font style, font size, other formatting options for the selected text and in order to finish to press ok </span>
6 0
3 years ago
Which line(s) immediately flush the output buffer?
Murljashka [212]

Answer:

lines 2,3 and 5 ( c )

Explanation:

The lines that immediately flush the output buffer are :  System.out.print("Two for the show\n"); .   System.out.println("Three to get ready"); and   System.out.flush();

making use of the new line character or printIn() statement the buffered output gets flushed therefore making autoflush true

but System.out.print() doesn't  use any flush technique so we need to flush the buffered output done by the print() statement hence we choose lines 2,3,5

3 0
3 years ago
What are the pros and cons of using ICT​
Setler79 [48]

Answer:

Communication - Speed / time – money can be saved because it's much quicker to move information around. ...

Globalization - Video conferencing saves money on flights and accommodation. ...

Cost effectiveness - It feels free to send an email (although it isn't); it's without doubt cheaper than phone calls.

Explanation:

4 0
3 years ago
Other questions:
  • What New England industry quickly collapsed with the discovery of oil in Pennsylvania?
    11·2 answers
  • The index finger on your right hand types the _____.
    11·1 answer
  • QUESTION 8 Software applications that generate information are located in the first tier of n-tier architectures. True False
    7·1 answer
  • Using a subject directory, you locate a particular topic by clicking links through different levels. a. True b. False
    10·1 answer
  • Which one of the following may be used as an input as well as an output device? A. Fax machine B. Scanner C. Printer D. Voice re
    9·1 answer
  • The SmoothWall open source firewall solution uses colors to differentiate networks. Which color indicates the private, trusted s
    9·1 answer
  • The function of network switch is to _____.
    5·1 answer
  • Write a function that removes duplicates from an array. For example, if remove_ duplicates is called with an array containing 1
    15·1 answer
  • What type of computer is used in ATM?
    11·2 answers
  • Mga halimbawa ng migrasyon​
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!