Question

The goal in this assignment is to perform various matrix multiplications. Input matrices A and B...

The goal in this assignment is to perform various matrix multiplications. Input matrices A and B are read from a file. Output matrices A*A, A*B, B*A and B*B are written to file.

-Ask the user to enter the input and output file names.

-Read the input data matrices A and B from the input file. (A sample input file is shown in Appendix A)

            -Read the number of rows n.

            -Read matrix A, which has n rows and n columns.

            -Read string “A”.(this data is not used)

            -Read matrix B, which has n rows and n columns.

            -Read string “B”. (this data is not used)

- Calculate matrices AA, AB, BA and BB, where

            -AA=A*A

            -AB=A*B

            -BA=B*A

            -BB=B*B

-Write the following to the output file (A sample output file is shown in Appendix B. Use type double for the matrix elements, and include one decimal place in the output of each number)

            -String “First matrix A”

            - line space

            -Matrix A, which has n rows and n columns (requires n lines)

            -line space

            -String “Second matrix B”

            - line space

            -Matrix B, which has n rows and n columns (requires n lines)

            -line space

            -String “Matrix multiplication A*A”

            - line space

            -Matrix AA, which has n rows and n columns (requires n lines)

            -line space

            -String “Matrix multiplication A*B”

            - line space

            -Matrix AB, which has n rows and n columns (requires n lines)

            -line space

            -String “Matrix multiplication B*A”

            - line space

            -Matrix BA, which has n rows and n columns (requires n lines)

            -line space

            -String “Matrix multiplication B*B”

            - line space

            -Matrix BB, which has n rows and n columns (requires n lines)

            -line space

Note: Read and write matrixes from files, and explain each steps.

the language is JAVA.

Thank you.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer to the above question is as follows-

Reading file contents can be done though the Scanner class object.

But we can't write to a file using Scanner.

For the purpose of writing to a file we use the Writer class object. Then at last we close the opened files.

The required JAVA code is as follows -


import java.io.*;
import java.util.*;


class test {
  
  
/*function to multiply two matrices and return their product matrix */
public static int[][] multiplyTwoMatrices( int A[][], int B[][], int n){
  
int mul[][] = new int [n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
mul[i][j] = 0;
for ( int k = 0; k < n; k++)
mul[i][j] += A[i][k] *
B[k][j];
}
}
  
return mul;
}



// main method with all the functionalities
   public static void main (String[] args)throws FileNotFoundException,IOException {
       // may throw a IOException or FileNotFoundException
      
       // Scanner class to take user input of input and output file
       Scanner sc = new Scanner(System.in);
      
       //take input file name
       System.out.println("Enter the input file name.");
       String input = sc.nextLine();
      
       //take output file name
       System.out.println("Enter the output file name.");
       String output = sc.nextLine();
      
       //Scanner class object to read a file contents
       Scanner scanner = new Scanner(new File(input));
      
       int n = scanner.nextInt(); // to store the size of matrix
int [][] a = new int [n][n]; // store array A
int [][] b = new int [n][n]; // to store array B

  
// read 2d array A from file
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
a[i][j] = scanner.nextInt();
}
}
//read string A ( not usable )
String A = scanner.next();
  
// read 2d array B from file
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
b[i][j] = scanner.nextInt();
}
}
//read string B ( not usable )
String B = scanner.next();
  
  
//creating matrices to store the resulting matrices
int [][] aa = new int [n][n];
int [][] ab = new int [n][n];
int [][] ba = new int [n][n];
int [][] bb = new int [n][n];

//calling function ( defined above)to find the product matrices and store them accordingly
aa = multiplyTwoMatrices( a, a , n );
ab = multiplyTwoMatrices( a, b, n );
ba = multiplyTwoMatrices( b, a , n );
bb = multiplyTwoMatrices( b , b, n );
  
  
//Writer class object to write to the output file
Writer wr = new FileWriter(output);
  
