Question

In: Computer Science

To understand the value of counting loops: Write a java program that implements matrix multiplication using...

To understand the value of counting loops:

  1. Write a java program that implements matrix multiplication using counting loop constructs.
  2. Then write the same program using only logical loops—for example, while loops.
  3. Write 2 classes for each program

Sample Result is shown below:

Enter the number of rows of matrix A: 2

Enter the number of columns of matrix A: 3

Enter the number of columns of matrix B: 3

Enter the number of columns of matrix B: 4

Enter matrix A;

1 2 3

4 5 6

Enter matrix B:

7 8 9 10

11 12 13 14

15 16 17 18

Matrix A:

1 2 3

4 5 6

Matrix B:

7    8    9 10

11    12 13 14

15 16 17    18

Product of matrix A and Matrix B ( A x B) :

74 80 86 92

173 188 203    218   

Solutions

Expert Solution

Program:

import java.util.Scanner;

class Main

{

public static void main(String args[])

{

int rowSizeA,columnSizeA,rowSizeB,columnSizeB;

Scanner sc = new Scanner(System.in);

System.out.print("Enter the number of rows of matrix A: ");

rowSizeA = sc.nextInt();

System.out.print("Enter the number of columns of matrix A: ");

columnSizeA = sc.nextInt();

System.out.print("Enter the number of rows of matrix B: ");

rowSizeB = sc.nextInt();

System.out.print("Enter the number of columns of matrix B: ");

columnSizeB = sc.nextInt();

int A[][] = new int[rowSizeA][columnSizeA];

int B[][] = new int[rowSizeB][columnSizeB];

if (columnSizeA != rowSizeB){

System.out.println("The matrices can't be multiplied with each other.");

System.exit(0);

}

System.out.println("Enter matrix A:");

for (int i = 0; i < rowSizeA; i++)

{

for (int j = 0; j < columnSizeA; j++)

{

A[i][j] = sc.nextInt();

}

}

System.out.println("Enter matrix B:");

for (int i = 0; i < rowSizeB; i++)

{

for (int j = 0; j < columnSizeB; j++)

{

B[i][j] = sc.nextInt();

}

}

int C[][] = new int[rowSizeA][columnSizeB];

for (int i = 0; i < rowSizeA; i++)

{

for (int j = 0; j < columnSizeB; j++)

{

for (int k = 0; k < rowSizeB; k++)

{

C[i][j] = C[i][j] + A[i][k] * B[k][j];

}

}

}

System.out.println("Matrix A:");

for (int i = 0; i < rowSizeA; i++)

{

for (int j = 0; j < columnSizeA; j++)

{

System.out.print(A[i][j]+"\t");

}

System.out.println();

}

System.out.println("Matrix B:");

for (int i = 0; i < rowSizeB; i++)

{

for (int j = 0; j < columnSizeB; j++)

{

System.out.print(B[i][j]+"\t");

}

System.out.println();

}

System.out.println("Product of matrix A and Matrix B ( A x B) : ");

for (int i = 0; i < rowSizeA; i++)

{

for (int j = 0; j < columnSizeB; j++)

System.out.print(C[i][j]+"\t");

System.out.print("\n");

}

}

}

Output:


Related Solutions

Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
JAVA 6.8 PRACTICE: Loops*: Biggest difference Write a program that outputs the biggest difference (absolute value)...
JAVA 6.8 PRACTICE: Loops*: Biggest difference Write a program that outputs the biggest difference (absolute value) between any successive pair of numbers in a list. Such a list might represent daily stock market prices or daily temperatures, so the difference represents the biggest single-day change. The input is the list size, followed by the numbers. If the input is 5 60 63 68 61 59, the output is 7. Hints: Declare a variable for the current number, and another for...
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
How do you write a java program that implements Majority-Element using Boyer & Moore approach?
How do you write a java program that implements Majority-Element using Boyer & Moore approach?
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS...
IN jAVA Language PLEASE Write a JAVA program that implements the following disk-scheduling algorithms: a. FCFS b. SSTF c. SCAN Your program will service a disk with 5,000 cylinders numbered 0 to 4,999. The program will generate a random series of 50 requests and service them according to each of the algorithms you chose. The program will be passed the initial position of the disk head as a parameter on the command line and report the total amount of head...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT