Boundary Elements of 2D Array Program in Java and Python
Boundary elements of 2D array program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input a 2-D array and print only the boundary elements of the array.
Algorithm:
Step 1: Start.
Step 2: Accept the number of rows m and columns n.
Step 3: Declare matrix A[m][n].
Step 4: Input all matrix values using nested loops.
Step 5: For each position A[i][j], test whether it lies on the boundary.
Step 6: A position is on the boundary if i is 0, i is m - 1, j is 0 or j is n - 1.
Step 7: If the position is on the boundary, display A[i][j].
Step 8: Otherwise display blank spacing so the matrix shape is preserved.
Step 9: Move to the next line after every row.
Step 10: Stop.
Explanation:
Boundary elements are the elements present on the outer border of a 2-D array. These include all elements in the first row, last row, first column and last column.
The nested loops visit every position of the matrix using row index i and column index j. For each position, the program checks whether it lies on any one of the four borders.
The condition i == 0 checks the first row, i == m - 1 checks the last row, j == 0 checks the first column, and j == n - 1 checks the last column.
If a position satisfies any of these conditions, its value is printed. Otherwise, blank space is printed so that the boundary shape of the matrix remains visible in the output.
The boundary of a matrix consists of the first row, last row, first column and last column. The inner elements are not part of the boundary. The program uses row and column indexes to decide whether an element lies on an edge. A position is on the boundary if its row is 0, its row is the last row, its column is 0, or its column is the last column. This condition allows the program to print only edge elements while leaving spaces or blanks for inner positions if matrix shape is preserved.
Java Program:
/**
* The class Boundary_Element accesses and prints the boundary elements of a 2D array
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Boundary_Element
{
public static void main(String args[])
{
int i,j,m,n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the no. of rows: "); //Inputting the number of rows
m=sc.nextInt();
System.out.print("Enter the no. of columns: "); //Inputting the number of columns
n=sc.nextInt();
int A[][]=new int[m][n]; //Creating the array
/* Inputting the array */
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
System.out.print("Enter the elements: ");
A[i][j]=sc.nextInt();
}
}
System.out.println("The Boundary Elements are:");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==0 || j==0 || i == m-1 || j == n-1) //condition for accessing boundary elements
System.out.print(A[i][j]+"\t");
else
System.out.print(" \t");
}
System.out.println();
}
}
}Equivalent Python Program:
# Read the matrix or array size and store the values for indexed processing.
# Nested loops are used because each row/column or array position must be checked.
# Print the processed array or matrix in the required output format.
m = int(input("Enter the number of rows: "))
n = int(input("Enter the number of columns: "))
A = []
print("Enter the elements:")
for i in range(m):
row = []
for j in range(n):
row.append(int(input()))
A.append(row)
print("Boundary elements:")
for i in range(m):
for j in range(n):
if i == 0 or i == m - 1 or j == 0 or j == n - 1:
print(A[i][j], end=" ")
else:
print("", end=" ")
print()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.