import java.util.*;
class Addition
{
int ar[][],col,row;
public Addition(int r,int c)
{
row=r;
col=c;
ar=new int [row][col];
}
public void accept()
{
int i,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.println("Enter the elemets at the position " + (i+1) + " " + (j+1));
ar[i][j]=sc.nextInt();
}
}
}
public void display()
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
Formatter fmt=new Formatter();
fmt.format("%4d",ar[i][j]);
System.out.print(fmt);
}
System.out.println();
}
}
public void add(Addition ob1,Addition ob2)
{
int i,j,k;
i=j=k=0;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
ar[i][j]=ob1.ar[i][j] + ob2.ar[i][j];
}
}
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the row ");
int r=sc.nextInt();
System.out.println("Enter the column ");
int c=sc.nextInt();
Addition ob1=new Addition(r,c);
Addition ob2=new Addition(r,c);
Addition ob3=new Addition(r,c);
System.out.println("Enter the values for the first matrix");
ob1.accept();
System.out.println("Enter the values for the second matrix");
ob2.accept();
System.out.println("\n First matrix");
ob1.display();
System.out.println("\n Second matrix");
ob2.display();
ob3.add(ob1,ob2);
System.out.println("Final array after array addition");
ob3.display();
}
}
No comments:
Post a Comment
Convey your thoughts to authors.