इस लेख में, हम समझेंगे कि मैट्रिक्स के बाउंड्री एलिमेंट्स कैसे प्रिंट किए जाते हैं। मैट्रिक्स पंक्तियों और स्तंभों में एलिमेंट्स का प्रतिनिधित्व है। बाउंड्री एलिमेंट्स वे एलिमेंट्स हैं जो चारों दिशाओं में एलिमेंट्स से घिरे नहीं होते। उदाहरण के लिए, पहली पंक्ति, पहला स्तंभ, अंतिम पंक्ति और अंतिम स्तंभ के एलिमेंट्स।
उदाहरण
मान लीजिए हमारा इनपुट है:
The input matrix: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5
वांछित आउटपुट होगा:
The border elements of the matrix is: 9 8 9 8 8 7 7 6 6 5 6 5
एल्गोरिदम
चरण 1 - शुरू करें चरण 2 - एक इंटीजर मैट्रिक्स घोषित करें: input_matrix, BoundaryElements क्लास का एक ऑब्जेक्ट: border_values चरण 3 - मान परिभाषित करें चरण 4 - दो फॉर-लूप्स का उपयोग करके मैट्रिक्स के प्रत्येक एलिमेंट पर पुनरावृति करें और बूलियन OR कंडीशन का उपयोग करके जांचें कि क्या एलिमेंट बाउंड्री एलिमेंट है चरण 5 - बाउंड्री एलिमेंट्स प्रदर्शित करें चरण 6 - रुकें
उदाहरण 1: यूजर इनपुट आधारित
यहां, इनपुट यूजर द्वारा प्रॉम्प्ट के आधार पर दर्ज किया जाता है।
public class BoundaryElements {
public static void main(String[] args) {
int input_matrix[][] = new int[][] {
{ 9, 8, 9, 8 },
{ 8, 7, 8, 7 },
{ 7, 6, 7, 6 },
{ 6, 5, 6, 5 }
};
System.out.println("मैट्रिक्स परिभाषित है: ");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
System.out.print(input_matrix[x][y] + " ");
}
System.out.println();
}
BoundaryElements border_values = new BoundaryElements();
System.out.println("मैट्रिक्स के बॉर्डर एलिमेंट्स हैं:");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
if (x == 0 || y == 0 || x == input_matrix.length - 1 || y == input_matrix[x].length - 1) {
System.out.print(input_matrix[x][y] + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
आउटपुट
मैट्रिक्स परिभाषित है: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5 मैट्रिक्स के बॉर्डर एलिमेंट्स हैं: 9 8 9 8 8 7 7 6 6 5 6 5
उदाहरण 2: ऑब्जेक्ट-ओरिएंटेड दृष्टिकोण
यहां, इंटीजर पहले से परिभाषित है, और इसका मान एक्सेस करके कंसोल पर प्रदर्शित किया जाता है।
public class BoundryElements {
public void Boundary_Elements(int input_matrix[][]) {
System.out.println("मैट्रिक्स परिभाषित है: ");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
System.out.print(input_matrix[x][y] + " ");
}
System.out.println();
}
System.out.println("मैट्रिक्स के बॉर्डर एलिमेंट्स हैं:");
for (int x = 0; x < input_matrix.length; x++) {
for (int y = 0; y < input_matrix[x].length; y++) {
if (x == 0 || y == 0 || x == input_matrix.length - 1 || y == input_matrix[x].length - 1) {
System.out.print(input_matrix[x][y] + " ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
public static void main(String[] args) {
int input_matrix[][] = new int[][] {
{ 9, 8, 9, 8 },
{ 8, 7, 8, 7 },
{ 7, 6, 7, 6 },
{ 6, 5, 6, 5 }
};
BoundryElements border_values = new BoundryElements();
border_values.Boundary_Elements(input_matrix);
}
}
आउटपुट
मैट्रिक्स परिभाषित है: 9 8 9 8 8 7 8 7 7 6 7 6 6 5 6 5 मैट्रिक्स के बॉर्डर एलिमेंट्स हैं: 9 8 9 8 8 7 7 6 6 5 6 5