text: Construct a matrix with sum equal to the sum of diagonal elements | C ++ program to implement the above approach ; Function to construct matrix with diagonal sum equal to matrix sum ; If diagonal position ; Positive element ; Negative element ; Driver Code
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; void constructmatrix ( int N ) { bool check = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { cout << 1 << " ▁ " ; } else if ( check ) { cout << 2 << " ▁ " ; check = false ; } else { cout << -2 << " ▁ " ; check = true ; } } cout << endl ; } } int main ( ) { int N = 5 ; constructmatrix ( 5 ) ; return 0 ; }