1

This is my code

private double wordcompare(word a, word b)
{    
     if (a.AtTheEndOf(b))
         return -1;
     double matchPer=word.Compare( a,b);
     Console.WriteLine(matchPer);
     a.shiftToleft();
     double nextMatchPer = wordcompare(a, b);
     return matchPer > nextMatchPer ? matchPer : nextMatchPer;
}

the Compare() method compare 2 words and return a percentage of match letters the shifttoleft() method move the compared word one step virtually, I have been debugged every step when it arrives to the return of Compare() there is numbers like 2/3 or 1/3 but when but when i print the returned value it shows 0 except the first call help please.

word class

class word
{
    public static double Pertolarnce = 0.2;

    private int pos;
    public int wordTolarnce;
    string letters;

    public int Pos {  get{ return pos; }  }
    public word(string s)
    {
        letters = s;
        wordTolarnce=(int)Math.Round(Pertolarnce * s.Length, MidpointRounding.AwayFromZero) ;
        pos = 0 - wordTolarnce;
    }


    public void shiftToleft()
    {
        pos++;
    }

    public int Len { get { return letters.Length; } }

    public static double Compare(word a,word w)
    {
        int comLeters = 0;
        int matchedLeters = 0;
        int i, j;
        i = 0 - a.pos;
        j = 0 + a.pos;
        i=i < 0 ? 0 : i;
        j=j < 0 ? 0 : j;
        while(i<a.Len && j<w.Len )
        {
            comLeters++;
            if (a.letters[i] == w.leterAt(j))
                matchedLeters++;
            i++;
            j++;
        }
        return matchedLeters / comLeters;
    }

    private char leterAt(int index)
    {
        return letters[index];
    }

    public bool AtTheEndOf(word w)
    {
        if(w.Len-this.pos+1 == this.Len-wordTolarnce)
        return true;
        else
            return false;
    }
}

output of

    Console.WriteLine(matchPer);
    1
    0
    0
    0
    0
    0
3
  • It would be awesome if you could provide a minimal reproducible example with sample inputs and expected results for those sample inputs. Commented Feb 23, 2018 at 10:07
  • 1
    Please provide a minimal reproducible example. We don't know what shiftToLeft does, or wordcompare, or how you're calling the method, or what result you expect, or what result you get. I'd also very strongly advise you to start following .NET naming conventions. Commented Feb 23, 2018 at 10:08
  • 1
    Your wordcompare method appears to change the a object, this is really not a good idea. A comparison method should ideally be pure, ie. it takes input, it returns a result based on that input, and doesn't modify anything. Commented Feb 23, 2018 at 10:33

1 Answer 1

1

Your problem is integer division. matchedLeters / comLeters will perform integer division which means that any remainder is discarded. That means that if matcheLeters is less than comLeters that you will get 0 returned as you saw.

To fix this you can do the following:

return (double)matchedLeters / comLeters;

This will cast matchedLeters to a double before the division which will force it to use floating point division instead.

As a debugging tip you might have got more of an idea of the problem if you'd done (for debugging):

var retVal = matchedLeters/comLeters;
return retVal;

This would have allowed you to see exactly what operation was causing you the problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.