c - 二维数组引用整个索引,而不是 C 中的单个单元格

标签 c debugging multidimensional-array

我似乎无法弄清楚我的问题是什么。我正在编写一个 2D 随机游走模拟,我的想法是使用 2D 数组来模拟它所在的网格。但是,在我的代码中,当我尝试引用数组中的特定单元格以增加其值(例如 bin[3][4])时,它会增加整个索引的值,即 bin[1][4],bin[ 2][4],bin[3][4]等。关于如何解决这个问题或我做错了什么有什么想法吗?谢谢。

#include <stdio.h>
#include <math.h>

#define total 10.  /*total iterations*/
#define walk 5  /*total # of steps in a walk*/
#define lambda 1.0 /*step size*/
#define binsize 0.1
/*current chosen values are for bug checking simplicity*/

main()
{
  float range = walk*lambda*2.; /*2 times max range, all positive*/
  int n,prob,i,k,j,placex,placey,bins;
  double valuex,valuey, a; /*value starts at half range so all values are +*/
  bins=(range/binsize);

  int bin[bins][bins];
  for(i=0;i<=bins;i++) /*zero out bin*/
  {
    for(j=0;j<=bins;j++)
    {
      bin[i][j]=0;
    }
  }

  for(k=0;k<total;k++)
  {
    valuex=range/2.;
    valuey=range/2.;

    for(n=1;n<=walk;n++)
    {
      prob= rand(4) % 100+1;
      if(prob<=25)
      {
        valuex=valuex+pow(lambda,n);
      }
      else if(prob>25 && prob<=50)
      {
        valuex=valuex-pow(lambda,n);
      }
      else if(prob>50 && prob<=75)
      {
        valuey=valuey+pow(lambda,n);
      }
      else if(prob>75)
      {
        valuey=valuey-pow(lambda,n);
      }
    }   

    placex=floor(valuex/binsize+0.5);
    placey=floor(valuey/binsize+0.5);

    bin[placex][placey]=++bin[placex][placey];

    printf("%d %d %d\n",placex,placey,bin[placex][placey]); /* for bug checking. it prints the bin numbers where the value should go and then the value of that element.*/
  }

  for(i=0;i<=bins;i++)
  {
    for(j=0;j<=bins;j++)
    {
      a=bin[i][j]/total;
      //printf("%lf %lf %lf\n",i*binsize-range/2.,j*binsize-range/2.,a); /*format for input into IGOR*/
    }
  }
}

最佳答案

想通了。您正在超出数组范围。您的 for 循环会上升到 bin,但数组的索引从 0 到 bin -1。当我在 for 循环中从 <= 中取出 = 时,它就起作用了。我的最终代码如下所示。一般来说,超出数组范围会产生不可预测的行为。

#include <stdio.h>
#include <math.h>

#define total 10.  /*total iterations*/
#define walk 5  /*total # of steps in a walk*/
#define lambda 1.0 /*step size*/
#define binsize 0.1
/*current chosen values are for bug checking simplicity*/

main()     {
   float range = walk * lambda * 2.; /*2 times max range, all positive*/
   int n, prob, i, k, j, placex, placey, bins;
   double valuex, valuey, a; /*value starts at half range so all values are +*/

   bins = (range / binsize);

   int bin[bins][bins];

   /*zero out bin*/
   for(i = 0; i < bins; i++) {
      for(j = 0; j < bins; j++) 
         bin[i][j] = 0;
   }

   for(k = 0; k < total; k++) {
      valuex = range / 2. - 1;
      valuey = range / 2. - 1;

      for(n = 1; n <= walk; n++) {
         prob = rand(4) % 100 + 1;
         if(prob<=25)
            valuex += pow(lambda, n);
         else if(prob <= 50)
            valuex -= pow(lambda, n);
         else if(prob <= 75)
            valuey += pow(lambda, n);
         else 
            valuey -= pow(lambda, n);
      }   

      placex = floor(valuex / binsize + 0.5); 
      placey = floor(valuey / binsize + 0.5); 
      bin[placex][placey] += 1;

      printf("%d %d %d\n", placex, placey, bin[placex][placey]); /* for bug checking. it prints the bin numbers where the value should go and then the value of that element.*/
   }

   for(i = 0; i < bins; i++) {
    printf("\n");
      for(j = 0 ;j < bins; j++) {
        if (bin[i][j] == 0)
        printf("  ");
        else
        printf("%d ", bin[i][j]);
         a = bin[i][j] / total;

       }
    }
}

我在底部添加的 for 循环只是输出模式。

关于c - 二维数组引用整个索引,而不是 C 中的单个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16071150/

相关文章:

c++ - 什么时候修改链表?

.NET 调试 - System.Threading.ExecutionContext.runTryCode

javascript - 如何从 <function scope >'s Closure in Chrome Developer tool' s Watch 面板访问值?

php - 从动态多维表单获取

c - 替代密码打印表

c - 不会读数学方程式。为什么是这样?

c - 分配和分配 : different memory size allocated

c# - 如何调试 C# 命令行程序

PHP - 如何以任何顺序测试多维数组中的重复元素值

c++ - 在 C++ 中使用二维数组进行循环