Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y | C ++ implementation of the above approach ; Function to print the required matrix ; For 1 * 1 matrix ; Greater number ; Smaller number ; Sets / Resets for alternate filling of the matrix ; Print the matrix ; If end of row is reached ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { cout << y << " STRNEWLINE " ; } else { cout << x << " STRNEWLINE " ; } return ; } a = min ( x , y ) ; b = min ( 2 * x , y ) - a ; bool flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) cout << a << ' ▁ ' ; else cout << b << ' ▁ ' ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; cout << " STRNEWLINE " ; } } int main ( ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; }