Print out symmetric matrix from compactly stored array I have an array of the form (3x3 case as an example)
$$A = [a_{11}, a_{12}, a_{22}, a_{13}, a_{23}, a_{33}] $$
corresponding to the symmetric matrix
$$\begin{pmatrix} a_{11} &amp; a_{12} &amp; a_{13}\\ a_{12} &amp; a_{22} &amp; a_{23} \\ a_{13} &amp; a_{23} &amp; a_{33} \end{pmatrix}$$
I want to print out this very matrix (in general of size NxN) given the array A (in general of length N*(N+1)/2) in a nice way.
My approach was
void print__symm_matrix_packed(double* arr, int N){
  
  int idx2 = 0;

  for(int i=0; i&lt;N; i++){
    printf(&quot;(&quot;);
    
    int idx1 = i;
    
    for(int j=0; j&lt;N; j++){ 
      if(j &lt; i){
        printf(&quot;%f &quot;, arr[idx2 + j]);  
      } else {
        printf(&quot;%f &quot;, arr[idx1]);
      }
      idx1 += j+1;
    }
    idx2 += i+1;
    printf(&quot;)\n&quot;);
  }
}

Is there room for some elegant improvement?
I think it the following is a good approach:
//use lowercase on non constant values
void print_symmetric_packed_matrix(double* compact_matrix, int n)
{
    const int entries = N*N;
    for (int i = 0; i &lt; entries;)
    {
        //i++ here avoids i != 0 each iteration
        printf(&quot;%f &quot;, compact_matrix[i++]);
        if (i % N == 0) printf(&quot;\n&quot;);
    }
}

I hope it helped you.