2012-03-23 110 views
0

!注:我有两个变量具有相同的名称......非常感谢斯特凡Birladeanu和亨里克的注意到,*访问冲突写入位置...错误在哪里? (图维奇)

最近我开始写代码,可以帮助我输入布尔值函数给Veitch(卡诺)4个变量的图表。该代码应写元件,以矩阵尺寸4×4,但是与这些索引:

  1. 元件 - 索引3,3
  2. 元件 - 索引2,3
  3. 元件 - 索引3,2
  4. 元件 - 指数2,2-
  5. 元件 - 索引0,3
  6. 元件 - 索引1,3
  7. 元件 - 索引0,2
  8. 元件 - IND离1,2-
  9. 元件 - 索引3,0
  10. 元件 - 索引2,0
  11. 元件 - 索引3,1
  12. 元件 - 索引2,1
  13. 元件 - 索引0,0
  14. 元件 - 索引1,0
  15. 元件 - 索引0,1
  16. 元件 - 索引1,1 这是代码的main():

    void main() 
        { 
         int n; 
    
        n=4; 
    
        int **VeitchDiagram; 
    
        //allocate memory for Veitch diagram 
        VeitchDiagram = new int *[n]; 
        for(int i=0; i<n; i++) 
         VeitchDiagram[i]=new int [n]; 
    
        //enter the elements 
        for(int i=0; i<n; i++) 
        { 
         int j, k; 
         if(i%2==1) 
         { 
          k=0; 
          if(i<2) 
           j=4; 
          else 
           j=-1; 
          for(int k=0; k<2; k++) 
          { 
           if(i<2) 
            j--; 
           else 
            j++; 
           cin >> VeitchDiagram[k][j];  //this part writes the input to elements with index (at least it should do that): 
           k++;       //0,3  1,3  0,2  1,2  if i%2==1 and i<2 
           cin >> VeitchDiagram[k][j];  //0,0  1,0  0,1  1,1  if i%2==1 and i>=2 
           k--; 
          } 
         } 
         else 
         { 
          k=3; 
          if(i<2) 
           j=4; 
          else 
           j=-1; 
          for(int k=0; k<2; k++) 
          { 
           if(i<2) 
            j--; 
           else 
            j++; 
           cin >> VeitchDiagram[k][j];  //this part writes the input to elements with index (at least it should do that): 
           k--;       //3,3  2,3  3,2  2,2 if i%2==0 and i<2 
           cin >> VeitchDiagram[k][j];  //3,0  2,0  3,1  2,1 if i%2==0 and i>=2 
           k++; 
          } 
         } 
        } 
    
        //free memory allocated for VeitchDiagram 
        for(int i=0; i<n; i++) 
         delete [] VeitchDiagram[i]; 
        delete [] VeitchDiagram; 
    } 
    
+0

你是否已经通过调试器完成了它? – 2012-03-23 14:12:02

+0

是...多次。它让我输入前两个元素,然后给我访问冲突。 – zkristic 2012-03-23 14:20:58

回答

2
 for(int k=0; k<2; k++) 
     { 
      if(i<2) 
       j--; 
      else 
       j++; 
      cin >> VeitchDiagram[k][j];  //this part writes the input to elements with index (at least it should do that): 
      k--;       //3,3  2,3  3,2  2,2 if i%2==0 and i<2 
      cin >> VeitchDiagram[k][j];  //3,0  2,0  3,1  2,1 if i%2==0 and i>=2 
           ^k == -1 

但你真的应该学习如何使用调试器。

+0

Ohhhh ...我有两个变量“K”...我刚刚注意到了! 谢谢你指出,是的,我应该学会使用调试器(更好):D 谢谢......并对不起你烦! – zkristic 2012-03-23 14:23:44

2

对于i = 0到达这个分支

else 
      { 
       k=3; 
       if(i<2) 
        j=4; 
       else 
        j=-1; 
       for(int k=0; k<2; k++) 
       { 
        if(i<2) 
         j--; 
        else 
         j++; 
        cin >> VeitchDiagram[k][j];  //this part writes the input to elements with index (at least it should do that): 
        k--;       //3,3  2,3  3,2  2,2 if i%2==0 and i<2 
        cin >> VeitchDiagram[k][j];  //3,0  2,0  3,1  2,1 if i%2==0 and i>=2 
        k++; 
       } 
      } 

当k = 0

cin >> VeitchDiagram[k /* = 0 OK */][j];  //this part writes the input to elements with index (at least it should do that): 
        k--; //decrease it       //3,3  2,3  3,2  2,2  if i%2==0 and i<2 
        cin >> VeitchDiagram[k /* here k = -1 BAD!!! */][j];  //3,0  2,0  3,1  2,1 if i%2==0 and i>=2 
        k++; 
1

如在别处所指出的,你索引阵列ouside。
正如一个建议,基于表格的版本可以少棘手得到正确的:

const size_t k_index[] = {3,2,3,2,0,1,0,1,3,2,3,2,0,1,0,1}; 
const size_t j_index[] = {3,3,2,2,3,3,2,2,0,0,1,1,0,0,1,1}; 

int main() 
{ 
    const int n = 4; 
    int VeitchDiagram[n][n]; // No need for dynamic allocation here. 

    //enter the elements 
    for(int i = 0; i < n * n; i++) 
    { 
     cin >> VeitchDiagram[k_index[i]][j_index[i]]; 
    } 
} 

这是一对夫妇更短的行也是如此。

+0

大声笑...“几行更短”:D 是的,这是(很多)更容易阅读!感谢您的代码 – zkristic 2012-03-23 17:30:21