Saturday, April 5, 2014

Java Program to Implement crammers rule


import java.util.Scanner;

public class CramerBackEnd {
    public  float calcdeterminant(float temp2[][] ) {  // returns the value of the calculated determinant
        float diter1= temp2[0][0]*( (temp2[1][1]*temp2[2][2] )-(temp2[2][1]*temp2[1][2]) );
        float diter2= temp2[0][1]*( (temp2[1][0]*temp2[2][2] )-(temp2[2][0]*temp2[1][2]) );
        float diter3= temp2[0][2]*( (temp2[1][0]*temp2[2][1] )-(temp2[2][0]*temp2[1][1]) );
        float result = diter1-diter2+diter3;
        return result;
    }   
}
   
public class MainCramers {
    public static void main(String args[]){
       Scanner scan = new Scanner(System.in)  ;
       CramerBackEnd process = new CramerBackEnd();
      
        float matrix[][] =    new float[5][5];
        float temp1[][]=      new float [5][5];
        float temp2[][]=      new float [5][5];
      
        System.out.println("plz give the value of the 3x4 matrix:");
        for(int i =0;i<3;i++){ //  loop i begins
            for(int j=0;j<4;j++) { // loop j begins
              
        System.out.println(" the matrix"+(i+1)+ (j+1) );
              
        matrix[i][j]= scan.nextFloat();
                temp1[i][j]= matrix[i][j];
                temp2[i][j]= matrix[i][j];
              
             } //  loop j ends
        }// loop i  ends
       
       matrix[0][0]= matrix[0][3]; matrix[1][0]= matrix[1][3]; matrix[2][0]= matrix[2][3];  //
       temp1[0][1]= matrix[0][3]; temp1[1][1]= matrix[1][3]; temp1[2][1]= matrix[2][3];  //
       temp2[0][2]= matrix[0][3]; temp2[1][2]= matrix[1][3]; temp2[2][2]= matrix[2][3];  //
      
       float determinant = process.calcdeterminant(matrix);
      
        System.out.println("the determinant:"+determinant);
     
       System.out.printf("The value of X1:");
       float var1 = process.calcdeterminant(matrix)/determinant;
       System.out.printf("%.5f\n",var1);
      
       System.out.printf("The value of X1:");
       float var2 = process.calcdeterminant(temp1)/determinant;
       System.out.printf("%.5f\n",var2);
      
       System.out.printf("The value of X1:");
       float var3 = process.calcdeterminant(temp2)/determinant;
       System.out.printf("%.5f\n",var3);
      
    }
}

No comments:

Post a Comment