473,503 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing 2D Dynamic Arrays to Functions?

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. :)

Jul 22 '05 #1
6 2179

"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
Jul 22 '05 #2

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. :)

Jul 22 '05 #3
>>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);"

Jul 22 '05 #4

"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
Jul 22 '05 #5
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

"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
Jul 22 '05 #7

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

Similar topics

4
1568
by: Vannela | last post by:
How can i achieve the dynamic arrays concept in C#? I have used the ArrayLists of C# but in few cases it is failing like when i pass these dynamic arrays a arguments to functions b'coz in those...
58
10046
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
7663
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
9
4765
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
2
1948
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
2
3309
by: NM | last post by:
Hello all, I am supposed to do some mixed programming with c++ and fortran. I was succeeful in exchanging the 2D arrays from fortran to c++ and the other way, but was unable to that same with...
6
2958
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
10
3123
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
60
10093
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: >...
0
7204
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.