# C++ Program to Print Boundary Elements of a Matrix

15/06/2024

To print the boundary elements of a matrix, we need to identify and print elements that lie on the outermost edges of the matrix. These elements include the elements in the first and last rows, and the elements in the first and last columns, excluding the corners already included in the first and last rows.

Problem Statement

Given a matrix, print its boundary elements. The boundary elements include:

• All elements in the first row.
• All elements in the last row.
• All elements in the first column (excluding the first and last elements if already included).
• All elements in the last column (excluding the first and last elements if already included).

Approach

1. First Row: Print all elements in the first row.
2. Last Column: Print all elements in the last column, excluding the first and last elements.
3. Last Row: Print all elements in the last row, in reverse order, excluding the last element (already printed).
4. First Column: Print all elements in the first column, in reverse order, excluding the first and last elements.

Example

For a 4×4 matrix:

1  2  3  4

5  6  7  8

9  10 11 12

13 14 15 16

The boundary elements are:

1 2 3 4 8 12 16 15 14 13 9 5

C++ Code Implementation

Here is a C++ program to print the boundary elements of a matrix:

#include <iostream>

#include <vector>

using namespace std;

// Function to print the boundary elements of a matrix

void printBoundaryElements(const vector<vector<int>>& matrix) {

int rows = matrix.size();

int cols = matrix[0].size();

if (rows == 1) {

// Special case: single row matrix

for (int i = 0; i < cols; ++i) {

cout << matrix[0][i] << ” “;

}

} else if (cols == 1) {

// Special case: single column matrix

for (int i = 0; i < rows; ++i) {

cout << matrix[i][0] << ” “;

}

} else {

// Print the first row

for (int i = 0; i < cols; ++i) {

cout << matrix[0][i] << ” “;

}

// Print the last column

for (int i = 1; i < rows – 1; ++i) {

cout << matrix[i][cols – 1] << ” “;

}

// Print the last row

for (int i = cols – 1; i >= 0; –i) {

cout << matrix[rows – 1][i] << ” “;

}

// Print the first column

for (int i = rows – 2; i > 0; –i) {

cout << matrix[i][0] << ” “;

}

}

cout << endl;

}

// Function to print a matrix

void printMatrix(const vector<vector<int>>& matrix) {

for (const auto& row : matrix) {

for (const auto& elem : row) {

cout << elem << ” “;

}

cout << endl;

}

}

int main() {

// Initializing a 4×4 matrix

vector<vector<int>> matrix = {

{1, 2, 3, 4},

{5, 6, 7, 8},

{9, 10, 11, 12},

{13, 14, 15, 16}

};

cout << “Matrix:” << endl;

printMatrix(matrix);

// Printing the boundary elements of the matrix

cout << “Boundary Elements:” << endl;

printBoundaryElements(matrix);

return 0;

}

Explanation

1. Function printBoundaryElements:

• Parameters: Takes a 2D vector (matrix).
• Single Row Matrix: Handles the special case where the matrix has only one row by printing all elements in that row.
• Single Column Matrix: Handles the special case where the matrix has only one column by printing all elements in that column.
• General Case:
• Prints all elements in the first row.
• Prints all elements in the last column, except the first and last elements.
• Prints all elements in the last row in reverse order, except the last element.
• Prints all elements in the first column in reverse order, except the first and last elements.
1. Function printMatrix:

• Prints the matrix in a formatted manner for better visualization.
2. Main Function:

• Initializes a sample 4×4 matrix.
• Prints the matrix.
• Calls the printBoundaryElements function to print the boundary elements.
• Outputs the boundary elements of the matrix.