0% found this document useful (0 votes)
3 views4 pages

Practical

The Java program prompts the user to input the dimensions of a matrix and its elements, validating that the dimensions are between 2 and 8. It calculates and displays the sum of the boundary elements of the matrix both before and after sorting the matrix elements in ascending order. The sorted matrix and the corresponding sums of the boundary elements are printed to the console.

Uploaded by

Shirsa Samanta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Practical

The Java program prompts the user to input the dimensions of a matrix and its elements, validating that the dimensions are between 2 and 8. It calculates and displays the sum of the boundary elements of the matrix both before and after sorting the matrix elements in ascending order. The sorted matrix and the corresponding sums of the boundary elements are printed to the console.

Uploaded by

Shirsa Samanta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

*;

class matrixsort

public static void main()

int M,N,i,j;

Scanner sc=new Scanner(System.in);

System.out.println("M= ");

M=sc.nextInt();

System.out.println("N= ");

N=sc.nextInt();

int A[][]=new int [M][N];

if(M<2 || M>8 || N<2 || N>8)

System.out.println("INVALID INPUT" );

else

System.out.println("ENTER THE ELEMENTS OF THE MATRIX ");

for(int i=0;i<M;i++)

for(int j=0;j<N;j++)

A[i][j]=sc.nextInt();

System.out.println("ORIGINAL MATRIX ");

for(int i=0;i<M;i++)

{
for(int j=0;j<N;j++)

System.out.print(A[i][j]+"\t");

System.out.println();

int s=0;

for(int i=0;i<M;i++)

for(int j=0;j<N;j++)

if( i==0|| j==0||i==M-1||j==N-1)

s=s+A[i][j];

System.out.println("SUM OF THE BOUNDARY ELEMENTS(UNSORTED)= "+s);

int tmp=0;

int arr[][]=new int[M*N];

for( int i=0;i<arr.length;i++)

for(int j=0;j<arr.length-1;j++)

arr[tmp]=A[i][j];

tmp++;

}
for(int i=tmp;i>0;i--)

for(int j=tmp-1;i>0;j--)

if(arr[j+1]>arr[j])

int tmp=arr[j+1];

arr[j+1]=arr[j];

arr[j]=tmp;

int tmp=0;

for(int i=0;i<M;i++)

for(int j=0;j<N;j++)

A[i][j]=arr[t];

t++;

System.out.println("SORTED MATRIX ");

for(int i=0;i<M;i++)

for(int j=0;j<N;j++)

System.out.println(A[i][j]+"\t");

}
System.out.println();

int sum=0;

for(i=0;i<M;i++)

for( j=0;j<N;j++)

if(i==0||j==0||i==M-1||j==N-1)

sum=sum+A[i][j];

System.out.println("SUM OF THE BOUNDARY ELEMENTS(SORTED)= "+sum);

You might also like