pointers array problem

Hi people.

This code works fine when N=DIM, but for N!=DIM doens't. How can i fix that and why this happen

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>

void InitialValues(double *ptr_total_force,double *ptr_chain_r, double *ptr_chain_v, double sigma,const int N, const int DIM){

  double randomaux;
  for (int i=0;i<N;i++){
    for (int d=0;d<DIM;d++){
      // vector force
      ptr_total_force[i*N+d]=0;
      // vector positions and velocities
      ptr_chain_r[i*N+d] = 0.0;
      ptr_chain_v[i*N+d] = 0.0;
      if (d==0){
        randomaux = 0.001*i*d; //gsl_rng_uniform_pos(r1);
        ptr_chain_r[i*N+d] = randomaux;
        // vector velocities
        randomaux = 0.5*(i-d);//(2*gsl_ran_gaussian(r2,sigma) - 1);
        ptr_chain_v[i*N+d] = randomaux;
      }
    }
    ptr_chain_r[i*N+0] += sigma*i;
    printf("%f\t%f\n",ptr_chain_r[i*N+0],ptr_chain_v[i*N+0]);
  }

}


int main(){
  const int N=4;
  const int DIM=3;
  double total_force[N][DIM];
  double total_force_old[N][DIM];
  double chain_r[N][DIM];   // monomer positions
  double chain_v[N][DIM];   // monomer velocities
  double sigma = 1.0; // diametro
  
  InitialValues(total_force[0],chain_r[0], chain_v[0],sigma, N, DIM);
  printf("-------------\n");
  for (int i=0;i<N;i++){
    printf("%f\t%f\n",chain_r[i][0],chain_v[i][0]);
  }

}


OUTPUT examples
N=4, DIM=3
0.000000 0.000000
1.000000 0.500000
2.000000 1.000000
3.000000 1.500000
-------------
1.500000 0.000000
0.000000 0.000000
0.000000 0.000000
0.000000 0.000000

N=4, DIM=4
0.000000 0.000000
1.000000 0.500000
2.000000 1.000000
3.000000 1.500000
-------------
0.000000 0.000000
1.000000 0.500000
2.000000 1.000000
3.000000 1.500000


Pleaseeee any HELPPPP!!!

Carlos
Because data is stored in row-major order http://en.wikipedia.org/wiki/Row-major_order
Need to use i*DIM not i*N.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>

void InitialValues(double *ptr_total_force,double *ptr_chain_r, double *ptr_chain_v, double sigma,const int N, const int DI
M){

  double randomaux;
  for (int i=0;i<N;i++){
    for (int d=0;d<DIM;d++){
      // vector force
      ptr_total_force[i*DIM+d]=0;
      // vector positions and velocities
      ptr_chain_r[i*DIM+d] = 0.0;
      ptr_chain_v[i*DIM+d] = 0.0;
      if (d==0){
        randomaux = 0.001*i*d; //gsl_rng_uniform_pos(r1);
        ptr_chain_r[i*DIM+d] = randomaux;
        // vector velocities
        randomaux = 0.5*(i-d);//(2*gsl_ran_gaussian(r2,sigma) - 1);
        ptr_chain_v[i*DIM+d] = randomaux;
      }
    }
    ptr_chain_r[i*DIM+0] += sigma*i;
    printf("%f\t%f\n",ptr_chain_r[i*DIM+0],ptr_chain_v[i*DIM+0]);
  }

}


int main(){
  const int N=4;
  const int DIM=3;
  double total_force[N][DIM];
  double total_force_old[N][DIM];
  double chain_r[N][DIM];   // monomer positions
  double chain_v[N][DIM];   // monomer velocities
  double sigma = 1.0; // diametro
  
  InitialValues(total_force[0],chain_r[0], chain_v[0],sigma, N, DIM);
  printf("-------------\n");
  for (int i=0;i<N;i++){
    printf("%f\t%f\n",chain_r[i][0],chain_v[i][0]);
  }

}
0.000000        0.000000
1.000000        0.500000
2.000000        1.000000
3.000000        1.500000
-------------
0.000000        0.000000
1.000000        0.500000
2.000000        1.000000
3.000000        1.500000

thanks a lot histrungalot, maybe you know too how to debug the values of the pointer for example *ptr_chain_r ? because i need to debug other function and i just get the first value.

Carlos
Post example
Topic archived. No new replies allowed.