Mutidimensional Array problem

This is a discussion on Mutidimensional Array problem within the C Programming forums, part of the General Programming Boards category; Code: for(trial = 0; trial < N; trial++) { b[trial] = 0.0; nstep = 0; t = 0.0; dis = ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    64

    Mutidimensional Array problem

    Code:
      for(trial = 0; trial < N; trial++)
        {
          b[trial] = 0.0;
          nstep = 0;
          t = 0.0;
          dis = 0;
          counter = 0;
          while(t < to)
    	{
    	  tau = tao * (1 + g * t); //tao, g, t are all entered at the command prompt
    	  dt = -log(a) * tau; //a is entered at the command prompt
    	  u = gsl_ran_flat (r, 0.0, 1.0);
    	  b[nstep * maxstep + dis]++; //maxstep is pre-determined, as is nstep
    	  if(u > a)
    	    {
    	      dis++;
    	    }
    	  t = t + dt;
    	  nstep++;
    	}
        }
      
      for(i = 0; i < maxstep; i++)
        {
          for(j = 0; j < maxstep; j++)
    	{
    	  fprintf(stderr, "%lf ", b[j * maxstep + i]);
    	}
    	  fprintf(stderr, "\n");
        }
    Code:
    20.000000 4.000000 0.000000 2.000000 1.000000 0.000000 0.000000 0.000000 
    0.000000 7.000000 2.000000 6.000000 5.000000 4.000000 2.000000 0.000000 
    0.000000 0.000000 0.000000 10.000000 4.000000 3.000000 2.000000 2.000000 
    0.000000 0.000000 0.000000 2.000000 10.000000 9.000000 8.000000 6.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 4.000000 5.000000 7.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 3.000000 4.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
    Supposedly, the sum of each column should be 20 (N = 20), however, column 2 and 3 do not add up to 20...why not?
    Last edited by vutek0328; 11-14-2006 at 04:55 PM.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Where are all of these variables defined? None of them has types. Why are you printing to stderr?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
      int N, i = 0, j = 0, trial, to = 0, dis = 0, maxstep = 0, nstep = 0;
      double tao, tau, dx, counter = 0, u = 0, a = 0, dt = 0, t = 0, g = 0;
      double *array, *b;
    printing to file.stderr to have a look in the another file instead of under terminal, but that is not necessary.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    What is this a formula for? How is it supposed to be used?
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    #include <gsl/gsl_randist.h>
    
    int main(int argc, char *argv[])
    {
      int N, i = 0, j = 0, trial, to = 0, dis = 0, maxstep = 0, nstep = 0;
      double tao, tau, dx, counter = 0, u = 0, a = 0, dt = 0, t = 0, g = 0;
      double *array, *b; 
      
      if(argc != 7)
        {
          printf("Error!\nUsage: N(int), tao(double), a(double), dx(double), to(int), g(double)\n");
          return 0;
        }
      else
        {
          sscanf( argv[1], "%d", &N);
        }
      sscanf( argv[2], "%lf", &tao);
      sscanf( argv[3], "%lf", &a);
      sscanf( argv[4], "%lf", &dx);
      sscanf( argv[5], "%d", &to);
      sscanf( argv[6], "%lf", &g);
    
      tau = tao * (1 + g * t);
      dt = -log(a) * tau;
      
      const gsl_rng_type * T;
      gsl_rng * r;
      
      gsl_rng_env_setup();
      
      T = gsl_rng_default;
      r = gsl_rng_alloc (T);
      gsl_rng_set (r, time(NULL));
    
      array = (double*) malloc (to * to * sizeof(double));
    
      for(i = 0; i < to * to; i++)
        {
          array[i] = 0.0;      
        }
    
      while(t < to)
        {
          tau = tao * (1 + g * t);
          dt = -log(a) * tau;
          t = t + dt;
          nstep++;
        }
      maxstep = nstep;
    
      b = (double *) malloc (maxstep * maxstep * sizeof(double));
      
      for(trial = 0; trial < N; trial++)
        {
          b[trial] = 0.0;
          nstep = 0;
          t = 0.0;
          dis = 0;
          counter = 0;
          while(t < to)
    	{
    	  tau = tao * (1 + g * t);
    	  dt = -log(a) * tau;
    	  u = gsl_ran_flat (r, 0.0, 1.0);
    	  b[nstep * maxstep + dis]++;
    	  if(u > a)
    	    {
    	      dis++;
    	    }
    	  t = t + dt;
    	  nstep++;
    	}
        }
      
      for(i = 0; i < maxstep; i++)
        {
          for(j = 0; j < maxstep; j++)
    	{
    	  fprintf(stderr, "%lf ", b[j * maxstep + i]);
    	}
    	  fprintf(stderr, "\n");
        }
      free(array);
      free(b);
      return 0;
    }
    This is a simulation of particle hopping in one dimension. maybe it's easier for you to look at then for me to explain, essentially, I need to print out b(i,j) which stores the number of particles which were at position j after i steps out of all N trials. This is used at the end to calculate the average position after i steps.

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Code:
    else
        {
          sscanf( argv[1], "%d", &N);
        }
    This is unneeded. If the condition is true, the program will quit. You may take away the else.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Thanks, but could you explain to me why my two columns don't sum up to 20 respectively? I run 20 trials, so it should have a registered position for every step of the 20 trials. The table is of steps(X) * position(Y).

  8. #8
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I guess I am slow, but I don't understand your question. Show me the exact code that isn't working.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    The code is working fine, there are some malfunctions in the results, or it could be that I'm misinterpreting it.

    I'm running 20 trials.
    Each particle has a position after a certain steps.
    The columns represents all the particles at step 1 ( or 2 or 3...etc)
    The rows represents where the parctiel is at step 1 (or 2 or 3...etc)

    Code:
    20.000000 3.000000 0.000000 3.000000 2.000000 1.000000 0.000000 0.000000 
    0.000000 8.000000 0.000000 6.000000 4.000000 3.000000 3.000000 2.000000 
    0.000000 0.000000 2.000000 9.000000 8.000000 3.000000 2.000000 2.000000 
    0.000000 0.000000 0.000000 2.000000 4.000000 8.000000 7.000000 4.000000 
    0.000000 0.000000 0.000000 0.000000 2.000000 4.000000 4.000000 7.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 3.000000 2.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000 2.000000 
    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 1.000000
    at (0,0), all 20 particles are at position 0 because it's the initial position, ie. step 0.
    at (0,1), it shows that there are 3 particles at position 0 at step 1.
    at (1,1), it shows that there are 8 particles at position 1 at step 1.

    etc.

    So shouldn't there be 20 particles at step 1, regardless of their position? (there are only 8+3 = 11)

  10. #10
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    I guess, but I can't see the problem
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
        b[trial] = 0.0;
    That's not supposed to be there, instead there should have been a loop that initializes all the b values. =) I just found that out.

  12. #12
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    There we go!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  13. #13
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    #include <gsl/gsl_rng.h>
    #include <gsl/gsl_randist.h>
    
    int main(int argc, char *argv[])
    {
      int N, i = 0, j = 0, trial, to = 0, dis = 0, maxstep = 0, nstep = 0;
      double tao, tau, dx, counter = 0, u = 0, a = 0, dt = 0, t = 0, g = 0;
      double *array, *b, *times; 
      
      if(argc != 7)
        {
          printf("Error!\nUsage: N(int), tao(double), a(double), dx(double), to(int), g(double)\n");
          return 0;
        }
      else
        {
          sscanf( argv[1], "%d", &N);
        }
      sscanf( argv[2], "%lf", &tao);
      sscanf( argv[3], "%lf", &a);
      sscanf( argv[4], "%lf", &dx);
      sscanf( argv[5], "%d", &to);
      sscanf( argv[6], "%lf", &g);
    
      tau = tao * (1 + g * t);
      dt = -log(a) * tau;
      
      const gsl_rng_type * T;
      gsl_rng * r;
      
      gsl_rng_env_setup();
      
      T = gsl_rng_default;
      r = gsl_rng_alloc (T);
      gsl_rng_set (r, time(NULL));
    
      array = (double*) malloc (to * to * sizeof(double));
    
      for(i = 0; i < to * to; i++)
        {
          array[i] = 0.0;      
        }
    
      while(t < to)
        {
          tau = tao * (1 + g * t);
          dt = -log(a) * tau;
          t = t + dt;
          nstep++;
        }
      maxstep = nstep;
    
      b = (double *) malloc (maxstep * maxstep * sizeof(double));
      times = (double *) malloc (maxstep *sizeof(double));
     
      for(i = 0; i < maxstep * maxstep; i++)
        {
          b[i] = 0.0;
        }
      
      for(trial = 0; trial < N; trial++)
        {
          nstep = 0;
          t = 0.0;
          dis = 0;
          counter = 0.0;
          while(t < to)
    	{
    	  tau = tao * (1 + g * t);
    	  dt = -log(a) * tau;
    	  u = gsl_ran_flat (r, 0.0, 1.0);
    	  b[nstep * maxstep + dis]++;
    	  if(u > a)
    	    {
    	      dis++;
    	    }
    	  t = t + dt;
    	  nstep++;
    	  times[nstep] = t; //Segmentation Fault...why?
    	  counter = counter + nstep * b[nstep * maxstep + dis]/N;
    	  fprintf(stdout, "%f %f\n", times[nstep], counter);
    	}
        }
      
      for(i = 0; i < maxstep; i++)
        {
          for(j = 0; j < maxstep; j++)
    	{
    	  fprintf(stderr, "%lf ", b[j * maxstep + i]);
    	}
    	  fprintf(stderr, "\n");
        }
      free(array);
      free(b);
      return 0;
    }

  14. #14
    Registered User
    Join Date
    Aug 2006
    Posts
    64
    *bump*

    help

  15. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    *bump* closed?

    Try using the debugger (or lots of printf statements) to follow the value of nstep just before you use it to index the array.
    I think you'll find it gets to be a lot bigger than you planned for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Replies: 6
    Last Post: 02-15-2005, 10:20 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM
  5. From stream/file to a string array problem
    By dradsws in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 06:24 PM

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