Can anybody help me out how to write a program of multiplication of two matrix with taking input for rows and column through the Buffered Reader ie. by taking inputs of my own form keyboards ?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Can anybody help me out how to write a program of multiplication of two matrix with taking input for rows and column through the Buffered Reader ie. by taking inputs of my own form keyboards ?
Define the algorithm for the "multiplication of two matrix".how to write a program of multiplication of two matrix
Given that we can help you write the java code.
See: Matrix multiplication for how to multiply two matrices. Personally, I would implement the naive method because it's much easier, but if you want better performance for larger matrices, there's Strassen's method or Coppersmith–Winograd algorithm (only use these for large matrices, they're considerably slower for smaller ones).
If you're unsure how to program in Java, I recommend reading: Leaerning the Java Language.
But i need the code that one can use his/her own input from keyboard for the rows and column. The data input part i can do it.
Do you have to use a BufferedReader? If you can, use the Scanner object. It makes reading in data (especially numbers) much easier.
If you must use a BufferedReader, what I would do is read in "words" (elements separated by spaces), then use Integer.parseInt() or Double.parseDouble() to get the data value from the string. Scanner has methods called nextInt() and nextDouble() which basically automate this process.
public class ArrayMultiplication
{
public static void main(String s[])
{
int rows1=Integer.parseInt(s[0]);
int columns1=Integer.parseInt(s[1]);
int rows2=Integer.parseInt(s[2]);
int columns2=Integer.parseInt(s[3]);
int array1[][]=new int[rows1][columns1];
int array2[][]=new int[rows2][columns2];
int array3[][]=new int[rows1][columns2];
if(columns1==rows2)
{
int m=4;
for(int i=0;i<rows1;i++)
{
for(int j=0;j<columns1;j++)
{
array1[i][j]= Integer.parseInt(s[m]);
m=m+1;
}
}
int l=4+(rows1*columns1);
for(int i=0;i<rows2;i++)
{
for(int j=0;j<columns2;j++)
{
array2[i][j]=Integer.parseInt(s[l]);
l=l+1;
}
}
for(int i=0;i<rows1;i++)
for(int k=0;k<columns2;k++)
for(int j=0;j<rows2;j++)
{
array3[i][k]=array3[i][k]+(array1[i][j])*(array2[j][k]);
}
for(int i=0;i<rows1;i++)
{
System.out.println("");
for(int j=0;j<columns2;j++)
{
System.out.print(array3[i][j]+"\t");
}
}
}
else
{
System.out.println("The arrays cannot be multiplied");
}
}
}