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.

Results 1 to 3 of 3

Thread: What's wrong with the code? Multiplication of Matrices

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with the code? Multiplication of Matrices

    I ALWAYS GET THE OUTPUT AS ZERO. PLEASE TELL ME WHERE IS MY MISTAKE
    import java.util.*;
    class Multi
    {
    public static void main(String args[])
    {
    Scanner sc=new Scanner(System.in);
    int m1,n1,m2,n2,i,j,k;

    System.out.println("Enter the no of rows for first matrix");
    m1=sc.nextInt();
    System.out.println("Enter the no of coloumns for first matrix");
    n1=sc.nextInt();
    System.out.println("Enter the no of rows for second matrix");
    m2=sc.nextInt();
    System.out.println("Enter the no of coloumns for second matrix");
    n2=sc.nextInt();
    int a[][]=new int[m1][n1];
    int b[][]=new int[m2][n2];
    int c[][]=new int[m1][n2];
    if(n1!=m2)
    System.out.println("Not possible");
    else
    {
    System.out.println("Enter the elements of first matrix");
    for(i=0;i<m1;i++)
    {
    for(j=0;j<n1;j++)
    {
    a[i][j]=sc.nextInt();

    }
    }
    System.out.println("Enter the elements of second matrix");
    for(i=0;i<m2;i++)
    {
    for(j=0;j<n2;j++)
    {
    a[i][j]=sc.nextInt();
    }
    }
    for(i=0;i<m1;i++)
    {
    for(j=0;j<n2;j++)
    {
    c[i][j]=0;
    {
    for(k=0;k<n1;k++)
    {
    c[i][j]=c[i][j]+a[i][k]*b[k][j];
    }
    }
    }
    }
    System.out.println("The multiplied matrix is");
    for(i=0;i<m1;i++)
    {
    for(j=0;j<n2;j++)
    {
    System.out.print(c[i][j]+"\t");
    }
    System.out.println();
    }
    }


    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What's wrong with the code? Multiplication of Matrices

    First, please read the announcements at to the of every subforum. This post describes how to wrap your code in the code tags (as is, your code is virtually unreadable). Next, add some println's to evaluate the values of each entered matrix (hint, esp. the b array)

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What's wrong with the code? Multiplication of Matrices

    The Arrays class has a method: deepToString() that is useful when debugging two dim arrays.
    Use it to format the array for printing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. ILL CONDITION MATRICES
    By soso84 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 27th, 2013, 09:54 AM
  2. java help with inversing matrices
    By gspease839 in forum What's Wrong With My Code?
    Replies: 25
    Last Post: February 9th, 2013, 08:34 AM
  3. Solving linear equation systems including sparse matrices
    By GregXel in forum Java Theory & Questions
    Replies: 1
    Last Post: November 24th, 2012, 01:05 PM
  4. Java program to do Matrix operation
    By saladfingers73 in forum Collections and Generics
    Replies: 5
    Last Post: March 7th, 2012, 09:17 AM
  5. Multiplication code
    By bomboy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 5th, 2011, 02:25 AM

Tags for this Thread