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<n; j++)
{
w = w + pow((x[k][j]  v[i][j]), 2);
}
return w;
}
In addition, I have another question. Is it possible to call this function
by only using the first two parameters (i and k)?
"x" and "v" are the matrices that are being read in and they will always
be used. "n" is a set constant at the beginning of the program that isn't
changed. It would clean up the code if I could just say "distance(i, k)"
and the other three variables would automatically be used.
As it sits now, this is how the function would be called (given that I
could get that double pointer issue sorted out... LOL!)
double q = 0;
for (int i=0; i<C; i++)
{
for (int k=0; k<Nm; k++)
{
q = q + pow((u[i][k]),m)*distance(k, i, n, x, v);
}
}
Any help would be greatly appreciated. :)  
Share this Question
P: n/a

"fivelitermustang" <fi**************@shaw.ca> 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<n; j++) { w = w + pow((x[k][j]  v[i][j]), 2); } return w; }
Ho hum. What does 'doesn't work' mean? Does it give a compile error, a run
time 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 an
uninitialised variable.
How exactly are you allocating your arrays, and how exactly are you calling
the function? Both of these are potential issues.
Why did you choose 'double *x[]', instead of 'double **x', any particular
reason?
So many questions. In addition, I have another question. Is it possible to call this function by only using the first two parameters (i and k)?
Yes, if you write a class. Put x and v and n as members of the class and
distance as a method of the class. Since you're programming C++, and C++ is
all about classes, this might be a good oppotunity to get some practise with
them.
"x" and "v" are the matrices that are being read in and they will always be used. "n" is a set constant at the beginning of the program that isn't changed. It would clean up the code if I could just say "distance(i, k)" and the other three variables would automatically be used.
john  
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 <vector>
using namespace std;
class Matrix
{
public:
// create zero matrix
Matrix( int M, int N )
{
for (int m=0; m<M ; m++)
{
vector<double> row;
for (int n=0; n<N; n++)
{
row.push_back(0.0);
}
_matrix.push_back(row);
}
}
Matrix( const Matrix& m )
{
// copy constructor
}
public:
vector<double>& 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<double> > _matrix;
};
// test program to illustrate the [][] indexing of the matrix elements
int main(int argc, char* argv[])
{
Matrix m(3, 3 );
printf (" M[0][0]=%f\n", m[0][0]);
m[0][0]=1.0;
printf (" M[0][0]=%f\n", m[0][0]);
double foo = m[0][0];
printf (" M[0][0]=%f\n", m[0][0]);
return 0;
}
"fivelitermustang" <fi**************@shaw.ca> 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<n; j++) { w = w + pow((x[k][j]  v[i][j]), 2); } return w; }
In addition, I have another question. Is it possible to call this function by only using the first two parameters (i and k)? "x" and "v" are the matrices that are being read in and they will always be used. "n" is a set constant at the beginning of the program that isn't changed. It would clean up the code if I could just say "distance(i, k)" and the other three variables would automatically be used.
As it sits now, this is how the function would be called (given that I could get that double pointer issue sorted out... LOL!)
double q = 0; for (int i=0; i<C; i++) { for (int k=0; k<Nm; k++) { q = q + pow((u[i][k]),m)*distance(k, i, n, x, v); } }
Any help would be greatly appreciated. :)  
P: n/a

>>Ho hum. What does 'doesn't work' mean? Does it give a >>compile error, a
run time 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 an uninitialised variable.
How exactly are you allocating your arrays, and how exactly >>are you calling the 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);"  
P: n/a

"fivelitermustang" <fi**************@shaw.ca> 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 an uninitialised variable.
How exactly are you allocating your arrays, and how exactly >>are you calling the 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  
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.  
P: n/a

"fivelitermustang" <fi**************@shaw.ca> 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   This discussion thread is closed Replies have been disabled for this discussion.   Question stats  viewed: 1795
 replies: 6
 date asked: Jul 22 '05
