本规范我不知道为什么不工作。 [英] This Code I do't know why not working .

查看:69
本文介绍了本规范我不知道为什么不工作。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么至少图表显示G [2] [4],G [3] [4],G [4] [1] == 0 ????我已经初始化那些是1。比起为什么??



Why at least graph is Showing G[2][4],G[3][4] , G[4][1] == 0 ???? i have made initialization of those to be 1 . Than why ??

#include <stdio.h>

typedef int bool;
#define true 1
#define false 0 

#define N 4
#define E 5


//Have to check for three condition to be met for Hamiltonian circuit

//1.If has Verrtex >= 3
//2.Degree of every vertex is at least n/2
//3.deg u + deg v >= No of vertex 


void main () {
   _Bool G[N][N];
   int d[N];
   unsigned int i =0; 
   unsigned int j=0;

//Initialise all the content 
for(i=1;i<=N;i++){
 for(j=1;j<=N;j++){
   G[i][j] = false;
   }
}



 G[1][2] =  true;
 G[2][3] = true;
 G[4][1] = true;
 G[3][4] =  true;
 G[2][4] =  true;

//Represent the graph Given
 
//Initalise the degree of all possible combination
for(i=1 ; i<= N ; i++) {
         d[i] = 0 ;
}

//Build the degree of each vertices 

for (i=1; i<=N ; i++) {
  for (j=1 ; j <=N ; j++) {
    
    //seprate out self looping positions     
    if (i != j){
        if (G[i][j] == true){
                    d[i] = d[i] + 1;
                    d[j] = d[j] + 1;
          }

        }
    }
}

//Condition 1 : Graph has at least 3 Vertices
_Bool C1;
C1 = (N >= 3) ? 1 : 0;

//Condition 2 : degree of every vertices at least N/2
_Bool C2 = true;

for (i=1; i<=N ; i++) {
  if(d[i] >= N/2) {
      C2 = C2 && true ;
      } 
  else {
      C2 = C2 && false ;
     }

}

//Condition 3 : if adject exist no prob 
//if both are non adjecent then deg u + degv >= N

//If the Graph is hamiltonian we have to check adding any adjecent edge 
// Will stay that at hamiltonian if they are not adjecent we'll check for the 
//the degree addition is greater than or equal to N .

_Bool C3 = true;

for (i=1;i<= N;i++) {
    for(j=1;j<=N; j++){
         if (i!=j){
           if (G[i][j] == true) {
                  C3 = C3 && true;
              }
           else {
                     if( d[i] + d[j] >= N) {
                                C3 = C3 && true;
                           }
                     else {
                                 C3 = C3 && false;
                                 break;
                     }
              }
          }
       }
    }


for (i=1;i<= N;i++) {
    for (j=1;j<= N;j++) {
            printf("\nThe value of G [%d] [%d] is : %d" , i, j, G[i][j]);
         }
     }


 printf("\nThe value of C1  is : %d" , C1);

 printf("\nThe value of C2  is : %d" , C2);

 printf("\nThe value of C3  is : %d" , C3);

 __CPROVER_assert( !(C1 && C2 && C3) , "Hamiltonian path exists");

}

推荐答案

首先,你的代码忽略了C数组基于0的事实。 br />


因此,要么修复所有索引和循环大小,要么在每个方向分配一个项目(并且不要使用索引0)。



如果您已经在某处复制了算法表单并且使用的是使用基于1的数组的语言编码,如果您不太了解算法,则可能更容易调整分配的大小。



另一方面,如果您自己编写代码,通常会使用基于0的索引,除非索引本身意味着什么并且您更喜欢使用这些基于1的索引。例如,这些在矩阵的数学中很常见。



G [2] [4]例如不是有效的数组索引,因为N是4.有效索引是0 ,1,2和3.



在运行时不会直接检测到这种错误,但可能导致所有类型的未定义行为,因为它可以写入过去分配的内存用于其他东西的变量。在最坏的情况下,它会覆盖返回地址,这将导致未定义的行为。类似地,它可以覆盖指针的地址,如果该对象有虚拟表,则可能发生灾难性行为。



所以最后,你要么必须学习C语言或使用更友好的语言,如C#,在这种情况下会引发错误。
For one thing, your code ignore the fact that C array are 0 based.

Thus either you fix all indexes and loop size or you allocate one more item in each directions (and don't use index 0).

If you have copied the algorithm form somewhere and it was coded in a language that uses 1-based array, it might be simpler to adjust the allocated size particulary if you don't understand well the algorithm.

On the other hand, if you wrote the code yourself, usually, you would uses 0-based indexes except maybe in cases where index themselves mean something and you prefer to uses those 1-based index. Those are common in mathematics for matrices for example.

G[2][4] for example is not a valid array index as N is 4. Valid index are 0, 1, 2, and 3.

That kind of error are not directly detected at run time but can cause all sort of undefined behavior as it can write past allocated memory on a variable used for something else. In the worst case, it would overwrite the return address which will really cause undefined behavior. Similary, it can overwrite the address of a pointer and if that object has a virtual table, then catastrophic behavior can occurs.

So in the end, you either have to learn the C language or use a more friendly language like C# that would raise an error in such case.


这篇关于本规范我不知道为什么不工作。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