0

I'm trying to use a multi-dimensional array to store data, however, some of the indexes of the array don't seem to be working right. I get correct data for most of the elements but a portion of them will all read zero even though the values that were read in are not zero. The array ends up looking something like this:

-3.238467  -3.237679  -0.487128  -3.237634  -3.238513  -3.239868  
-3.239361  -3.238660  0.000000  -3.238610  -3.435949  -3.437109  
-3.622402  -3.621796  0.000000  -3.621911  -3.436662  -3.437587  
-3.237072  -3.236771  0.000000  -3.237329  -3.237926  -3.238605  
-3.227221  -3.227291  0.000000  -3.228477  -3.229173  -3.229775  
-3.204790  -3.205429  0.000000  -3.207087  -3.207956  -3.208255  
-2.618961  -2.621088  -2.622552  -2.623831  -2.624973  -2.625057

Even if I check the values like this:

for(j=0; j<(num_cell_y); j++) {
    for(i=0; i<(num_cell_x); i++) {
      ...
      ...
      ...
      grid[i][j] = u_avg;
      printf("%f\n", u_avg);
      printf("%f\n", grid[i][j]);
    }
}

I get two different values printed out:

-3.237675
0.000000

Has anyone else had this happen to them or know of something I may have missed that is causing this?

4

2 に答える 2

5

この順序で配列を埋める必要があるようです-

grid[j][i] = u_avg;  // Note the indexes being reversed.
于 2012-09-13T02:53:48.187 に答える
1

これがあるべき順序に誤りがあります

grid[j][i] = u_avg;

ご注文の状況grid[i][j] = u_avg;

j は外側のループにあるため、一度 i まで修正されi<(num_cell_x);、配列は多次元であり、コードによれば、外側の配列を次のように変更します

i    j
a[0][0]=>
a[1][0]=>
a[3][0]=>

これは正しくないので、j よりも i を実行すると

j    1
a[0][2]=>array(1=>'',2=>'',...)
a[1][0]=>
a[3][0]=>
于 2012-09-13T03:04:51.183 に答える