Where is the variable sz defined and given avalue, and what value does it have in relation to dimensions of the the variable called data?
Also, please indent code so it is easier to read.
Lerner
Nearly a Posting Maven
2,406 posts since Jul 2005
Reputation Points: 739
Solved Threads: 405
Skill Endorsements: 9
Two problems I can see:
• Your memory allocation is not proper. Each of your rows share a common vector variable because of the stmt:
vector<int> v(n);
vector<vector<int> > data(n,v);
Here the memory for each inner row is getting allocated only once and your two dimensional matrix gets populated with the same vector.
You need to change it to:
vector<vector<int> > data(n,vector<int> (n));
So that the memory allocation occurs for each row rather than only once for all rows.
• Your logic is skewed. The line :
i=0;
j=n/2;
for (int k=1; k<=nsqr; ++k)
{
data[i][j] = k;
i--;
is bound to produce an out of bounds error since you are decemeneting i which already is zero, resulting in its value becoming -1, which is not a valid index.
Better use size_t instead of int for index variables to make sure you don't invade the out of bounds exception land. (assigning -ve values to such variables leads to a warning which is fairly easy to decode).
~s.o.s~
Failure as a human
12,220 posts since Jun 2006
Reputation Points: 3,307
Solved Threads: 783
Skill Endorsements: 55
Question Answered as of 6 Years Ago by
Lerner
and
~s.o.s~