Monday, January 16, 2023

Sorting boundary elements of a 2 d array display the boundary elements along with sum

 


Using Scanner Class

import java.util.*;
public class Main
{

int x,t,r,c,i,j,n,m;
int a[][],b[];
void show()
{
t=1;
Scanner br=new Scanner(System.in);
c=0;
while(true)
{

System.out.println("enter the number of rows:");
n=br.nextInt();
if(n<2 || n>20)
continue;
System.out.println("enter the number of columns:");
m=br.nextInt();
if(m<2 || m>20)
continue;
else
break;
}

a=new int[n][m];
b=new int[2*(m+n)];
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.println("enter value:");
a[i][j]=br.nextInt();
}
}
System.out.println("\nEntered values are\n");
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println("\n\n");
for(i=0;i< m;i++)
{
b[c++]=a[0][i];
}
for(i=1;i<=n-1;i++)
{
b[c++]=a[i][m-1];
}
for(i=m-2;i >=0;i--)
{
b[c++]=a[n-1][i];
}
for(i=n-2;i >0;i--)
{
b[c++]=a[i][0];
}

bsort();
r=0;
c=-1;
t=0;
for(i=1;i<=m;i++)
{
a[r][++c]=b[t++];
}
for(i=1;i<=n-1;i++)
{
a[++r][c]=b[t++];
}
for(i=1;i<=m-1;i++)
{
a[r][--c]=b[t++];
}
for(i=1;i<=n-2;i++)
{
a[--r][c]=b[t++];
}
System.out.println("\nAfter sorting the matrix\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  System.out.print(" "+a[i][j]);
  }
  System.out.println();
 }
 x=0;
 System.out.println("\nAfter sorting the boundary values\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  if(i==0 || i==n-1 ||  j==0 ||  j==m-1)
{
  System.out.print(" "+a[i][j]);
  x=x+a[i][j];
}
  else
  System.out.print(" ");
  }
  System.out.println();
 }
System.out.println("\nSum of the boundary elements "+x);
}
private void bsort()
{
 int flag;
 for(i=0;i< c;i++)
 {
  flag=0;
  for(j=0;j< c-i-1;j++)
  {
   if(b[j] >b[j+1])
   {
    flag=1;
    t=b[j];
    b[j]=b[j+1];
    b[j+1]=t;
    }
   }
   if(flag==0)
   break;
  }
  }
public static void main(String args[])throws Exception
{
Main ob=new Main();
ob.show();
}
}


Using BufferedReader Class

import java.io.*;
public class Boundary
{
int x,t,r,c,i,j,n,m;
int a[][],b[];
void show()throws IOException
{
t=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
c=0;
while(true)
{
System.out.println("enter the number of rows:");
n=Integer.parseInt(br.readLine());
if(n<2 || n>20)
continue;
System.out.println("enter the number of columns:");
m=Integer.parseInt(br.readLine());
if(m<2 || m>20)
continue;
else
break;
}
a=new int[n][m];
b=new int[2*(m+n)];
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.println("enter value:");
a[i][j]=Integer.parseInt(br.readLine());
}
}
System.out.println("\nEntered values are\n");
for(i=0;i< n;i++)
{
for(j=0;j< m;j++)
{
System.out.print(" "+a[i][j]);
}
System.out.println();
}
System.out.println("\n\n");
for(i=0;i< m;i++)
{
b[c++]=a[0][i];
}
for(i=1;i<=n-1;i++)
{
b[c++]=a[i][m-1];
}
for(i=m-2;i >=0;i--)
{
b[c++]=a[n-1][i];
}
for(i=n-2;i >0;i--)
{
b[c++]=a[i][0];
}
bsort();
r=0;
c=-1;
t=0;
for(i=1;i<=m;i++)
{
a[r][++c]=b[t++];
}
for(i=1;i<=n-1;i++)
{
a[++r][c]=b[t++];
}
for(i=1;i<=m-1;i++)
{
a[r][--c]=b[t++];
}
for(i=1;i<=n-2;i++)
{
a[--r][c]=b[t++];
}
System.out.println("\nAfter sorting the matrix\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  System.out.print(" "+a[i][j]);
  }
  System.out.println();
 }
 x=0;
 System.out.println("\nAfter sorting the boundary values\n");
for(i=0;i< n;i++)
{
 for(j=0;j< m;j++)
 {
  if(i==0 || i==n-1 ||  j==0 ||  j==m-1)
{
  System.out.print(" "+a[i][j]);
  x=x+a[i][j];
}
  else
  System.out.print(" ");
  }
  System.out.println();
 }
System.out.println("\nSum of the boundary elements "+x);
}
private void bsort()
{
 int flag;
 for(i=0;i< c;i++)
 {
  flag=0;
  for(j=0;j< c-i-1;j++)
  {
   if(b[j] >b[j+1])
   {
    flag=1;
    t=b[j];
    b[j]=b[j+1];
    b[j+1]=t;
    }
   }
   if(flag==0)
   break;
  }
  }
public static void main(String args[])throws Exception
{
Boundary ob=new Boundary();
ob.show();
}
}



Sample Input & Output

enter the number of rows:
3
enter the number of columns:
4
enter value:
5
enter value:
6
enter value:
7
enter value:
55
enter value:
4
enter value:
22
enter value:
1
enter value:
34
enter value:
5
enter value:
4
enter value:
34
enter value:
3

Entered values are

 5 6 7 55
 4 22 1 34
 5 4 34 3




After sorting the matrix

 3 4 4 5
 55 22 1 5
 34 34 7 6

After sorting the boundary values

 3 4 4 5
 55   5
 34 34 7 6

Sum of the boundary elements 157

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner