(Explain each component in the following code how you usually would with comment
ID: 3747463 • Letter: #
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[] 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;
}
}
}
Explanation / Answer
PROGRAM
import java.util.Random;
import java.util.Scanner;
public class matrix {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // create Scanner Object in
Random random = new Random(); // create Random Object random
String choice;
int size, sum;
int[][] A, B, C; // three 2D arrays
while (true) { // create infinity while loop when type n terminate while loop (outer loop)
while (true) { // create infinity while loop when size <50 terminate this loop (inner loop)
System.out.print("Enter size of matrix[>=50]: ");
size = in.nextInt(); // read size of matrix
if(size >= 50) break; // check size>=50 then terminate inner while loop and create three 2D Arrays A,B and C
System.out.println("Error. Enter size greater than or equal to 50"); // if size<50 display this error message
}
// create 3 2D array A,B and C
A = new int[size][size];
B = new int[size][size];
C = new int[size][size];
// calculate the time complexity of matrix multiplication
// using system time in milliseconds using System.currentTimeMillis();
long startTime = System.currentTimeMillis();
for(int i =0 ; i < size; ++i) { // create for loop for row size (outer loop)
for(int j =0 ; j < size; ++j) { // create for loop for column size(inner loop)
A[i][j] = 1 + random.nextInt(99); // generate random numbers of A matrix
B[i][j] = 1 + random.nextInt(99); // generate random numbers of B matrix
sum = 0; // initialize sum=0
for(int k = 0; k < size; ++k) { // create for loop for calculate matrix multiplication
sum += A[i][k] * B[k][j]; // calculate sum of two multiplied elements from matrices A and B
}
C[i][j] = sum; // assign sum to C matrix
}
}
// display First Matrix A
System.out.println("Matrix A");
for(int i = 0; i < size; ++i) { // create for loop for row size
for(int j = 0; j < size; ++j) { // create for loop for column size
System.out.printf("%3d ", A[i][j]); // display First Matrix A
}
System.out.println(); // control cursor to next line
}
// display Second Matrix B
System.out.println("Matrix B");
for(int i = 0; i < size; ++i) { // create for loop for row size
for(int j = 0; j < size; ++j) { // create for loop for column size
System.out.printf("%3d ", B[i][j]); // display Second Matrix B
}
System.out.println(); // control cursor to next line
}
// display resultant multiplication matrix of A *B
System.out.println("Matrix A * B");
for(int i = 0; i < size; ++i) { // create for loop for row size
for(int j = 0; j < size; ++j) { // create for loop for column size
System.out.printf("%6d ", C[i][j]); // display mutiplied matrix
}
System.out.println();
}
long endTime = System.currentTimeMillis(); // count time complexity endTime
System.out.println("It took " + (endTime-startTime) + " milliseconds"); // display total time using endTime - startTime in milliseconds
System.out.print("Do you want to try again(y or n)? "); // system ask continue next
choice = in.next(); // read choice
if(choice.equalsIgnoreCase("n")) break; // check choice='n' then terminate this program
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.