# 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. ## 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?

2. Where are all of these variables defined? None of them has types. Why are you printing to stderr?

3. 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. What is this a formula for? How is it supposed to be used?

5. 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. 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.

7. 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. I guess I am slow, but I don't understand your question. Show me the exact code that isn't working.

9. 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. I guess, but I can't see the problem

11. 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. There we go!

13. 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. *bump*

help

15. *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.

Page 1 of 2 12 Last