Tuesday, April 17, 2012

BLUEJ PROGRAM ON MATRIX MULTIPLICATION


In this program we will do multiplication of two matrix.  In doing matrix multiplication, the condition is that rows in first matrix should match the number of columns in the second matrix.


import java.io.*;
class Arr
{
    int m1[][]=new int[10][10];
    int m2[][]=new int[10][10];
    int mult[][]=new int[10][10];
    int i,j,k,r1,c1,r2,c2;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    public void show() throws IOException
    {
       System.out.println("Enter number of rows of first matrix (less than 10)\n");
        r1=Integer.parseInt(br.readLine());
        System.out.println("Enter number of columns of first matrix (less than 10)\n");
        c1=Integer.parseInt(br.readLine());
        System.out.println("Enter number of rows of second matrix (less than 10)\n");
        r2=Integer.parseInt(br.readLine());
        System.out.println("Enter number of columns of second matrix (less than 10)\n");
        c2=Integer.parseInt(br.readLine());
       
    if(r2==c1)
    {
      
        for(i=0;i< r1;i++)
        {
            for(j=0;j< c1;j++)
            {
                System.out.println("Value for 1st matrix:");
m1[i][j]=Integer.parseInt(br.readLine());
}
}

  for(i=0;i< r2;i++)
        {
            for(j=0;j< c2;j++)
            {
                System.out.println("Value for 2nd matrix:");
m2[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\n1st Matrix\n");
        for(i=0;i< r1;i++)
        {
            for(j=0;j< c1;j++)
                System.out.print(" "+m1[i][j]);
            System.out.println();
        }
        System.out.println("\n2nd Matrix\n");
        for(i=0;i< r2;i++)
        {
            for(j=0;j< c1;j++)
                System.out.print(" "+m2[i][j]);
            System.out.println();
        }
       for(i=0;i< r1;i++)
        {
            for(j=0;j< c2;j++)
            {
                mult[i][j]=0;
                for(k=0;k< r1;k++)
                {
                    mult[i][j]+=m1[i][k]*m2[k][j];
                }
            }
        }
    System.out.println("\nFinal Matrix\n");
        for(i=0;i< r1;i++)
        {
            for(j=0;j< c2;j++)
                System.out.print(" "+mult[i][j]);
            System.out.println();
        }
   }
}
}


Rule of matrix multiplication

If the first array is like: 
a  b
c  d

and the second array is like:

e  f
g  h

then the multiplication will be like:
ae + bg   af + bh
ce + dg   cf + dh



4 comments:

  1. sir ..pls can you explain about function overloading..using examples of char type..

    ReplyDelete
    Replies
    1. class Pat
      {

      public void show(char ch)
      {
      if(ch>=97 && ch<=122)
      ch=(char)(ch-32);
      System.out.println(ch);
      }
      public void show(String str)
      {
      str=str.toUpperCase();
      System.out.print(str);
      }
      }

      Delete
  2. Sir please tell me about recursion and explain how to use recursion in class based programs.

    ReplyDelete
    Replies
    1. Please check the program on recursion http://schooljava.blogspot.com/2011/09/bluej-program-to-take-any-decimal.html
      I will discuss about recursion in this site very soon.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner