import java.util.*;
import java.io.*;
class Matrix3
{
public static void main(String[] args) throws IOException
{
int ch;
for(;;)
{
System.out.println("1.MatAdd");
System.out.println("2.MatMult");
System.out.println("3.Exit");
System.out.println("Enter UR choice:");
DataInputStream in=new DataInputStream(System.in);
ch=Integer.parseInt(in.readLine());
MatAddMulti ob=new MatAddMulti();
switch(ch)
{
case 1: ob.MatAdd();
break;
case 2: ob.MatMulti();
break;
default:System.exit(0);
}
}
}
}
class MatAddMulti
{
int m,n,p,q;
int a[][]=new int[10][10];
int b[][]=new int[10][10];
int c[][]=new int[10][10];
DataInputStream in=new DataInputStream(System.in);
public void Order() throws IOException
{
System.out.println("Enter the order of I matrix");
m=Integer.parseInt(in.readLine());
n=Integer.parseInt(in.readLine());
System.out.println("Enter the order of II matrix");
p=Integer.parseInt(in.readLine());
q=Integer.parseInt(in.readLine());
}
public void Read() throws IOException
{
System.out.println("Enter the elements for the first matrix:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=Integer.parseInt(in.readLine());
System.out.println("Enter the elements for the second matrix:");
for(int i=0;i<p;i++)
for(int j=0;j<q;j++)
b[i][j]=Integer.parseInt(in.readLine());
}
public void Disp(int r,int s,int t[][])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<s;j++)
{
System.out.print(t[i][j]+" ");
}
System.out.println();
}
}
public void MatAdd() throws IOException
{
Order();
if(m==p && n==q)
{
Read();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
System.out.println("The First Matrix:");
Disp(m,n,a);
System.out.println("The Second Matrix:");
Disp(p,q,b);
System.out.println("Matrix sum:");
Disp(m,n,c);
}
else
{
System.out.println("Addition is not possible!");
}
}
public void MatMulti() throws IOException
{
Order();
if(n==p)
{
Read();
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
{
c[i][j]=0;
for(int k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("The First Matrix:");
Disp(m,n,a);
System.out.println("The Second Matrix:");
Disp(p,q,b);
System.out.println("Matrix Multiplication:");
Disp(m,q,c);
}
else
{
System.out.println("Multiplication is not possible!");
}
}
}
import java.io.*;
class Matrix3
{
public static void main(String[] args) throws IOException
{
int ch;
for(;;)
{
System.out.println("1.MatAdd");
System.out.println("2.MatMult");
System.out.println("3.Exit");
System.out.println("Enter UR choice:");
DataInputStream in=new DataInputStream(System.in);
ch=Integer.parseInt(in.readLine());
MatAddMulti ob=new MatAddMulti();
switch(ch)
{
case 1: ob.MatAdd();
break;
case 2: ob.MatMulti();
break;
default:System.exit(0);
}
}
}
}
class MatAddMulti
{
int m,n,p,q;
int a[][]=new int[10][10];
int b[][]=new int[10][10];
int c[][]=new int[10][10];
DataInputStream in=new DataInputStream(System.in);
public void Order() throws IOException
{
System.out.println("Enter the order of I matrix");
m=Integer.parseInt(in.readLine());
n=Integer.parseInt(in.readLine());
System.out.println("Enter the order of II matrix");
p=Integer.parseInt(in.readLine());
q=Integer.parseInt(in.readLine());
}
public void Read() throws IOException
{
System.out.println("Enter the elements for the first matrix:");
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
a[i][j]=Integer.parseInt(in.readLine());
System.out.println("Enter the elements for the second matrix:");
for(int i=0;i<p;i++)
for(int j=0;j<q;j++)
b[i][j]=Integer.parseInt(in.readLine());
}
public void Disp(int r,int s,int t[][])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<s;j++)
{
System.out.print(t[i][j]+" ");
}
System.out.println();
}
}
public void MatAdd() throws IOException
{
Order();
if(m==p && n==q)
{
Read();
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
System.out.println("The First Matrix:");
Disp(m,n,a);
System.out.println("The Second Matrix:");
Disp(p,q,b);
System.out.println("Matrix sum:");
Disp(m,n,c);
}
else
{
System.out.println("Addition is not possible!");
}
}
public void MatMulti() throws IOException
{
Order();
if(n==p)
{
Read();
for(int i=0;i<m;i++)
{
for(int j=0;j<q;j++)
{
c[i][j]=0;
for(int k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
System.out.println("The First Matrix:");
Disp(m,n,a);
System.out.println("The Second Matrix:");
Disp(p,q,b);
System.out.println("Matrix Multiplication:");
Disp(m,q,c);
}
else
{
System.out.println("Multiplication is not possible!");
}
}
}
No comments :
Post a Comment