Friday, March 18, 2011

Matrix Operation in Java

Last time, I struggle on how to repeat the whole program. How to run it again if the users want to run it again and exits if want to exit. Now I've found one solution how to solve it but I think there's a lot of solution out there to fixed that problem. And what I did is put the whole program in the "while loop" then condition the increment. That's all!

int x=0;
while(x>=0){//the whole program is here!}
x++;//condition the increment..


Okay..here's what I did. This is the perfect program what our instructor been looking for. But it's too late cause we've already got a grade of 65..lol!

import java.util.*;
public class descretematrix{
    public static void main(String[]args){
        
        System.out.println("\nMATRIX");
        int priloop=0;
        while(priloop>=0){
            int secloop=0;
            while(secloop>=0){            
        Scanner scan=new Scanner(System.in);
            System.out.println("\nMenu:");
            System.out.println("1=Addition!");
            System.out.println("2=Subtraction!");
            System.out.println("3=Multiplication!");
            System.out.println("0=Exit!");
            System.out.print("\nSelect a choice: ");
            int choice = scan.nextInt();
            if(choice==1){ System.out.print("\nADDITION OF MATRIX:");
                    System.out.print("\nEnter row of first table: ");
                    int row1=scan.nextInt();
                    System.out.print("Enter column of first table: ");
                    int col1=scan.nextInt();
                    System.out.print("Enter row of second table: ");
                    int row2=scan.nextInt();
                    System.out.print("Enter column of second table: ");
                    int col2=scan.nextInt();
                    
                    if(row1==row2 && col1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}
                                        
                    System.out.println("\nAdded Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print((mat1[i][j]+mat2[i][j])+" ");
                    }System.out.println();}
                    }                    
                    else{
                    System.out.println("\nSorry! The Matrix cannot be Add!");
                    System.out.println("Please enter the same size of table!");
                    }break;}
                    
            else if(choice==2){ System.out.print("\nSUBTRACTION OF MATRIX:");
                    System.out.print("\nEnter row of first table: ");
                    int row1=scan.nextInt();
                    System.out.print("Enter column of first table: ");
                    int col1=scan.nextInt();
                    System.out.print("Enter row of second table: ");
                    int row2=scan.nextInt();
                    System.out.print("Enter column of second table: ");
                    int col2=scan.nextInt();
                    
                    if(row1==row2 && col1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}
                                        
                    System.out.println("\nSubtracted Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print((mat1[i][j]-mat2[i][j])+" ");
                    }System.out.println();}
                    }                    
                    else{
                    System.out.println("\nSorry! The Matrix cannot be Subtract!");
                    System.out.println("Please enter the same size of table!");
                    }break;}                        
            
            else if(choice==3){ System.out.print("\nMULTIPLICATION OF MATRIX:");
                    System.out.print("\nEnter row of first table: ");
                    int row1=scan.nextInt();
                    System.out.print("Enter column of first table: ");
                    int col1=scan.nextInt();
                    System.out.print("Enter row of second table: ");
                    int row2=scan.nextInt();
                    System.out.print("Enter column of second table: ");
                    int col2=scan.nextInt();
                    
                    if(row1==row2 && col1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                    int mat3[][]=new int[100][100];                    
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}                    
                    
                    for(int i=0;i<=row1;i++){    
                    for(int j=0;j<=col1;j++){
                    for(int k=0;k<=row1;k++){
                    mat3[i][j] += mat1[i][k] * mat2[k][j];}}}                    
                                                    
                    System.out.println("\nMultiplied Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat3[i][j]+" ");}
                    System.out.println();}
                    }        
                        
                    else if(row1==col2){
                    int mat1[][]=new int[100][100];
                    int mat2[][]=new int[100][100];
                    int mat3[][]=new int[100][100];                    
                System.out.println();
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of first table = ");
                    mat1[i][j]=scan.nextInt();}}
                    
                    System.out.println();
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print("Enter element "+(i)+":"+(j)+" of second table = ");
                    mat2[i][j]=scan.nextInt();}}                    
                    
                    System.out.println("\nTable 1");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col1;j++){
                    System.out.print(mat1[i][j]+" ");
                    }System.out.println();}
                    
                    System.out.println("\nTable 2");                    
                    for(int i=0;i<row2;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat2[i][j]+" ");
                    }System.out.println();}                    
                    
                    for(int i=0;i<row1;i++){    
                    for(int j=0;j<col1;j++){
                    for(int k=0;k<=row1;k++){
                    mat3[i][j] += mat1[i][k] * mat2[k][j];}}}                    
                                                    
                    System.out.println("\nMultiplied Matrix");
                    for(int i=0;i<row1;i++){
                    for(int j=0;j<col2;j++){
                    System.out.print(mat3[i][j]+" ");}
                    System.out.println();}
                    }                                
                    else{
                    System.out.println("\nSorry! The Matrix cannot be Multiply!");
                    System.out.println("Please enter the same size of table!");
                    System.out.println("Or enter same size of row 1 and column 2!");
                    }break;}
                                                
            else if(choice==0){System.exit(0);}
            else{
                System.out.println("\nPlease Select from 1-4 only!\n");
                secloop++;}            
            }    
            
            int triloop=0;
            while(triloop>=0){
        Scanner scaned=new Scanner(System.in);
            System.out.println("\nStart Again?");
            System.out.println("1=start again!");
            System.out.println("0=exit!");
            System.out.print("\nSelect a Choice: ");
            int choice2=scaned.nextInt();
            if(choice2==1){break;}
            else if(choice2==0){System.exit(0);}
            else {
                System.out.print("\nPlease Select 1 and 0 only!");
                triloop++;}}        
        priloop++;}        
    }
}

1 comment:

  1. Good Job Genioler... May be your program might be helpful to other...
    Keep it Up....

    ReplyDelete