C program doesn't print

273 views Asked by At

I have a bit of an issue to print a variable. Here is my code :

int main(int argc, char *argv[]){
  int N = 10, i = 0, n = 10;
  double v[2] = {0.0,0.0};
  double meanS2 = 0.0, sum = 0.0;
  time_t t;

  srand((unsigned) time(&t));

  for(i=0;i<n;i++){
    RW(N, v);
    meanS2 += v[0]*v[1];
    sum += v[1];
  }

  meanS2 = meanS2/sum;


  printf("%f\n", meanS2);
  fflush(stdout);

  return(0);
}

RW is a function that works (I tried it independently). When I run the program it gives me nothing. But really nothing! As you can see I tried to put the fflush function but it stilll gives me nothing. From what I've read the problems with printf usually arise when there is an infinite loop in the program, but my loop is far from being infinite! I just really don't understand. I tried putting :

RW(N, v);
meanS2 += v[0]*v[1];
sum += v[1];

ten times in a row but it gives me nothing, actually it works when I put it two times, but stops working at three times. Does it mean that my RW function overflows the memory or something? I'm a beginner so I'm really lost right now. If there was a problem like that, wouldn't there be a message or something?

Thanks for any kind of help you can provide!

Here is the rest of the program just in case, it's supposed to be a simulation for a random walk with survival biasing :

#define XY(x,y) ((x)*(2*N+1) + (y))


double product(int a, int b) // This function calculate the "scalar product" of two "vectors";
{                         // a and b are the vectors giving the directions taken by the walk
   double res=0;             // +1 and -1 represent going up and down respectively and  
                      // +2 and -2 represent going right and left respectively
    if(a == b)             // if both of the vectors point in the same direction, the product is 1
   {                      // since the vectors are of length 1
     res = 1.0;             // if they point in opposite directions, the result is -1 and 
   }                      // if they point in different direction, the result is 0
   else if(a == -b)
   {
     res = -1.0;
   }
   return res;
}

void RW(int N, double *v)
{

  int i, j, step, i_diff, j_diff, p, q, nspots, k, r;
  int *lattice, *index_i, *index_j;
  int xi;
  int dir_i[3], dir_j[3], vec_dir[3];
  int *vec;
  double S2 = 0;
  double w;

  lattice = malloc((2*N+1)*(2*N+1)*sizeof(int)); // A lattice big enough so that if the walk goes
  index_i = malloc((N+1)*sizeof(int));           // always in the same direction, it stops at the  
  index_j = malloc((N+1)*sizeof(int));           // edge
  vec = malloc(N*sizeof(int)); // index_i and index_j keep tracks of the indices of the different spots the walk goes by, vec keeps tracks of the different directions corresponding (up (1), down (-1), right (2), left (-2))   

  i = N + 1; // start at the center of the lattice
  j = N + 1;
  step = 0;
  srand (time(NULL));

  index_i[step] = i;
  index_j[step] = j;


  lattice[XY(i,j)] = 1; // 1 means that the walk passed here

  xi = rand() % 4 + 1; //For the first step, we take a random number between 1,2,3 and 4

  step = 1;
  switch(xi)
  {
    case 1:// if 1, the walk goes down
       i++;
       index_i[step] = i;
       index_j[step] = j;
       lattice[XY(i,j)] = 1;
       vec[step-1] = -1;
    break;
    case 2:// if 2, the walk goes up
       i--;
       index_i[step] = i;
       index_j[step] = j;
       lattice[XY(i,j)] = 1;
       vec[step-1] = 1;
    break;
    case 3:// if 3, the walk goes right
       j++;
       index_i[step] = i;
       index_j[step] = j;
       lattice[XY(i,j)] = 1;
       vec[step-1] = 2;
    break;
    case 4:// if 4, the walk goes left
        j--;
       index_i[step] = i;
       index_j[step] = j;
       vec[step-1] = -2;
    break;
  }

  for(step = 2; step<N+1; step++)
  {
    nspots = 0;

    i_diff = i - index_i[step-2]; // difference between the indices we are at right now, and those
    j_diff = j - index_j[step-2]; // from one step before

    if(i_diff != 0){//dir_i and dir_j represent the indices of the available spots in each case, vec_dir the available direction corresponding
      dir_i[0] = i + i_diff;
      dir_j[0] = j;
      vec_dir[0] = -i_diff;
      dir_i[1] = i;
      dir_j[1] = j - 1;
      vec_dir[1] = -2;
      dir_i[2] = i;
      dir_j[2] = j + 1;
      vec_dir[2] = 2;
      for(r=0; r<3; r++){
        if(lattice[XY(dir_i[r],dir_j[r])] == 0){
          dir_i[nspots] = dir_i[r];
          dir_j[nspots] = dir_j[r];
          vec_dir[nspots] = vec_dir[r];
          nspots++;
        } 
      }
      if(nspots == 0){// program aborts if there is nowhere to go
        v[0] = 0;
        exit(0);
      }   
    }

    else if(j_diff != 0){
      dir_i[0] = i;
      dir_j[0] = j + j_diff;
      vec_dir[0] = 2*j_diff;
      dir_i[1] = i + 1;
      dir_j[1] = j;
      vec_dir[1] = -1;
      dir_i[2] = i - 1;
      dir_j[2] = j;
      vec_dir[2] = 1;
      for(r=0; r<3; r++){
        if(lattice[XY(dir_i[r],dir_j[r])] == 0){
          dir_i[nspots] = dir_i[r];
          dir_j[nspots] = dir_j[r];
          vec_dir[nspots] = vec_dir[r];
          nspots++;
        } 
      }
      if(nspots == 0){
        v[0] = 0;
        exit(0);
      }  
    }


    w = w*nspots/3;
    xi = rand() % nspots;//there are only nspots directions to chose from (no going back)  
    i = dir_i[xi];
    j = dir_j[xi];
    lattice[XY(i,j)] = 1;

    index_i[step] = i;
    index_j[step] = j;

    vec[step-1] = vec_dir[xi];

  }
  // if the walk is completed, calculate S2, the total distance squared
  for(p=0; p<N; p++)
  {
    for(q=0; q<N; q++)
    {
      S2 += product(vec[p],vec[q]);
    } 
  }

  free(lattice);
  free(index_i);
  free(index_j);
  free(vec);
  v[0] = S2;
  v[1] = w;
}
0

There are 0 answers