# Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0’s in C++

C++Server Side ProgrammingProgramming

Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always.

This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row.

## Example

#include <iostream>
using namespace std;
void makeSymmetricMatrix(int n) {
int matrix[n][n];
for(int i = 0; i<n; i++){
int count = 1;
for(int j = 0; j <n; j++){
if(i == j){
matrix[i][j] = 0;
}else{
matrix[i][j] = count++;
}
}
}
for(int i = 0; i<n; i++){
for(int j = 0; j <n; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
int main() {
int n = 5;
makeSymmetricMatrix(n);
}

## Output

0 1 2 3 4
1 0 2 3 4
1 2 0 3 4
1 2 3 0 4
1 2 3 4 0
Published on 24-Oct-2019 13:02:41