0

Hello folks

I'm trying to simulate a magic square algorithm, but having some problems. The program successfully creates the magic square, but when I'm trying to print it, the VC2005 compiler says "vector subscript out of range", which it shouldnt be.

Any help would be appreciated.

Cheers

Here's the code:

```
class Magicsquare{
public:
Magicsquare(int n);
void display(void);
private:
vector<vector<int> > square; //storing #s here
int totalsize; //stuff for generating the square
int nsqr;
int i,j;
};
MagicSquare::MagicSquare(int n)
{
totalsize=n;
vector<int> v(n);
vector<vector<int> > square(n,v);
nsqr = n * n; //this part runs ok
i=0;
j=n/2;
for (int k=1; k<=nsqr; ++k)
{
data[i][j] = k;
i--;
j++;
if (k%n == 0)
{
i += 2;
--j;
}
else
{
if (j==n)
j -= n;
else if (i<0)
i += n;
}
}
}
void Magicsquare::display(void) //this one doesn't
{
for (int i=0; i<sz; i++)
{
for (int j=0; j<sz; j++)
cout << data[i][j] << endl;
cout<<endl;
}
}
```