Sorting boundary elements of a matrix | C ++ program for the above approach ; Appending border elements ; Sorting the list ; Printing first row with first N elements from A ; Printing N - 2 rows ; Print elements from last ; Print middle elements from original matrix ; Print elements from front ; Printing last row ; Driver Code ; Dimensions of a Matrix ; Given Matrix ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( int grid [ ] [ 5 ] , int m , int n ) { vector < int > A ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == n - 1 || ( i == m - 1 ) j == 0 i == 0 ) A . push_back ( grid [ i ] [ j ] ) ; } } sort ( A . begin ( ) , A . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) cout << A [ i ] << " ▁ " ; cout << endl ; for ( int i = 0 ; i < m - 2 ; i ++ ) { cout << A [ A . size ( ) - i - 1 ] << " ▁ " ; for ( int j = 1 ; j < n - 1 ; j ++ ) cout << grid [ i + 1 ] [ j ] << " ▁ " ; cout << ( A [ n + i ] ) << endl ; } reverse ( A . begin ( ) + n + m - 2 , A . begin ( ) + n + m + n - 2 ) ; for ( int i = n + m - 2 ; i < n + m - 2 + n ; i ++ ) cout << A [ i ] << " ▁ " ; [ n + m - 2 : n + m - 2 + n ] << endl ; } int main ( ) { int m = 4 , n = 5 ; int grid [ ] [ 5 ] = { { 1 , 2 , 3 , 4 , 0 } , { 1 , 1 , 1 , 1 , 2 } , { 1 , 2 , 2 , 2 , 4 } , { 1 , 9 , 3 , 1 , 7 } } ; printMatrix ( grid , m , n ) ; return 0 ; }