3x3 Matrix Multiplication Tracing in Java

MATRIX MULTIPLICATION TRACING IN JAVA

Tracing which means to trace the code and find the workflow of the program especially for loops and other complicated conditions. Tracing is one of the important parts of Software Developing. You can trace a code by written format or with the help of a print statement also depending upon your comfort.


Matrix Multiplication Tracing

Declare a 2-D Array to get the input for 1st Matrix and another one to get the input 2nd Matrix from the user or you can declare it directly with the value to be stored. And the 3rd Array is to store the value of Matrix Multiplication of 1st and 2nd Matrix.

Program



Note: Arrays always begins with index zero. It is a 3x3 matrix so you have to declare the arrays index as 9 or above. While using an array with loops ends up with array out of bounds error it's because variable value must be exceeding array index value. c[i][j]=0; this line in this program helps us to reset the value to zero; the details are shown below in the Tracing part.

Tracing Matrix 1


Here two for loops has been used to index a value in a unique position every time and the outer loop iterates for three times and the inner loop iterates for three times for every iteration of the outer loop which makes a total of 9 times. In the tracing part the i denotes the row and j denotes the column of the matrix and A(i,j) is the value entered by the user on the ith row and jth column.



Here both the i and j are starts with zero, after getting the value from user i remains the same and the inner loop will increment by 1 because of the j++ condition it will increment the value of variable by 1, now the i value remains the same and j value will be 1 once the value has been entered the i remains the same because the inner loop hasn't failed yet. Once the inner loop fails it will go to the outer loop and the outer loop will increment by 1 this process continues until the outer loop fails. In this case after the 9th iteration it breaks.

Tracing Matrix 2


Second Matrix which is the same as the first one. This one also iterates for 9 times and it is the same as above for loop but the values are stored in a different 2-D Array.



Tracing Matrix Multiplication


In Matrix Multiplication the 1st row of the 1st matrix will be multiplied to the 2nd matrix 2nd column and then added and the value is stored in the 0th row and 0th column, similarly all the other rows and columns are done in the same way. For that here we are having 3 for loops to do the job. c[i][j]=0 it will set the value to zero after every time it exits the inner loop the purpose of doing this is to prevent the value of previous sum added to the next one it will significantly change all other outputs.



Once the loop enters the outer loop it will enter the 1st inner loop and then it will iterate through the 2nd inner loop each loop iterates 3 times every time. Hence the for loop will iterate upto 27 times and then terminate. Here you can clearly see that c(i,j) is set to 0 every time it exits from the inner loop. c[i][j] += a[i][k] *b[k][j]; this line of code helps us to multiply and add and store value in c[i][j] by changing A matrices column and B matrices row and then adding to the value stored in c[i][j]. As I mentioned earlier you can also use System.out.print(""); to trace a program effectively.

Here is the link for Google KickStart Round A 2020 Plates problem using Java.

0 Comments