Question
Program to sort non-boundary elements of the matrix in ascending order.
Matrix = [5, 2, 9, 8, 7]
[9, 6, 4, 2, 3]
[3, 8, 1, 3, 9]
[1, 2, 3, 4, 5]
SORTED MATRIX
[5, 2, 9, 8, 7]
[9, 1, 2, 3, 3]
[3, 4, 6, 8, 9]
[1, 2, 3, 4, 5]
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
Java
Python
Java
import java.util.Scanner;
public class SortNonBoundaryElementsOfMatrix
{
public static void main(String[] args)
{
int row=0,col=0,i=0,j=0,k=0,temp=0,count=0;
int arr[][],b[];
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER OF ROW OF MATRIX");
row=sc.nextInt();
System.out.println("ENTER THE NUMBER OF COLUMN OF MATRIX");
col=sc.nextInt();
arr=new int[row][col];
System.out.println("ENTER THE ELEMENTS IN MATRIX");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
arr[i][j]=sc.nextInt();
}
}
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
/*counting the number of non-boundary elements*/
count++;
}
}
b=new int[count];
System.out.println("ORIGINAL MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
k=0;
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
b[k]=arr[i][j];
k++;
}
}
/*sorting one-dimensional array*/
for(i=0;i< b.length;i++)
{
for(j=0;j< b.length;j++)
{
if(b[i] < b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
k=0;
for(i=1;i< row-1;i++)
{
for(j=1;j< col-1;j++)
{
arr[i][j]=b[k];
k++;
}
}
System.out.println("SORTED MATRIX:");
for(i=0;i< row;i++)
{
for(j=0;j< col;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Python
import numpy as np
row=int(input("Enter number of rows:"))
columns=int(input("Enter number of columns:"))
matrix=np.empty([row,columns],dtype=np.int)
# Taking input from user
print("Enter elements in matrix:")
for i in range(0,row):
for j in range(0,columns):
print("(",(i+1),",",(j+1),")",":",end="")
matrix[i,j]=int(input())
#printing the matrix
print("Original Matrix:")
for i in range(0, row):
print(matrix[i])
#Finding non-boundary elements and putting them in a list
nonBoundaryElements=[]
for i in range(1,row-1):
for j in range(1,columns-1):
nonBoundaryElements.append(matrix[i,j])
for i in range(0,len(nonBoundaryElements)):
for j in range(0, len(nonBoundaryElements)):
if(nonBoundaryElements[i]< nonBoundaryElements[j]):
temp=nonBoundaryElements[i]
nonBoundaryElements[i]=nonBoundaryElements[j]
nonBoundaryElements[j]=temp
"""
You can also replace code from line 22 to line 27 with nonBoundaryElements.sort()
it will do the same thing.
"""
k=0
for i in range(1,row-1):
for j in range(1,columns-1):
matrix[i,j]=nonBoundaryElements[k]
k=k+1
print("Matrix with sorted non-boundary elements:")
for i in range(0, row):
print(matrix[i])
Coding Store
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale

ICSE QUESTION PAPER WITH SOLUTION(PROGRAMMING ONLY)
Sale

ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale
