434,711 Members | 2,144 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,711 IT Pros & Developers. It's quick & easy.

Passing 2D Dynamic Arrays to Functions?

 P: n/a I have two matrices allocated dynamically in both directions: matrix x and matrix v. I want to pass these matrices into a function by reference. What I have written down isn't working... can somebody enlighten me on how I would solve this? Here is what my prototype and my function look like: ================================================== === double distance(int k, int i, int& n, double *x[], double *v[]); double distance(int k, int i, const int& n, double *x[], double *v[]) { double w; for (int j=0; j
6 Replies

 P: n/a "fivelitermustang" wrote in message news:a6******************************@localhost.ta lkaboutprogramming.com... I have two matrices allocated dynamically in both directions: matrix x and matrix v. I want to pass these matrices into a function by reference. What I have written down isn't working... can somebody enlighten me on how I would solve this? Here is what my prototype and my function look like: ================================================== === double distance(int k, int i, int& n, double *x[], double *v[]); double distance(int k, int i, const int& n, double *x[], double *v[]) { double w; for (int j=0; j

 P: n/a Yuk! Your approach doesn't use any of the featurs of C++, you are still thinking in C! My suggestion would be to use the STL to generate your matrices. You can have a general Matrix class, with the matrix represented internally as variable of type vector of vectors. I've seen this published somewhere, I can't remember where, but you can probably find it. As a taste, look at the code below. You can encapsulate things quiet nicely and include the various matrix operations, multiply, addition, negation etc, as overloaed operators, perhaps even provide meaning to / using the inverse operation. Throw exceptions if the matrices are incompatible or operation is undefined. ( take a look at Scott Myer's book Effective C++, I think he handles two dimensional subscripting very nicely using a proxy class, my approach below is a bit hacked. ) #include using namespace std; class Matrix { public: // create zero matrix Matrix( int M, int N ) { for (int m=0; m row; for (int n=0; n& operator[]( int rowindex) { return _matrix[rowindex]; } public: Matrix operator*( const Matrix& m ) { // check validity of multiplying these matrices // throw execption / bomb out if necessary. } private: vector< vector > _matrix; }; // test program to illustrate the [][] indexing of the matrix elements int main(int argc, char* argv[]) { Matrix m(3, 3 ); printf (" M=%f\n", m); m=1.0; printf (" M=%f\n", m); double foo = m; printf (" M=%f\n", m); return 0; } "fivelitermustang" wrote in message news:a6******************************@localhost.ta lkaboutprogramming.com... I have two matrices allocated dynamically in both directions: matrix x and matrix v. I want to pass these matrices into a function by reference. What I have written down isn't working... can somebody enlighten me on how I would solve this? Here is what my prototype and my function look like: ================================================== === double distance(int k, int i, int& n, double *x[], double *v[]); double distance(int k, int i, const int& n, double *x[], double *v[]) { double w; for (int j=0; j

 P: n/a >>Ho hum. What does 'doesn't work' mean? Does it give a >>compile error, a runtime error, does it run to completion but not give the >>results you expect?What are the results you expect?Is this really the code you have? The obvious problem is >>that w is anuninitialised variable.How exactly are you allocating your arrays, and how exactly >>are youcallingthe function? Both of these are potential issues.Why did you choose 'double *x[]', instead of 'double **x', >>any particularreason? So many questions. Well, I chose to use double *x[] because it seemed intuitive to me. How would I go about initializing it using **x? And how would I pass a matrix initialized like that by reference? The entire program is written however it is all monolithic and it does work properly. I just want to put some of these operations into functions for clarity and easier coding. The error that it gives me is a compile time error. "Error : function call 'distance(int, int, const int, double **, double **)' does not match 'std::distance<...>(T0, T0)' 'distance(int, int, int &, double **, double **)' clustering.cpp line 157 q = q + pow((u[i][k]),m)*distance(k, i, n, x, v);" Jul 22 '05 #4

 P: n/a "fivelitermustang" wrote in message news:ca******************************@localhost.ta lkaboutprogramming.com...Ho hum. What does 'doesn't work' mean? Does it give a >>compile error, a runtime error, does it run to completion but not give the >>results you expect?What are the results you expect?Is this really the code you have? The obvious problem is >>that w is anuninitialised variable.How exactly are you allocating your arrays, and how exactly >>are youcallingthe function? Both of these are potential issues.Why did you choose 'double *x[]', instead of 'double **x', >>any particularreason?So many questions. Well, I chose to use double *x[] because it seemed intuitive to me. How would I go about initializing it using **x? And how would I pass a matrix initialized like that by reference? The entire program is written however it is all monolithic and it does work properly. I just want to put some of these operations into functions for clarity and easier coding. The error that it gives me is a compile time error. "Error : function call 'distance(int, int, const int, double **, double **)' does not match 'std::distance<...>(T0, T0)' 'distance(int, int, int &, double **, double **)' clustering.cpp line 157 q = q + pow((u[i][k]),m)*distance(k, i, n, x, v);" OK well I can now see the other problem Look at your prototype double distance(int k, int i, int& n, double *x[], double *v[]); and your function double distance(int k, int i, const int& n, double *x[], double *v[]) There's an extra const in the function which isn't in the prototype. Personally I would change both to plain int, there's not much point in const int&, and you don't need int&. And fix that uninitialised variable w. john Jul 22 '05 #5

 P: n/a Essentially I had "n" defined at the beginning of the program as a constant. When I was calling the function it wasn't expecting "n" to be a constant. I just changed both the definitions to contain const and that did the trick... Thanks a lot for the help. Jul 22 '05 #6

 P: n/a "fivelitermustang" wrote in message news:ed******************************@localhost.ta lkaboutprogramming.com... Essentially I had "n" defined at the beginning of the program as a constant. When I was calling the function it wasn't expecting "n" to be a constant. I just changed both the definitions to contain const and that did the trick... Thanks a lot for the help. Glad you worked it out. The essential point is that the prototype and the actual function should have the same signature. Just because n is a constant is no reason to use const int&, as I said a plain int is better. john Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion. 