fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main() {
  6. int rows, cols;
  7. cin >> rows >> cols;
  8.  
  9. // Input the 2D array
  10. vector<vector<int>> arr(rows, vector<int>(cols));
  11. for(int i = 0; i < rows; i++) {
  12. for(int j = 0; j < cols; j++) {
  13. cin >> arr[i][j];
  14. }
  15. }
  16.  
  17. // Print border elements
  18. cout << "Border elements:" << endl;
  19. for(int i = 0; i < rows; i++) {
  20. for(int j = 0; j < cols; j++) {
  21. if(i == 0 || j == 0 || i == rows - 1 || j == cols - 1) {
  22. cout << arr[i][j] << " ";
  23. } else {
  24. cout << " ";
  25. }
  26. }
  27. cout << endl;
  28. }
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 5264KB
stdin
3 3 
1 2 3 
4 5 6
7 8 9
stdout
Border elements:
1 2 3 
4   6 
7 8 9