Question
(Explain each component in the following code how you usually would with comments) import java.util.Random; import java.util.Scanner; public class matrix { public static void main(String[]
(Explain each component in the following code how you usually would with comments)
import java.util.Random; import java.util.Scanner;
public class matrix {
public static void main(String[] args) { Scanner in = new Scanner(System.in); Random random = new Random(); String choice; int size, sum; int[][] A, B, C; while (true) { while (true) { System.out.print("Enter size of matrix[>=50]: "); size = in.nextInt(); if(size >= 50) break; System.out.println("Error. Enter size greater than or equal to 50"); } A = new int[size][size]; B = new int[size][size]; C = new int[size][size]; long startTime = System.currentTimeMillis(); for(int i =0 ; i < size; ++i) { for(int j =0 ; j < size; ++j) { A[i][j] = 1 + random.nextInt(99); B[i][j] = 1 + random.nextInt(99); sum = 0; for(int k = 0; k < size; ++k) { sum += A[i][k] * B[k][j]; } C[i][j] = sum; } } System.out.println("Matrix A"); for(int i = 0; i < size; ++i) { for(int j = 0; j < size; ++j) { System.out.printf("%3d ", A[i][j]); } System.out.println(); } System.out.println("Matrix B"); for(int i = 0; i < size; ++i) { for(int j = 0; j < size; ++j) { System.out.printf("%3d ", B[i][j]); } System.out.println(); } System.out.println("Matrix A * B"); for(int i = 0; i < size; ++i) { for(int j = 0; j < size; ++j) { System.out.printf("%6d ", C[i][j]); } System.out.println(); } long endTime = System.currentTimeMillis(); System.out.println("It took " + (endTime-startTime) + " milliseconds"); System.out.print("Do you want to try again(y or n)? "); choice = in.next(); if(choice.equalsIgnoreCase("n")) break; } }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started