Create 2 text files each holding a 3x3 array ofnumbers. Your program should read
ID: 3609274 • Letter: C
Question
Create 2 text files each holding a 3x3 array ofnumbers. Your program should read them in , perform a matrixmultiplication and save to a new text file. Matrix(File1) Matrix (File2) Result matrix 2 3 4 1 2 3 2 6 12 5 2 2 3 2 1 ========= 15 4 2 3 2 3 2 2 2 6 4 6 Create 2 text files each holding a 3x3 array ofnumbers. Your program should read them in , perform a matrixmultiplication and save to a new text file. Matrix(File1) Matrix (File2) Result matrix 2 3 4 1 2 3 2 6 12 5 2 2 3 2 1 ========= 15 4 2 3 2 3 2 2 2 6 4 6Explanation / Answer
import java.io.*;
import java.util.*;
public class matrixMult
{
public static void main(String[]args)throws FileNotFoundException
{
int n,i,j;
n=3;
int [][] a = new int [n][n];
int [][] b = new int [n][n];
int [][] c = new int [n][n];
Scanner in1=new Scanner(newFile("mat1.txt"));
Scanner in2=new Scanner(newFile("mat2.txt"));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{a[i][j]=in1.nextInt();
b[i][j]=in2.nextInt();
}
System.out.println("The starting matrices:");
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
System.out.printf("%3d ",a[i][j]);
System.out.print(" ");
for(j=0;j<n;j++)
System.out.printf("%3d ",b[i][j]);
System.out.println(" ");
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]*b[i][j];
System.out.println(" multiplied matrix:");
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
System.out.printf("%4d ",c[i][j]);
System.out.println(" ");
}
}
}