// write the matrix A to the file
wr.write("First matrix A\n");
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
wr.write(String.valueOf( a[i][j] )); // write int to file
wr.write(" ");
}
wr.write("\n"); //change line
}
  
// write the matrix B to the file
wr.write("\nSecond matrix B\n");
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
wr.write(String.valueOf( b[i][j] )); // write int to file
wr.write(" ");
}
wr.write("\n"); //change line
}


// write the matrix product A*A to the file
wr.write("\nMatrix multiplication A*A\n");
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
wr.write(String.valueOf( aa[i][j] )); // write int to file
wr.write(" ");
}
wr.write("\n"); //change line
}
  
// write the matrix product A*B to the file
wr.write("\nMatrix multiplication A*B\n");
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
wr.write(String.valueOf( ab[i][j] )); // write int to file
wr.write(" ");
}
wr.write("\n"); //change line
}

// write the matrix product B*A to the file
wr.write("\nMatrix multiplication B*A\n");
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
wr.write(String.valueOf( ba[i][j] )); // write int to file
wr.write(" ");
}
wr.write("\n"); //change line
}
  
// write the matrix product B*B to the file
wr.write("\nMatrix multiplication B*B\n");
for(int i = 0; i<n; i++ )
{
for( int j =0; j < n; j++){
wr.write(String.valueOf( bb[i][j] )); // write int to file
wr.write(" ");
}
wr.write("\n"); //change line
}

wr.flush(); //flush Writer class object
wr.close(); // close the file


   }
  
}

Screenshot of the executed code is as follows-

The Screenshot of tested output is as follows -

Screenshot of the input file is as follows-

Screenshot of the output file is as follows-

Please COMMENT if you have any queries regarding the solution.

Please THUMBS UP and UPVOTE if you find the answer helpful and satisfactory.

Thank You!!!


Add a comment
Know the answer?
Add Answer to:
The goal in this assignment is to perform various matrix multiplications. Input matrices A and B...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The goal in this assignment is to perform various matrix multiplications. Input matrices A and B...

    The goal in this assignment is to perform various matrix multiplications. Input matrices A and B are read from a file. Output matrices A*A, A*B, B*A and B*B are written to file. Code Requirements: -Include the problem statement in the top of the code (copy and paste this document and enclose in comments) -Include comments in the code -Ask the user to enter the input and output file names. -Read the input data matrices A and B from the input...

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • In C++ Design a class to perform various matrix operations. A matrix is a set of...

    In C++ Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of five rows and six columns, we say that the matrix A is of the size 5 X 6 and sometimes denote it as Asxc. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two...

  • Recall that if A is an m times n matrix and B is a p × q matrix

    Recall that if A is an m times n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p. in which case C is an m × q matrix.  a. Write a function M-file that takes as input two matrices A and B, and as output produces the product by rows of the two matrices. For instance, if A is 3 times 4 and B is...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and on...

    MATLAB HELP!!! Recall that if A is an m × n matrix and B is a p × q matrix, then the product C = AB is defined if and only if n = p, in which case C is an m × q matrix. 5. Recall that if A is an mx n matrix and B is a px q matrix, then the product C-AB is defined if and only if n = p, in which case C is...

  • READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you...

    READ CAREFULLY AND CODE IN C++ Dynamic Programming: Matrix Chain Multiplication Description In this assignment you are asked to implement a dynamic programming algorithm: matrix chain multiplication (chapter 15.2), where the goal is to find the most computationally efficient matrix order when multiplying an arbitrary number of matrices in a row. You can assume that the entire input will be given as integers that can be stored using the standard C++ int type and that matrix sizes will be at...

  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp...

    Write a program mexp that multiplies a square matrix by itself a specified number of times、mexp takes a single argument, which is the path to a file containing a square (k × k) matrix M and a non-negative exponent n. It computes M and prints the result Note that the size of the matrix is not known statically. You ust use malloc to allocate space for the matrix once you obtain its size from the input file. Tocompute M". it...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT