473,323 Members | 1,560 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

two-dimentional array with non constant variables

Al
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

Oct 27 '05 #1
8 5118
Al wrote:
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-lit...html#faq-16.16

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

Cheers! --M

Oct 27 '05 #2
Al wrote:
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
Oct 27 '05 #3
Al
you know... just newbie training...
thx

Oct 27 '05 #4
Al wrote:
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.

Oct 27 '05 #5

Al wrote:
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<int> > 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;

Oct 28 '05 #6
Al
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>)

Oct 28 '05 #7
Al
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<int> >& v)
{
for(int i = 0;i<v.size();i++)
{ for(int j = 0;j< /*here*/ ;j++)
{ cout <<"v["<<i<<"]["<<j<<"] = ";
cin >>v[i][j]; } }
}

Oct 28 '05 #8
Al wrote:
what should I do to get the size of the second dimension of the vector,
without using it as a parameter?
v[i].size()

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

Oct 29 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a....
8
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). ...
6
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a...
6
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form...
7
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to...
0
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application...
9
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where...
9
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a...
13
by: paul.joseph.davis | last post by:
Hi, I've just had my first encounter with two-phase lookup and I'm scratching my head a bit. The idea behind two phase look up is pretty easy to understand, but I have a case that fails to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.