Question:
Hi everybody...is the any one who knows about matrix?
2014-07-26 07:49:40 UTC
Well i have to write a Java program code to perform matrix multiplication. The program should read two matrices (the value of the matrices should come from two different files, for example
Matrix1.txt and Matrix2.txt), should perform the multiplication and should store the result
into a third file. and i'm stuck.

These are the steps the program should take:
1. Asks user for the name of the file containing the first matrix
2. Checks whether or not the file exists (if it does not exist, repeat previous task)
3. Import the first matrix into a matrix object
4. Displays the first matrix and its size
5. Asks user for the name of the file containing the second matrix
6. Checks whether or not the file exists (if it does not exist, repeat previous task)
7. Import the second matrix into a matrix object
8. Displays the second matrix and its size
9. Asks user for the name of the file where the result should be stored
10.Perform the calculation (matrix multiplication)
11.Displays the result of the operation
12.Stores the result into the file
13.Asks user whether or not to perform a new calculation

thanks in advance
Three answers:
The Black Hole
2014-07-26 12:04:41 UTC
You can store the matrix into a 2 dimensional array

int matrix[ ][ ]



If you format the text files in matrix form you can read each line and count the number of elements in a line to determine the size of a row (number of columns).



1 2 3

4 5 6



is a 2 x 3 matrix. 2 rows and 3 columns.

This is referred to as an m x n matrix. m rows and n columns



The rule for multiplying matrices is that the number of rows in the first matrix must match the number of columns in the second matrix.

If matrix A is m x n , then matrix B must be n x p .

The resulting product matrix is m x p, or number of rows in A x number of columns in B.



Matrix multiplication is not commutative.

A x B is not equal to B x A.



Each similar element in A row is multiplied by the same element in B column, then the products are added.

A

1 2 3

4 5 6



B

2 4

7 5

3 8



Row A1 x column B1 = (1*2 + 2*7 + 3*3) = 25

Row A2 x column B1 = (4*2 + 5*7 + 6*3) = 61



Row A1 x column B2 = (1*4 + 2*5 + 3*8 ) = 38

Row A2 x column B2 = (4*5 + 5*5 + 6*8) = 93



The resulting matrix is 2 x 2

25 38

61 93
husoski
2014-07-26 10:11:38 UTC
Stuck on what? If you don't know how to ask the user for a file name, you have not paid attention in previous lessons.



Don't try to write a large program all at once. You'll confuse yourself, and anyone who tries to help you with it.



Start with a portion you think you know how to write and do that. The first version might just ask for a file name, open the file, and display a message if the open fails. That will get your user-input logic started and see that you have the text file in the right place. If you have a problem, then you are debugging a very simple program. You can use a Scanner to read a stream of numbers from a file with nextDouble(), or read text lines with the nextLine() method. A sample to "open" such a scanner is:



Scanner readMat1;

String mat1Name = "matrix1.txt"; // fixed name for testing

try {

.... readMat1 = new Scanner(File(mat1Name));

}

catch (FileNotFoundException ex) {

.... System.out.println( "File not found: " + mat1Name );

.... mat1Name = null; // signal that open failed

}



// If open succeeded, say so. Replace this with matrix reading code later.



if (readMat1 != null) {

.... System.out.println( "Open succeeded on " + mat1Name );

.... readMat1.close(); // always a good idea to close files when done with them

.... readMat1 = null; // reset to null so nobody tries to read, or close again

}



That's some basic file handling using Scanner and File objects. A File alone does not have any I/O capability. It mainly represents a file name (path, actually.) You need a Scanner or some kind of Reader or InputStream to actually read data. Scanners are easier and I expect you already know how to use them from previous assignments.



When you get a version coded and tested, then go on to the next version. You might want to do a save-as between steps to save a copy of known-working code up to that point.



Work in small steps. Version 1 might use a do {} while (readMat1==null); loop to do that "repeat previous action".



If you are expected to make a Matrix class, do that as a separate task and write a test program that runs each constructor and method and verifies that the results are reasonable. Each time you update that class, you can re-run that test program to be sure nothing's broken. As you add new methods, update the test program to include tests that use that new method. Keep all of the old tests, so that everything gets tested every time. The tester can be silent on a test that succeeds, or print out a very short "OK" message so that it's clear looking at the output whether any errors were detected.
?
2014-07-26 07:52:14 UTC
You Can Get The Mention Answer's From The Best Knowlaged Persons Here



http://stackexchange.com/


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...