Draw a flowchart and write a Java program to multiply two matrices

First we must download the Java SE Development Kit from (http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html).
Then install it. After that set the path variable. Next write the following code in any text editor and save it [with it’s class name.java] in a particular location .
Open CMD and set the saved file location. Then write javac filename.java and press enter. Then type java filename and press enter.

Program code:
import java.util.Scanner;
class MatrixMultiplication
{
public static void main(String args[])
{
Scanner r = new Scanner(System.in);
System.out.println("Enter the number of rows of first matrix");
int rowFM = r.nextInt();
System.out.println("Enter the number of columns of first matrix");
int colFM = r.nextInt();
int first[][] = new int[rowFM][colFM];
System.out.println("Enter the elements of first matrix");
int loop1, loop2;
for ( loop1 = 0 ; loop1 < rowFM ; loop1++ )
{
for (loop2 = 0 ; loop2 < colFM ; loop2++ )
first[loop1][loop2] = r.nextInt();
}
System.out.println("First matrix you entered is:");
for ( loop1 = 0 ; loop1 < rowFM ; loop1++ )
{
for (loop2 = 0 ; loop2 < colFM ; loop2++ )
System.out.print(first[loop1][loop2]+”\t”);
System.out.print(“\n”);
}
System.out.println(“Enter the number of rows of second matrix”);
int rowSM = r.nextInt();
System.out.println(“Enter the number of columns of second matrix”);
int colSM = r.nextInt();
if ( colFM != rowSM )
System.out.println(“Permission decliened! You should enter the same number of columns of first and same number of rows of second matrix.”);
else
{
int second[][] = new int[rowSM][colSM];
System.out.println(“Enter the elements of second matrix”);
int sum=0;
for (loop1 = 0 ; loop1 < rowSM ; loop1++ )
for (loop2 = 0 ; loop2 < colSM ; loop2++ )
second[loop1][loop2] = r.nextInt();
System.out.println(“The Second matrix you entered is:”);
for (loop1 = 0 ; loop1 < rowSM ; loop1++ )
{
for (loop2 = 0 ; loop2 < colSM ; loop2++ )
System.out.print(second[loop1][loop2]+”\t”);
System.out.print(“\n”);
}
int mul[][] = new int[rowFM][colSM];
int loop3 ;
for ( loop1 = 0 ; loop1 < rowFM ; loop1++ )
{
for ( loop2 = 0 ; loop2 < colSM ; loop2++ )
{
for ( loop3 = 0 ; loop3 < rowSM ; loop3++ )
{
sum = sum + first[loop1][loop3]*second[loop3][loop2];
}
mul[loop1][loop2] = sum;
sum = 0;
}
}
System.out.println(“Multiplication of the two matrixes are:”);
for ( loop1 = 0 ; loop1 < rowFM ; loop1++ )
{
for ( loop2 = 0 ; loop2 < colSM ; loop2++ )
System.out.print(mul[loop1][loop2]+”\t”);
System.out.print(“\n”);
}
}
}
}
Dry run of matrix multiplication program:
When we compile the java program, a new file is created and the file is no longer in .java extension the MatrixMultiplication.java file remains beside a new file is created e.g. MatrixMultiplication.class. Then we run the file by the command: java MatrixMultiplication

Step 1: 
It will check the line Scanner r = new Scanner (system.in); and understands what kind of input it will take. The scanner class will work as we already have declared it in the header section import java.util.Scanner. Then from the next line it prints a message Enter the number of rows of the first Matrix:

Step 2:
In the line int rowFM=r.nextInt(); it will stop running the program and waits for user’s input. Suppose we input 2 and then press enter. Then the program will again start running. The program itself will check whether the inputted value is legal or not. As we have chosen the Scanner r = new Scanner (system.in);method it will permit the input. After that it will print a message coming to the line “Enter the number of columns of the first Matrix:” then it will go to the next line int colFM=r.nextInt(); and wait for user’s input. Let the input be 3.

Step 3: 
Now we need to enter the elements in a 2d matrix as the rows of the matrix are 2 and columns are 3. The program checks the 2d matrix has been allocated in the next line that is: int first[][]= new int[rowFM][colFM].

Step 4:
Now the program checks its next line int loop1, loop2; . It means that loop1 and loop2 are two integer type variables. It can hold or store integer values. Then in the next line it checks next line for (loop1=0; loop1< rowFM ; loop1++ ). The line describes for loop1=1 and if loop1 less than rowFM the following statement will be executed after that the loop1 s value will be increased by 1. And the following statement is for (loop2=0 ; loop2 < colFM ; loop2 +1 ) but the line describes almost similar thing. This is called nested loop. So again we will find the following statement and the following statement is first [loop1] [loop2] = r.nextInt () ; The most complex thing is to the students new to programming is how are the loops logic work?

Comments

Popular posts from this blog

Write a Windows Program to display a message box with cancel, Retry and Continue.

How computer is used as social networking?

Differentiate between primary and secondary memory