C++ Exercises: Produce a square matrix with 0's down the main diagonal
69. Square Matrix with Diagonal Zero and Incrementing Off-Diagonals
Write a program in C++ to produce a square matrix with 0's down the main diagonal, 1's in the 
entries just above and below the main diagonal, 2's above and below that, etc.
 	 0     1     2     3     4
     1     0     1     2     3
     2     1     0     1     2
     3     2     1     0     1
     4     3     2     1     0
Visual Presentation:

Sample Solution:-
C++ Code :
#include <iostream> // Include input/output stream library
using namespace std; // Use the standard namespace
int main() // Main function where the execution of the program starts
{
    int n, i, j, k, m = 0; // Declare integer variables 'n', 'i', 'j', 'k', 'm' and initialize 'm' as 0
    cout << "\n\n Print patern........:\n"; // Display message prompting for pattern printing
    cout << "-----------------------------------\n"; // Display separator line
    cout << " Input number or rows: "; // Prompt user to input the number of rows for the pattern
    cin >> n; // Read the number of rows input by the user
    for (i = 1; i <= n; i++) { // Outer loop iterating through each row from 1 to 'n'
        if (i == 1) { // For the first row,
            for (j = 1; j <= i; j++) { // Loop to print 'm' (which is 0) once
                cout << m << "  ";
            }
            for (k = 1; k <= n - i; k++) { // Loop to print numbers from 1 to (n - i)
                cout << k << "  ";
            }
        }
        else { // For rows other than the first row,
            for (k = i - 1; k >= 1; k--) { // Loop to print numbers in decreasing order from (i - 1) to 1
                cout << k << "  ";
            }
            cout << m << "  "; // Print 'm' (which is 0) once in the middle of the row
            for (j = 1; j <= n - i; j++) { // Loop to print numbers from 1 to (n - i)
                cout << j << "  ";
            }
        }
        cout << endl; // Move to the next line after printing each row
    }
    cout << endl; // Add an extra newline at the end
}
Sample Output:
Print patern........: ----------------------------------- Input number or rows: 8 0 1 2 3 4 5 6 7 1 0 1 2 3 4 5 6 2 1 0 1 2 3 4 5 3 2 1 0 1 2 3 4 4 3 2 1 0 1 2 3 5 4 3 2 1 0 1 2 6 5 4 3 2 1 0 1 7 6 5 4 3 2 1 0
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to generate a square matrix where the main diagonal is 0, and each off-diagonal element is the distance from the diagonal.
 - Write a C++ program that creates a matrix such that the element at (i, j) equals the absolute difference between i and j.
 - Write a C++ program to construct a square matrix where elements increase incrementally from the main diagonal outward.
 - Write a C++ program that reads a number n and outputs an n×n matrix with 0's on the diagonal and ascending numbers on adjacent diagonals.
 
Go to:
PREV : First N Numbers for a Specific Base.
 NEXT :  Decimal to Binary Conversion.
C++ Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
