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

To understand the value of counting loops: Write a java program that implements matrix multiplication using...
To understand the value of counting loops: Write a java program that implements matrix multiplication using counting loop constructs. Then write the same program using only logical loops—for example, while loops. 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...
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 program that uses nested for loops to print a multiplication table. Your program should...
Write a program that uses nested for loops to print a multiplication table. Your program should print out the multiplication table starting a 1 and going to 12. Output should be like: 1 x 1 = .1 x 2 = 2
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
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 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...
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
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...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS...
Develop a C program for matrix multiplication focusing on using malloc and pointers WITHOUT USING BRACKETS [] !! * DO not use [ ] 'brackets', focus on using malloc, calloc, etc... Program will ask user for the name of the text file to be read Read the datafile with format: 1 2 1 1 2 3 4 So the first three lines will represent m, n, p (Matrix A: m x n ||| Matrix B: n x p), follwing are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT