Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

*Another* array question

This topic is 4595 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright...last question! I promise! My code works fine if I hard code an array of [15][10] (col x row). But when I use my dynamic array that you guys helped me make, it come out all gimped. Soo...I assume its because the array is using row x col and I want col x row. So this is my array2D class,
#ifndef TwoDArray_H
#define TwoDArray_H

template <class T>
class Array2D
{
public:
	//creates a new 2D array of [row][col]
	void createArray(int row, int col);

	//delete our array
	void deleteArray();

	//so we can do m(x,y). more flexible than [].
	T& operator () ( int  row,  int  col);

	//how many colums and rows
	int retnCol();
	int retnRow();

	int ARRAY_COLUMS;
	int ARRAY_ROW;

	//size in bytes
	//int retnSize();

private:
	//the actual array, which is really 1D
	T *Array;

	//how many Cols we have

};

template<class T> void Array2D<T>::createArray(int row, int col)
{
	Array = new T[row * col];
	ARRAY_COLUMS = col;
	ARRAY_ROW = row;
}
template<class T> void Array2D<T>::deleteArray()
{
	delete[] Array;
	Array = NULL;
}

template<class T> T& Array2D<T>::operator () ( int  row,  int  col)
{
		return Array[row*ARRAY_COLUMS + col];

}

template<class T> int Array2D<T>::retnCol()
{
	return MATRIX_COLUMS;
}

template<class T> int Array2D<T>::retnRow()
{
	return MATRIX_ROW;
}
#endif

So my question is, how can i make this work with col, row instead of row,col. I've tried a few things, all of which didn't work. Thanks!!!

Share this post


Link to post
Share on other sites
Advertisement
I assume people have already suggested Boost.MultiArray, and that you have a good reason for using column-major form instead of row-major. In which case, this may be useful.

If not, then perhaps the problem is elsewhere. In particular, if you aren't consistant with how you access the array [sometimes doing (col, row) instead of (row, col)], you'll get weird results.

CM

Share this post


Link to post
Share on other sites
Is it better to use row major vs col major?
Ah, nice link! Thanks!

//edit
Is there a preferance to either way? Even just style wise?

[Edited by - _Sigma on January 21, 2006 8:55:20 PM]

Share this post


Link to post
Share on other sites
Row-major is the most common (and, in my opinion, the most intuitive).
Technically it makes no difference. But if you try to traverse the entire array in the wrong order (row by row in a column-major array, or vice versa), you get horrible performance. So as long as you're consistent... :)

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sigma
Is it better to use row major vs col major?
Ah, nice link! Thanks!

//edit
Is there a preferance to either way? Even just style wise?


when/if you start writing a opengl compatable math library you gata use colom major.

which becomes a big headache when trying to allow your matrix struct be type casted into a (float *) >.<

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!