title: Magic Square, I can't get this to sum the rows, columns, and diagonals
body: <p>I need to sum and output the rows, columns, and main diagonals. Can someone please tell me what I'm doing wrong? This is the code I was given to use but I can't seem to get it to output my sums. I put a comment in the code below where I beleive my problem exist, where it starts summing and where it stops. I would appreciate any help as I am having trouble figuring this out on my own. The output should a 3 x 3 matrix with the sums of the rows, columns and diagonals in a fourth row and column to equal 15. My output is not summing the rows, columns, and diagonls.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  
  int main()
  {
  
  int n = 3;
  int row, col, r, c, i, j, k, diag=0;
  int magic[19][19];
  ofstream fout;
  fout.open("p4-out.txt");
  
  for(j=0 ; j&lt;n+1 ; j++){
      for(k=0 ; k&lt;n+1 ; k++)
      magic[j][k] = 0;}
  
  row=1; col=(n+1)/2;
  magic[row-1][col-1]=1;
  
  for(i=2 ; i&lt;=(n*n) ; i++){
      row-=1; col-=1;
      if(row==0 &amp;&amp; col==0){col++; row+=2;}
      else if(row==0) row=n;
      else if(col==0) col=n;
      else if(magic[row-1][col-1]!=0)
      {col++; row+=2;}
      magic[row-1][col-1]=i;}
  
  for(r=0; r&lt;n; r++){                    
      for(c=0; c&lt;n; c++)
      magic[r][n]+=magic[r][c];}
  
  for(c=0; c&lt;n; c++){
      for(r=0; r&lt;n; r++)
      magic[n][c]+=magic[r][c];}
  
  for(r=0; r&lt;n; r++){
      magic[n][n]+=magic[r][r];}
  
  for(r=1; r&lt;(n-1); r++){
      c = n - r + 1;
      diag+=magic[r][c];}              
  
  for(r=0; r&lt;(n+1) ; r++){
      fout &lt;&lt; endl;
      for(c=0; c&lt;(n+1) ; c++)
      {fout &lt;&lt; setw (5) &lt;&lt; magic[r][c];}
      fout &lt;&lt; endl;}
  return 0;
  }
  
  
                               // This my output.
                        15 &lt;---// I need this 15 to go
  
          6    1    8   15                       |
                                                 |
          7    5    3   15                       |
                                                 |
          2    9    4   15                       |
                                                 |
   [15]  15   15   15   15                       |
   ^-------------&lt;------------&lt;----------&lt;-------|
   here
  </code></pre>