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
Which line in the following program will cause a compiler error? #include using namespace std; int main() { int number =5; if (n
DanielleElmas [232]

Answer:

  1. #include  <iostream>
  2. using namespace std;
  3. int main() {
  4. int number =5;
  5. if (number>=0&& number <=100){
  6.    cout<<"passed.\n";
  7. }
  8. else{
  9.    cout<<"failed.\n";
  10. }
  11. return 0;
  12. }

Explanation:

There where multiple errors in the code given in the questions

Line 1: Missing <iostream>

Line 5: The comparison operator was wrong correction is highlighted

Line 12 Missing closing brace for the main function

All the errors have been fixed and the code above compiles

4 0
3 years ago
Que es un algoritmos
marin [14]
Algorithms: rules to follow when problem-solving
5 0
3 years ago
Read 2 more answers
Consider the following Stack operations:
maxonik [38]

Answer:

Sequence of popped values: h,s,f.

State of stack (from top to bottom): m, d

Explanation:

Assuming that stack is  initially empty. Suppose that p contains the popped values. The state of the stack is where the top and bottom are pointing to in the stack. The top of the stack is that end of the stack where the new value is entered and existing values is removed. The sequence works as following:

push(d) -> enters d to the Stack

Stack:  

d ->top

push(h) -> enters h to the Stack

Stack:

h ->top

d ->bottom

pop() -> removes h from the Stack:

Stack:

d ->top

p: Suppose p contains popped values so first popped value entered to p is h

p = h

push(f) -> enters f to the Stack

Stack:

f ->top

d ->bottom

push(s) -> enters s to the Stack

Stack:

s ->top

f

d ->bottom

pop() -> removes s from the Stack:

Stack:

f ->top

d -> bottom

p = h, s

pop() -> removes f from the Stack:

Stack:

d ->top

p = h, s, f

push(m) -> enters m to the Stack:

Stack:

m ->top

d ->bottom

So looking at p the sequence of popped values is:

h, s, f

the final state of the stack:

m, d

end that is the top of the stack:

m

6 0
3 years ago
how can cindy collaborate with mallory in order for cindy to be able to decrypt all subsequent encrypted messages that are sent
Olenka [21]

Cryptography, like digital signatures, is used to accomplish nonrepudiation, which also includes services for authentication, auditing, and logging.

Digital signatures in online transactions make assurance that a party cannot later dispute the sending of information or the validity of its signature. Alice can encrypt the communication using her own Private Key. As a result, only her public key, which she is aware Bob (and everyone else) has access to, may be used to decrypt her message. Bob receives the message, and he uses Alice's public key to decrypt it. Secret key cryptography (symmetric encryption): To encrypt and decrypt given messages, both the sender and the receiver must use the same key.

Learn more about information here-

brainly.com/question/15709585

#SPJ4

6 0
1 year ago
If a binary search is applied to an array with 1024 elements, in the worst case, the main loop executes, approximately, _____.
trasher [3.6K]

Answer:

10.

Explanation:

Binary search divides the array to be search each in half according to the value of the element.

The worst case time complexity of binary search is O(logN).

In this case the time complexity will come out to be log₂(1024)=10.

So the binary search can divide this array in half maximum of 10 times.

Hence the main loop will executes 10 times.

7 0
4 years ago
Other questions:
  • Blender questions
    8·1 answer
  • You can display content variations to mobile, tablet or desktop users.
    12·1 answer
  • Write a Python program that reads the CSV file, compares the population estimates of every row for 2010 and 2017 and computes th
    10·1 answer
  • If you are a member of a security penetration testing team, and you identify vulnerabilities and exploits, what should you obtai
    11·1 answer
  • Examine the following algorithm.
    9·1 answer
  • Muultimedia Promo try answer this question and explaining them or give examples of that topic Thanks
    10·1 answer
  • Please help with my Python code - Functions
    6·1 answer
  • Monero is cryptocurrency that focuses on transparency of ownership. True or false
    12·1 answer
  • How do mutations affect natural selection? brain pop
    11·1 answer
  • Which two features could be added to a game using an "if () then " block
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!