2D array, pointer and function

Dear Friends,
Below I wrote one part of a code. In this part I defined a function get_particle_velocity which calculates u[][] according to the other parameters.
Now if I want to use the get_particle_velocity in my code for example in a for loop can I write:
get_particle_velocity(n, x, u, fluid_u); ?
If not what should I write?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  void get_particle_velocity(int n, double **x, double **u, double ***fluid_u, double **geometry){
	int px, py;
    int j;

	px=int(x[n][0]);
	py=int(x[n][1]);

	for (j=0; j<2; j++){
		if(geometry [px][py]==0 || particle_status[n]==0)
			u[n][j] = fluid_u[px][py][j];
		else
			u[n][j] = 0;
	}
}
Last edited on
would you please answer my question above?
what should I write?

Whatever that gets the job done.

You have shown the parameter types required by the function, but you have not shown the type of the variables that you propose to pass to it. Therefore, we cannot possibly answer your question.

You had a very related scenario in your previous thread (but with more details). I did give couple "pointers" there.
Thanks for your reply.
Consider the code below how can I send n to func in line 20 to get sets of two dimensional arrays?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>
#include <cmath>
using namespace std;
 int px=100;
 int py=100;
void func(int **x, int n, int **y, int **u){

	px=int(x[n][0]);
	py=int(x[n][1]);
	for (int j=0; j<2; j++){
	u[n][j]= y[px][py];
	}

}

int main()
{
int n;
	for (n=0; n<10; n++){
	cout<< func(n);
	}

return 0;

}
Last edited on
A program has both the big picture and the details. It is a good strategy to divide the whole into smaller logical subtasks. However, the detail that you are now asking seems to depend on the big picture.

There is an operation that you want to do. It requires data. The data should be in suitable data structures. The operation involves algorithms. The algorithms use that data and thus interact with the data structures. The structures and the algorithms have to be compatible and support each other. There are many ways to choose them.

What makes you think that the func has to have those particular parameter types? You know the problem that the program should solve.

For example, what is the range of valid values that are stored in the x? Why are px and py global variables in such minimal example?

how can I send n to func in line 20 to get sets of two dimensional arrays?

You do show the implementation of func() and thus reveal that it does not "give" 2D arrays. It merely operates on existing arrays. Less than that; it uses one 2D point as index to retrieve exactly one value from array y and copies it to both coordinates of one 2D point of list u. The func does not return any value, yet you try to pass something to a ostream.

Too much guessing.

I'll rewrite that example, but this is equally incomplete and nonfunctional:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const size_t BAR = ...;

struct Point {
  int x;
  int y;
};

Point func( const Point & a, const int b[][BAR] ) {
  const int c = b[a.y][a.x];
  Point d { c, c };
  return d;
}

int main()
{
  const int N = 10;
  Point x[N] {};
  int y[BAR][BAR] {};
  Point u[N]
  for (int n=0; n < N; ++n) {
    u[n] = func( x[n], y );
  }

  return 0;
}

The loop on lines 20-22 updates the u just like your example does (with the exception that your main() does not have array u).
Topic archived. No new replies allowed.