Friday, August 26, 2011

Two Matrixs summation in java


//Summation of two matrixs

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TwoMatrixsSummation
{
public static void main(String args[]) throws IOException
{
int matrix_one_row,matrix_one_column,matrix_two_row,matrix_two_column;
int matrix_one[][]=new int[100][100];
int matrix_two[][]=new int[100][100];
int result[][]=new int[100][100];
int r,c;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//Get First Matrix row and column number.
System.out.println("How many row in first Matrix?");
matrix_one_row=Integer.valueOf(br.readLine()).intValue();
System.out.println("How many column in first Matrix?");
matrix_one_column=Integer.valueOf(br.readLine()).intValue();

//Get Second Matrix row and column number.
System.out.println("How many row in second Matrix?");
matrix_two_row=Integer.valueOf(br.readLine()).intValue();
System.out.println("How many column in second Matrix?");
matrix_two_column=Integer.valueOf(br.readLine()).intValue();

//Condition check.
if((matrix_one_row==matrix_two_row)&&(matrix_one_column==matrix_two_column))
{
//Get First Matrix value.
System.out.println("Enter First Matrix value:");
for(r=1;r<=matrix_one_row;r++)
{
for(c=1;c<=matrix_one_column;c++)
{
System.out.print("Enter MatrixOne["+r+"]["+c+"] value = ");
matrix_one[r][c]=Integer.valueOf(br.readLine()).intValue();
}
}

//Get Second Matrix value.
System.out.println("Enter Second Matrix value:");
for(r=1;r<=matrix_two_row;r++)
{
for(c=1;c<=matrix_two_column;c++)
{
System.out.print("Enter MatrixTwo["+r+"]["+c+"] value = ");
matrix_two[r][c]=Integer.valueOf(br.readLine()).intValue();
}
}


//Print First Matrix.
for(r=1;r<=matrix_one_row;r++)
{
for(c=1;c<=matrix_one_column;c++)
{
System.out.print(matrix_one[r][c]+" ");
}
System.out.println("\n");
}

//Print a "+" sign.
System.out.println(" |+| ");

//Print Second Matrix.
for(r=1;r<=matrix_two_row;r++)
{
for(c=1;c<=matrix_two_column;c++)
{
System.out.print(matrix_two[r][c]+" ");
}
System.out.println("\n");
}

//Print a "=" sign.
System.out.println(" = ");

//Summation of two matrixs
for(r=1;r<=matrix_one_row;r++)
{
for(c=1;c<=matrix_one_column;c++)
{
result[r][c]=matrix_one[r][c]+matrix_two[r][c];
}
}

//Print summation result of two Matrixs.
for(r=1;r<=matrix_two_row;r++)
{
for(c=1;c<=matrix_two_column;c++)
{
System.out.print(result[r][c]+" ");
}
System.out.println("\n");
}
}
else
{
System.out.println("Summation is not possible as two matrixs have different order of rows and columns.");
}
}
}


Output1 =>

How many row in first Matrix?
2
How many column in first Matrix?
2
How many row in second Matrix?
2
How many column in second Matrix?
2
Enter First Matrix value:
Enter MatrixOne[1][1] value = 5
Enter MatrixOne[1][2] value = 5
Enter MatrixOne[2][1] value = 5
Enter MatrixOne[2][2] value = 5
Enter Second Matrix value:
Enter MatrixTwo[1][1] value = 6
Enter MatrixTwo[1][2] value = 4
Enter MatrixTwo[2][1] value = 3
Enter MatrixTwo[2][2] value = 9
5 5

5 5

|+|
6 4

3 9

=
11 9

8 14


Output2 =>

How many row in first Matrix?
2
How many column in first Matrix?
2
How many row in second Matrix?
3
How many column in second Matrix?
3
Summation is not possible as two matrixs have different order of rows and columns.

No comments:

Post a Comment