The Artima Developer Community
Sponsored Link

Java Answers Forum
Help me please.. [Matrix]

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
fgh jkl

Posts: 1
Nickname: sticky1042
Registered: Apr, 2006

Help me please.. [Matrix] Posted: Apr 24, 2006 5:43 AM
Reply to this message Reply
Advertisement
Hye.. I am very new in Java. My question is how to print an array in matrix form. Below is the code for my program:
import java.io.*;
 
public class matrik {
    //main() : application entry point
    public static void main(String[] args) throws IOException {
 
        // create 2D array of dim and initialised
	int[][] dim = new int[2][2];
	boolean plusminus = false;
        boolean multiple = false;
        
	//notes: dim(0,0) = height matrix A  dim(0,1) = width matrix A
	//       dim(1,0) = height matrix B  dim(1,1) = width matrix B
 
        //set input stream
        BufferedReader stdin = 
            new BufferedReader(new InputStreamReader(System.in));
 
	//MATRIX A
 
	//ask input(height,width and element(s) for Matrix A
        System.out.println("Enter the height of matrix A : ");
        dim[0][0] = Integer.parseInt(stdin.readLine());
        System.out.println("Enter the width of matrix A : ");
        dim[0][1] = Integer.parseInt(stdin.readLine());
 
        //create matrix A || all element(s) is zero
        int[][] A = new int[dim[0][0]][dim[0][1]];
 
 
        //ask for the element(s)
        for (int i=0 ; i < A.length ; i++)
            for  (int j=0 ; j < A[i].length ; j++)
            {
              System.out.println("Enter element A("+(i+1)+","+(j+1)+") : ");
              A[i][j] = Integer.parseInt(stdin.readLine());
            }
 
        //MATRIX B
 
        //ask input(height,width and element(s) for Matrix B
        System.out.println("Enter the height of matrix B : ");
        dim[1][0] = Integer.parseInt(stdin.readLine());
        System.out.println("Enter the width of matrix B : ");
        dim[1][1] = Integer.parseInt(stdin.readLine()) ;
        
        //create matrix B || all element(s) is zero
        int[][] B = new int[dim[1][0]][dim[1][1]];
 
 
        //ask for the element(s)
        for (int i=0 ; i < B.length ; i++)
            for  (int j=0 ; j < B[i].length ; j++)
            {
              System.out.println("Enter element B("+(i+1)+","+(j+1)+") : ");
              B[i][j] = Integer.parseInt(stdin.readLine());
            }
 
        //Check if the matrices can perform addtion and substraction
        if ( dim[0][0] == dim[1][0] && dim[0][1] == dim[1][1] )
           { plusminus = true; }
 
        //Check if the matrices can perform multiplication
        if ( dim[0][1] == dim[1][0] )
           { multiple = true; }
        
        //Addition
        
        if (plusminus == true)
        {
        //create matrix Cplus || all element(s) is zero
        int[][] Cplus = new int[dim[0][0]][dim[0][1]];
        //method to perform addition [(A + B) = Cplus]
          for (int i=0 ; i < Cplus.length ; i++)
           {   for  (int j=0 ; j < Cplus[i].length ; j++)
               Cplus[i][j] = A[i][j] - B[i][j];  }
        }
 
        //Substraction
 
        if (plusminus == true)
        {
        //create matrix Cminus || all element(s) is zero
        int[][] Cminus = new int[dim[0][0]][dim[0][1]];
        //method to perform subtration [(A - B) = Cminus ]
          for (int i=0 ; i < Cminus.length ; i++)
           {   for  (int j=0 ; j < Cminus[i].length ; j++)
               Cminus[i][j] = A[i][j] - B[i][j];  }
        }
 
        //Mulstiplication
        
        if (multiple == true)
        {
        //create matrix Cmutiply || all element(s) is zero
        int[][] Cmultiply = new int[dim[0][0]][dim[1][1]];
        //method to perform multiplication [(A*B) = Cmultilpy]
          for (int i=0 ; i < Cmultiply.length ; i++)
              for (int j=0 ; j < Cmultiply[i].length ; j++)
                  for (int k=0 ; k < dim[0][1] ; k++)
                  {
                    Cmultiply[i][j] = Cmultiply[i][j] + A[i][k]*B[k][j];
                  }
        }
        
        //Printing the results
        
        //Display Matrix A
        //********Here is my problem... ****
        //Problem arise if the element is more than one       digit
        System.out.println("Matrix A");
        for (int i=0 ; i < A.length ; i++)
        {     System.out.println();
            for  (int j=0 ; j < A[i].length ; j++)
                 {System.out.print(A[i][j]+" ");
 
 
                 }
        }
    }
}


Thanx.

Topic: How to convert this application into Applet, Thanks!! Previous Topic   Next Topic Topic: Rreading a file and creating a excel sheet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use