# How do you write a c plus plus program to add a magic square matrix?

Let me know if this code works

#include <iostream>

#include <iomanip>

using namespace std;

int size(bool flag)

{

int test; if(flag)

{

do

{

test=(int)(double(rand())/double(RAND_MAX)*14.0+3.0);

}

while(test%2==0);

return test;

}

else return 7;

}

int main()

{

int n = 7;

int row, col, size;

int magic[19][19];

for(int j=0 ; j<n ; j++)

{

for(int k=0 ; k<n ; k++)

{

magic[j][k] = 0;

}

}

row=1; col=(n+1)/2;

magic[row-1][col-1]=1;

for(int i=2 ; i<=(n*n) ; i++)

{

row-=1;

col-=1;

if(row==0 && 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 (row=1; row<=n; row++)

for(col=1; col<=n; col++)

magic[row][n+1]+=magic[row][col];

for(col=1; col<=n; col++)

for(row=1; row<=n; row++)

magic[n+1][col] += magic[row][col];

for(int r=0; r<(n+1) ; r++)

{

cout<<endl;

for(int c=0; c<(n+1) ; c++)

{

cout << setw (5) << magic[r][c];

} cout<<endl;

}

return 0;

}