-1

Liechtenstein in c programming always return infinite loop this is my code i try many solution and i try to stock variables and use pointers but always i have the infinite loop i think it's because the 3 recursive calls but in the doc of Liechtenstein algorithm i found this implementation

#include "distance.h"
#include "sequence.h"
#include "string.h"
#include <stdarg.h>
int max(int a, int b){
    if (a>b) return a;
    return b;
}

float min(float a,float b, float c){
    if((a<b) && (a<c)) return a;
    if((c<b) && (c<a)) return c;
    return b;
}

float dif(int a,int b){
    int d;
    d = a-b;
    if((a==32)||(b==32)) return 1.5;  //verification d'espaceC

    if(d==0.0) return d;
    if((abs(d)==17.0) || (abs(d)==6.0)) return 1.0;
    return 2;
}

float distance_D1(SEQUENCE S1, SEQUENCE S2){ 
    float d = 0.0; int a,b; int m; int i;
    m = max(S1.l,S2.l);
    for(i=0;i < m; i++){
        a = S1.tc[i];
        b = S2.tc[i];
        d = d + dif(a,b) ;
        printf("%.1f\n",d);
    }
    return d;
}



float DistanceMinRec(char* a,char* b,int n,int m){
 printf("%s \n",a);
  printf("%s \n",b);
  printf("%d \n",n);
  printf("%d \n",m);
 float l,k,j,d;

       if (m <= 0) return n; 

    // If second string is empty, the only option is to 
    // remove all characters of first string 
    if (n <= 0) return m; 

    // If last characters of two strings are same, nothing 
    // much to do. Ignore last characters and get count for 
    // remaining strings. 

    if (a[m] == b[n]) {
        return DistanceMinRec(a, b, m-1, n-1); 
  }
    // If last characters are not same, consider all three 
    // operations on last character of first string, recursively 
    // compute minimum cost for all three operations and take 
    // minimum of three values. 
    l=DistanceMinRec(a,  b, m, n-1)+dif(a[m],b[n-1]);
    k=DistanceMinRec(a,  b, m-1, n)+dif(a[m-1],b[n]);
    j=DistanceMinRec(a,  b, m-1, n-1)+dif(a[m-1],b[n-1]);
    d= min (l ,    // Insert 
                     k,   // Remove 
                   j   // Replace 
                   );
                   return d; 
}

the main file

int n=strlen(S1.tc);
    int m=strlen(S2.tc);
    char* X = S1.tc;  
    char* Y = S2.tc;
    // char* X = "sunday";  
    // char* Y =  "saturday";

    //affiche_sequence(S1);
    //affiche_sequence(S2);
    d = DistanceMinRec(X,Y,n,m);
    printf("Distance entre %s et %s = %.1f\n", argv[1],argv[2],d);

    exit(0);
zratan
  • 856
  • 8
  • 12
  • What is 32, 16, 17, 6 and 1.5? None of that makes sense. In fact, the call to `dif` should be replaced with `1`. – ikegami Nov 14 '19 at 02:47
  • Checking if a floating point number is exactly equal to another is rarely going to work out as intended. You need another approach such as checking if they are within a tolerance. In your case, it works because the only fraction with which you are dealing is 1/2. But that also means you could have simple multiplied all values by 2, then divided the result by 2 at the end. – ikegami Nov 14 '19 at 02:50
  • `char* a, char* b, int n, int m` should be `const char* a, const char* b, size_t n, size_t m`. There's no reason to expect the strings to be modifable, and `int` is not the appropriate type for the size of a string. – ikegami Nov 14 '19 at 02:51

1 Answers1

0

i think that the flush of variable is the problem i comment the block of printf in recursive function and it work but im not sure that is the correct output

zratan
  • 856
  • 8
  • 12