Vector of vectors & magic square issues

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2007
Posts: 3
Reputation: rQQt2 is an unknown quantity at this point 
Solved Threads: 0
rQQt2 rQQt2 is offline Offline
Newbie Poster

Vector of vectors & magic square issues

 
0
  #1
Mar 1st, 2007
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;
}
}
Last edited by rQQt2; Mar 1st, 2007 at 4:09 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Vector of vectors & magic square issues

 
0
  #2
Mar 1st, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: rQQt2 is an unknown quantity at this point 
Solved Threads: 0
rQQt2 rQQt2 is offline Offline
Newbie Poster

Re: Vector of vectors & magic square issues

 
0
  #3
Mar 1st, 2007
Here's my try for the properly indented code with the display function properly pasted. Sorry for the inconveniences, but pasting it from VC doesn't preserve whitespace, whis is very annoying!



#include"stdafx.h"
#include<iostream>
#include<vector>
usingnamespace std;




class MagicSquare{

private:
vector<vector<int> > data;

int sz; // !! sz is the size of the magic square, therefore the demensions of
// the data 'matrix'
int nsqr;
int i,j;

public:
MagicSquare(int n);
void displaySquare(void);
};

int main()
{
MagicSquare test(7);
test.displaySquare();

return 0;
}


MagicSquare::MagicSquare(int n)
{
sz=n;
vector<int> v(n);
vector<vector<int> > data(n,v);


nsqr = n * n;
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::displaySquare(void)
{
cout<<"\n\n\n\n####in printing function\n\n";
for (int i=0; i<sz; i++)
{
for (int j=0; j<sz; j++)
cout << data[i][j] << endl;
cout<<endl;
}

}
Last edited by rQQt2; Mar 1st, 2007 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,617
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Vector of vectors & magic square issues

 
0
  #4
Mar 2nd, 2007
Two problems I can see:

• Your memory allocation is not proper. Each of your rows share a common vector variable because of the stmt:
  1. vector<int> v(n);
  2. 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:
  1. 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 :
  1. i=0;
  2. j=n/2;
  3. for (int k=1; k<=nsqr; ++k)
  4. {
  5. data[i][j] = k;
  6. 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).
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 3
Reputation: rQQt2 is an unknown quantity at this point 
Solved Threads: 0
rQQt2 rQQt2 is offline Offline
Newbie Poster

Re: Vector of vectors & magic square issues

 
0
  #5
Mar 2nd, 2007
Originally Posted by ~s.o.s~ View Post
Two problems I can see:

• Your memory allocation is not proper. Each of your rows share a common vector variable because of the stmt:
  1. vector<int> v(n);
  2. 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:
  1. 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 :
  1. i=0;
  2. j=n/2;
  3. for (int k=1; k<=nsqr; ++k)
  4. {
  5. data[i][j] = k;
  6. 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).

Ok, thanks a lot man. I've finally fixed my code, and the proram works fine now. Thanks for all the help
Cheers!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC