Construct a Matrix N x N with first N ^ 2 natural numbers for an input N | C ++ program for the above approach ; Function to print the desired matrix ; Iterate ove all [ 0 , N ] ; If is even ; If row number is even print the row in forward order ; If row number is odd print the row in reversed order ; Driver Code ; Given matrix size ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void UniqueMatrix ( int N ) { int element_value = 1 ; int i = 0 ; while ( i < N ) { if ( i % 2 == 0 ) { for ( int f = element_value ; f < element_value + N ; f ++ ) { cout << f << " ▁ " ; } element_value += N ; } else { for ( int k = element_value + N - 1 ; k > element_value - 1 ; k -- ) { cout << k << " ▁ " ; } element_value += N ; } cout << endl ; i = i + 1 ; } } int main ( ) { int N = 4 ; UniqueMatrix ( N ) ; }