import java.io.*;
public class boundary
{
int a[][],b[][],c[],x,y,sum=0,index=0;
boundary(int m,int n)
{
a=new int[m][n];
b=new int[m][n];
c=new int[m*n];
x=m;
y=n;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=0;
b[i][j]=0;
}
}
}
public void input()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print("Enter the element:");
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println();
}
public void original_display()
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
System.out.print(a[i][j]+"\t");
}
System.out.println();
}
System.out.println();
}
public void extract()
{
for(int i=0;i<y;i++)
{
c[index]=a[0][i];
index++;
}
for(int i=1;i<x;i++)
{
c[index]=a[i][y-1];
index++;
}
for(int i=(y-2);i>=1;i--)
{
c[index]=a[x-1][i];
index++;
}
for(int i=x-1;i>=1;i--)
{
c[index]=a[i][0];
index++;
}
for(int i=0;i<index;i++)
{
sum=sum+c[i];
}
}
public void sort()
{
for(int i=0;i<index;i++)
{
for(int j=0;j<index-1;j++)
{
if(c[j]>c[j+1])
{
int temp=c[j];
c[j]=c[j+1];
c[j+1]=temp;
}
}
}
}
public void modify_part1()
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
a[i][j]=-1;
}
}
}
public void modify_part2()
{
int index=0;
for(int i=0;i<y;i++)
{
a[0][i]=c[index];
index++;
}
for(int i=1;i<x;i++)
{
a[i][y-1]=c[index];
index++;
}
for(int i=(y-2);i>=1;i--)
{
a[x-1][i]=c[index];
index++;
}
for(int i=x-1;i>=1;i--)
{
a[i][0]=c[index];
index++;
}
}
public void modify_display()
{
for(int i=0;i<x;i++)
{
for(int j=0;j<y;j++)
{
if(a[i][j]!=-1)
{
System.out.print(a[i][j]+"\t");
}
else if(a[i][j]==-1)
{
System.out.print("\t");
}
}
System.out.println();
}
System.out.println();
}
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the value of m:");
int m=Integer.parseInt(br.readLine());
System.out.print("Enter the value of n:");
int n=Integer.parseInt(br.readLine());
boundary obj=new boundary(m,n);
obj.input();
System.out.println("ORIGINAL MATRIX:-");
obj.original_display();
obj.extract();
System.out.println("REARRANGED MATRIX:-");
obj.sort();
obj.modify_part2();
obj.modify_display();
System.out.println("BOUNDARY ELEMENTS:-");
obj.sort();
obj.modify_part1();
obj.modify_part2();
obj.modify_display();
System.out.print("The sum is ="+obj.sum);
}
}
0 comments:
Post a Comment