C程序无法打印

时间:2015-06-09 11:44:22

标签: c printing

打印变量时遇到一些问题。这是我的代码:

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是一个有效的功能(我独立尝试过)。当我运行程序时它什么也没给我。但真的没什么!正如你所看到的,我试图把fflush功能放进去,但它仍然没有给我任何东西。从我所看到的,当程序中存在无限循环时,printf的问题通常会出现,但我的循环远非无限!我真的不明白。我试着把:

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

连续十次,但它没有给我任何东西,实际上当我把它放两次时它起作用,但是三次停止工作。这是否意味着我的RW功能溢出了内存或什么?我是初学者,所以我现在真的迷失了。如果有这样的问题,会不会有消息或什么?

感谢您提供的任何帮助!

这是程序的其余部分以防万一,它应该是一个模拟随机游走与生存偏差:

#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 个答案:

没有答案