Boundary elements of a Matrix
Printing Boundary Elements of a Matrix.
Examples:
Input : 1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
Output : 1 2 3 4
5 8
1 4
5 6 7 8
The idea is really simple. We traverse given matrix. For every element being traversed, we print it if it is a corner element. If it is not a corner element, then we print space character.
C++
// C++ program to print boundary element of // matrix. #include <bits/stdc++.h> using namespace std; const int MAX = 100; void printBoundary(int a[][MAX], int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0 || j == 0 || i == n - 1 || j == n - 1) cout << a[i][j] << " "; else cout << " " << " "; } cout << "\n"; } } // Driver code int main() { int a[4][MAX] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; printBoundary(a, 4, 4); return 0; } |
chevron_right
filter_none
Java
// JAVA Code for Boundary elements of a Matrix class GFG { public static void printBoundary(int a[][], int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0) System.out.print(a[i][j] + " "); else if (i == m - 1) System.out.print(a[i][j] + " "); else if (j == 0) System.out.print(a[i][j] + " "); else if (j == n - 1) System.out.print(a[i][j] + " "); else System.out.print(" "); } System.out.println(""); } } /* Driver program to test above function */ public static void main(String[] args) { int a[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; printBoundary(a, 4, 4); } } // This code is contributed by Arnav Kr. Mandal. |
chevron_right
filter_none
Python
# Python program to print boundary element # of matrix. MAX = 100 def printBoundary(a, m, n): for i in range(m): for j in range(n): if (i == 0): print a[i][j], elif (i == m-1): print a[i][j], elif (j == 0): print a[i][j], elif (j == n-1): print a[i][j], else: print " ", print # Driver code a = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ] printBoundary(a, 4, 4) # This code is contributed by Sachin Bisht |
chevron_right
filter_none
C#
// C# Code for Boundary // elements of a Matrix using System; class GFG { public static void printBoundary(int[, ] a, int m, int n) { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0) Console.Write(a[i, j] + " "); else if (i == m - 1) Console.Write(a[i, j] + " "); else if (j == 0) Console.Write(a[i, j] + " "); else if (j == n - 1) Console.Write(a[i, j] + " "); else Console.Write(" "); } Console.WriteLine(" "); } } // Driver Code static public void Main() { int[, ] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; printBoundary(a, 4, 4); } } // This code is contributed by ajit |
chevron_right
filter_none
PHP
<?php // PHP program to print // boundary element of // matrix. $MAX = 100; function printBoundary($a, $m, $n) { global $MAX; for ($i = 0; $i < $m; $i++) { for ($j = 0; $j < $n; $j++) { if ($i == 0) echo $a[$i][$j], " "; else if ($i == $m - 1) echo $a[$i][$j], " "; else if ($j == 0) echo $a[$i][$j], " "; else if ($j == $n - 1) echo $a[$i][$j], " "; else echo " ", " "; } echo "\n"; } } // Driver code $a = array(array( 1, 2, 3, 4 ), array( 5, 6, 7, 8 ), array( 1, 2, 3, 4 ), array( 5, 6, 7, 8 )); printBoundary($a, 4, 4); // This code is contributed // by akt_mit ?> |
chevron_right
filter_none
Output:
1 2 3 4 5 8 1 4 5 6 7 8
Finding sum of boundary elements
Examples:
Input : 1 2 3 4
5 6 7 8
1 2 3 4
5 6 7 8
Output : 54
C++
// C++ program to find sum of boundary elements // of matrix. #include <bits/stdc++.h> using namespace std; const int MAX = 100; int getBoundarySum(int a[][MAX], int m, int n) { long long int sum = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0) sum += a[i][j]; else if (i == m - 1) sum += a[i][j]; else if (j == 0) sum += a[i][j]; else if (j == n - 1) sum += a[i][j]; } } return sum; } // Driver code int main() { int a[][MAX] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; long long int sum = getBoundarySum(a, 4, 4); cout << "Sum of boundary elements is " << sum; return 0; } |
chevron_right
filter_none
Java
// JAVA Code for Finding sum of boundary elements class GFG { public static long getBoundarySum(int a[][], int m, int n) { long sum = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0) sum += a[i][j]; else if (i == m - 1) sum += a[i][j]; else if (j == 0) sum += a[i][j]; else if (j == n - 1) sum += a[i][j]; } } return sum; } /* Driver program to test above function */ public static void main(String[] args) { int a[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; long sum = getBoundarySum(a, 4, 4); System.out.println("Sum of boundary elements" + " is " + sum); } } // This code is contributed by Arnav Kr. Mandal. |
chevron_right
filter_none
Python
# Python program to print boundary element # of matrix. MAX = 100 def printBoundary(a, m, n): sum = 0 for i in range(m): for j in range(n): if (i == 0): sum += a[i][j] elif (i == m-1): sum += a[i][j] elif (j == 0): sum += a[i][j] elif (j == n-1): sum += a[i][j] return sum # Driver code a = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ] ] sum = printBoundary(a, 4, 4) print "Sum of boundary elements is", sum # This code is contributed by Sachin Bisht |
chevron_right
filter_none
C#
// C# Code for Finding sum // of boundary elements using System; class GFG { public static long getBoundarySum(int[, ] a, int m, int n) { long sum = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0) sum += a[i, j]; else if (i == m - 1) sum += a[i, j]; else if (j == 0) sum += a[i, j]; else if (j == n - 1) sum += a[i, j]; } } return sum; } // Driver Code static public void Main() { int[, ] a = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 1, 2, 3, 4 }, { 5, 6, 7, 8 } }; long sum = getBoundarySum(a, 4, 4); Console.WriteLine("Sum of boundary" + " elements is " + sum); } } // This code is contributed by ajit |
chevron_right
filter_none
PHP
<?php // PHP program to find // sum of boundary // elements of matrix. function getBoundarySum($a, $m, $n) { $sum = 0; for ($i = 0; $i < $m; $i++) { for ( $j = 0; $j < $n; $j++) { if ($i == 0) $sum += $a[$i][$j]; else if ($i == $m - 1) $sum += $a[$i][$j]; else if ($j == 0) $sum += $a[$i][$j]; else if ($j == $n - 1) $sum += $a[$i][$j]; } } return $sum; } // Driver code $a = array(array(1, 2, 3, 4), array(5, 6, 7, 8), array(1, 2, 3, 4), array(5, 6, 7, 8)); $sum = getBoundarySum($a, 4, 4); echo "Sum of boundary elements is ", $sum; // This code is contributed by ajit ?> |
chevron_right
filter_none
Output:
Sum of boundary elements is 54
This article is contributed by Sarthak Kohli. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Minimum steps to reach any of the boundary edges of a matrix | Set 1
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Program to print elements of a Matrix row-wise skipping alternate elements
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Move matrix elements in given direction and add elements with same value
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Maximum sum of elements from each row in the matrix
- Sum of all even frequency elements in Matrix
- Rotate Matrix Elements
- Sum of all odd frequency elements in a Matrix
- Sum of alternate elements of a N x N matrix
- Interchange elements of first and last rows in matrix
- Common elements in all rows of a given matrix
- Sum of all minimum frequency elements in Matrix
Improved By : jit_t



