C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

two-dimentional array with non constant variables

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Al
Guest





PostPosted: Thu Oct 27, 2005 12:59 pm    Post subject: two-dimentional array with non constant variables Reply with quote



I'd like to declare (in a Matrix class) a two-dimentional array with
user-defined coordinates. The constructor is:
Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {}

the compiler says 'lin' should be a constant, but I want it to be
defined from the user, so what should I do? and how must 'a' be
declared??
help me please

Back to top
Kai-Uwe Bux
Guest





PostPosted: Thu Oct 27, 2005 1:18 pm    Post subject: Re: two-dimentional array with non constant variables Reply with quote



Al wrote:

Quote:
I'd like to declare (in a Matrix class) a two-dimentional array with
user-defined coordinates. The constructor is:
Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {}

the compiler says 'lin' should be a constant, but I want it to be
defined from the user, so what should I do? and how must 'a' be
declared??
help me please

a) Do not roll your own matrix code unless you absolutely have to. Numerical
analysis is hard. Use a library instead. There are many good linear algebra
libraries out there. Google is your friend.

b) Do not use float for numerical computations unless you know with
certainty that the precision is good enough for your needs. Beware that
linear algebra algorithms can go mad on precision.

c) If you absolutely need to run your own matrix class, do not do the memory
management by yourself. Simply do

template <typename ArithmeticType>
class Matrix

std::size_t row_size;
std::size_t col_size;
std::vector< ArithmeticType > data;

Matrix ( std::size_t rows, std::size_t cols )
: row_size ( rows )
, col_size ( cols )
, data ( rows*cols )
{}

ArithmeticType &
operator() ( std::size_t row, std::size_t col ) {
// check for bounds here if you please ...
return( this->data[row*this->cols_size + col ] );
}

ArithmeticType const &
operator() ( std::size_t row, std::size_t col ) const {
// check for bounds here if you please ...
return( this->data[ row*this->cols_size + col ] );
}

};

You might also want to have a look into std::valarray and the numeric
header.

d) Adhere to established math conventions: matrices have rows and columns,
and rows go first. Make that second nature, and bugs will go away.

e) Again: do not roll your own code.


Best

Kai-Uwe Bux

Back to top
mlimber
Guest





PostPosted: Thu Oct 27, 2005 1:19 pm    Post subject: Re: two-dimentional array with non constant variables Reply with quote



Al wrote:
Quote:
I'd like to declare (in a Matrix class) a two-dimentional array with
user-defined coordinates. The constructor is:
Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {}

the compiler says 'lin' should be a constant, but I want it to be
defined from the user, so what should I do? and how must 'a' be
declared??
help me please

Please consult the FAQ:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.16

But, I'd suggest you use a vector< vector if possible. It can
make life simpler in many ways.

Cheers! --M


Back to top
Al
Guest





PostPosted: Thu Oct 27, 2005 7:10 pm    Post subject: Re: two-dimentional array with non constant variables Reply with quote

you know... just newbie training...
thx

Back to top
Old Wolf
Guest





PostPosted: Thu Oct 27, 2005 10:48 pm    Post subject: Re: two-dimentional array with non constant variables Reply with quote

Al wrote:
Quote:
I'd like to declare (in a Matrix class) a two-dimentional array with
user-defined coordinates. The constructor is:
Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {}

the compiler says 'lin' should be a constant, but I want it to be
defined from the user, so what should I do? and how must 'a' be
declared??

You've struck right to the heart of the problem. 'a' must be
declared as:

float (*a)[lin];

But C++ requires that the array dimension is a compile-time constant.
So it is not possible to have this array as you would like.

You could either make 'a' a one-dimension array and add a
function for accessing it as if it were 2-D; or you could
use a vector of vectors.


Back to top
Axter
Guest





PostPosted: Fri Oct 28, 2005 5:57 am    Post subject: Re: two-dimentional array with non constant variables Reply with quote


Al wrote:
Quote:
I'd like to declare (in a Matrix class) a two-dimentional array with
user-defined coordinates. The constructor is:
Matrix(int c, int l): col(c), lin(l), a(new float[col][lin]) {}

the compiler says 'lin' should be a constant, but I want it to be
defined from the user, so what should I do? and how must 'a' be
declared??
help me please

Check out the following link for an example:
http://code.axter.com/dynamic_2d_array.h

However, I recommend you use a vector of vector.
Example:
int col = 123;
int row = 456;
vector<vector My2dArray(col, vector<int>(row));

You can reference both the above vector code and the dynamic_2d_array
class using double index ([][])
My2dArray[0][0] = 99;


Back to top
Al
Guest





PostPosted: Fri Oct 28, 2005 9:45 pm    Post subject: Re: two-dimentional array with non constant variables Reply with quote

thx
I've never used vectors of vectors before, but it's a good reason to
learn it!
How do you use 'typename' ?? (from Kai-Uwe Bux: template<typename
ArithmeticType>)

Back to top
Al
Guest





PostPosted: Fri Oct 28, 2005 10:40 pm    Post subject: Re: two-dimentional array with non constant variables Reply with quote

what should I do to get the size of the second dimension of the vector,
without using it as a parameter?

void getVector(vector<vector& v)
{
for(int i = 0;i<v.size();i++)
{ for(int j = 0;j< /*here*/ ;j++)
{ cout <<"v["< cin >>v[i][j]; } }
}

Back to top
Mark P
Guest





PostPosted: Sat Oct 29, 2005 12:05 am    Post subject: Re: two-dimentional array with non constant variables Reply with quote

Al wrote:
Quote:
what should I do to get the size of the second dimension of the vector,
without using it as a parameter?

v[i].size()

Quote:

void getVector(vector<vector& v)
{
for(int i = 0;i<v.size();i++)
{ for(int j = 0;j< /*here*/ ;j++)
{ cout <<"v["< cin >>v[i][j]; } }
}


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.