function, array, pointer problem

This is a discussion on function, array, pointer problem within the C++ Programming forums, part of the General Programming Boards category; Hi people. This code works fine when N=DIM, but for N!=DIM doens't. How can i fix that and why this ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    8

    function, array, pointer 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


    Code:
    #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=4;
      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

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try i*DIM instead of i*N.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2011, 01:01 AM
  2. Pointer + Array + Function Problem~~
    By Vinsento in forum C Programming
    Replies: 4
    Last Post: 07-26-2011, 07:49 AM
  3. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  4. pointer to an array in a function
    By js_badboy in forum C Programming
    Replies: 5
    Last Post: 09-13-2003, 12:09 PM
  5. Array of pointer function
    By BigAl in forum C Programming
    Replies: 4
    Last Post: 11-14-2001, 01:19 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21