for (k=0; k<M; k++)  
   for (i=0; i<N; i++){  
      c[i][k]=0.0;  
          for(j=0;j<P;j++) 
        c[i][k]+ =a[i][j]*b[j][k]; 

I have implemented the above sequential matrix-matrix multiplication algorithm, but i'm not sure about implementing a method to display the output to verify that the algorithm works as expected.

Recommended Answers

All 3 Replies

If you just want to see the contents of the array, the Arrays class's toString() method will format a single dim array for printing. Loop through the first dim and pass it to the toString() method to format and print what the toString() method returns. Something like:
S.o.p(Arrays.toString(c[i]));

class MatrixMultiplication.java

public class MatrixMultiplicationClass {
//------------------------------------------------------------------------------
    //instance variables
    private double[][] A; //first matrix
    private double[][] B; //second matrix
    private double[][] resultantMatrix; //Product matrix
    private int rowsA;
    private int columnsA;
    private int rowsB;
    private int columnsB;
//------------------------------------------------------------------------------
    //default constructor
    public MatrixMultiplicationClass(){

        //initialisation
        rowsA = 0;
        columnsA = 0;
        rowsB = 0;
        columnsB = 0;
        A = new double[rowsA][columnsA];
        B = new double[rowsB][columnsB];
        resultantMatrix = new double[columnsA][rowsB];
    };
//------------------------------------------------------------------------------
/*    //constructor with parameters
    public MatrixMultiplicationClass(int rowsA,int columnsA,int rowsB, int columnsB){
            this.rowsA = rowsA;
            this.columnsA = columnsA;
            this.rowsB = rowsB;
            this.columnsB = columnsB;
    };*/
//------------------------------------------------------------------------------
    //computeMatrix() method
    public void computeMatrix(){
        if(A[columnsA].length != B[rowsB].length)
        {
            System.out.println("\nThe number of columns in first matrix does not equal the number of rows in second matrix");
            System.out.println("\nTherefore matrix multiplication cannot be computed.");
        }else{
              try{        
        for(int i=0;i<=columnsA;i++)
        {
            for(int j=0;j<=rowsB;j++)
            {
                resultantMatrix[i][j] = 0.0;
                for(int k=0;k<rowsB;k++)
                {
                    resultantMatrix[i][j]+=A[i][j]*B[j][i];
        for(int z=0;z<resultantMatrix.length;z++)
        {
            for(int c=0;c<resultantMatrix[i].length;c++)
                System.out.printf("%7d", resultantMatrix[i][j]);
                System.out.println();
        } 

                }
            }
        }

        }catch(NullPointerException npe){
            JOptionPane.showMessageDialog(null,""+npe,"",JOptionPane.ERROR_MESSAGE);
        }
        }

    };
//------------------------------------------------------------------------------
    //display the resultant matrix
    public void displayMatrix(){


    };
//------------------------------------------------------------------------------
    //implementation of access and mutator methods
//------------------------------------------------------------------------------
    public void setRowsA(int rowsA){
        this.rowsA = rowsA;
    }
//------------------------------------------------------------------------------
    public int getRowsA(){
        return rowsA;
    }
//------------------------------------------------------------------------------
    public void setColumnsA(int columnsA){
        this.columnsA = columnsA;
    }
//------------------------------------------------------------------------------
    public int getColumnsA(){
        return columnsA;
    }
//------------------------------------------------------------------------------
    public void setRowsB(int rowsB){
        this.rowsB = rowsB;
    }
//------------------------------------------------------------------------------
    public int getRowsB(){
        return rowsB;
    }
//------------------------------------------------------------------------------
    public void setColumnsB(int columnsB){
        this.columnsB = columnsB;
    }
//------------------------------------------------------------------------------
    public int getColumnsB(){
        return columnsB;
    }
//------------------------------------------------------------------------------
}

class MainClass.java

public class MainClass {
    private static Scanner console = new Scanner(System.in);
    private static MatrixMultiplicationClass myMatrix;
    public static void main(String[] args){
        myMatrix = new MatrixMultiplicationClass();
        try{
        System.out.println("\nEnter the number of rows for first matrix: ");
        myMatrix.setRowsA(console.nextInt());
        System.out.println("\nEnter the number of columns for first matrix: ");
        myMatrix.setColumnsA(console.nextInt());
        System.out.println("\nEnter the number of rows for second matrix: ");
        myMatrix.setRowsB(console.nextInt());
        System.out.println("\nEnter the number of columns for second matrix: ");
        myMatrix.setColumnsB(console.nextInt());

        myMatrix.computeMatrix();
        }catch(ArrayIndexOutOfBoundsException aiobe)
        {
            JOptionPane.showMessageDialog(null,""+aiobe,"",JOptionPane.ERROR_MESSAGE);
        }
    };
}

When i run the program it doesn't display the result of the sequencial matrix multiply algorithm, so i have no idea if it works or not...Could someone tell me what i am doing wrong?...as i am really really stuck on this problem. thanks.

it doesn't display the result

Try debugging the code to see why it doesn't execute the code that is supposed to display the result. Add lots ofprintln statements to shown the values of the variables that are ued to control the display logic and to show when methods are called.

NOTE: The posted code does not compile without errors. It is missing some import statements.

EDIT: You should add calls to the printStackTrace() method to all the catch blocks so the full text of the error message and the location of the error are displayed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.