• C

explaining compname () algorithms

Dear Expert, especially to imladris who has implemented these following codes...

I just had a chance to have a closer look at all of your codes -Imladris...and i found out that 3 of your codes (3 files below) which u have implemented, are quiet different especially the output I got....
could you comment and explain each code  and please illustrate how they are different from each other....am a bit confused and it was even more when I got 3 differnt outputs from them..
p.s. please explain clearly especially how different of each codes....

/*file1 */
                  int compname(char *nm1,char *nm2)
                 {   int i,l1,l2,len;
                    float same,diff;

                    l1=strlen(nm1);
                    l2=strlen(nm2);
                     len=(l1>l2?l1:l2);
                     same=0.;
                     diff=0.;
                    for(i=0; i<len; ++i)
                    {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                        else diff+=1.;
                     }
                    return(same/(same+diff)>0.5);
                 }

                   int compchar(char a,char b)
                   {     return(toupper(a)==toupper(b));
                   }


--------------------
/*file2 */
                   float compname(char *nm1,char *nm2)
                   {   int i,j,k,l1,l2,len;
                     float same,diff;

                     same=diff=0.;
                     l1=strlen(nm1);
                     l2=strlen(nm2);
                     j=0;
                     for(i=0; i<l1; ++i)
                     {   if(compchar(nm1[i],nm2[j]))
                         {   same+=1.;
                             ++j;
                         }
                         else
                         {   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
                             if(k<l2)
                             {   same+=1.;
                                 diff+=(float)(k-j);
                                 j=k+1;
                             }
                         }
                     }
                     if(same==0. && diff==0.)return(0.);
                     return(same/(same+diff));
                   }

--------------------
/* file3 */
int compname(char *nm1,char *nm2)
                 {   int i,l1,l2,len;
                    float same,diff;

                    l1=strlen(nm1);
                    l2=strlen(nm2);
                     len=(l1>l2?l1:l2);
                     same=0.;
                     diff=0.;
                    for(i=0; i<len; ++i)
                    {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                        else diff+=1.;
                     }
                    return(same/(same+diff)>0.5);
                 }
-----------------------

korsilaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
imladrisConnect With a Mentor Commented:
Yes, these compnames are from when we started with different algorithms for determining whether names matched or not. I don't remember what the programs were called, but here is what they appear to do:

/*file1 */

// return true, indicating that nm1 matches nm2 if
// the number of identical letters, divided by total
// letters is greater than 1/2.
// This algorithm simply compares characters by position
// i.e. smith and smath have 4 same and 1 diff
// and  smith and smeeth have 2 same and 3 diff
                 int compname(char *nm1,char *nm2)
                {   int i,l1,l2,len;
                   float same,diff;

                   // get length of names
                   l1=strlen(nm1);
                   l2=strlen(nm2);

                    //use greatest length for loop
                    //initialize same and different
                    //letter counters
                    len=(l1>l2?l1:l2);
                    same=0.;
                    diff=0.;

                   //for each letter in the longest name
                   //  if both names have letters and letters
                   //      are the same, increment same
                   //  else increment diff
                   for(i=0; i<len; ++i)
                   {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                       else diff+=1.;
                    }

                   // return match if same/(same+diff) > 0.5
                   return(same/(same+diff)>0.5);
                }

                  //compare two characters, ignoring case
                  //  done by making both uppercase
                  int compchar(char a,char b)
                  {     return(toupper(a)==toupper(b));
                  }



/*file2 */
//return match value for nm1 and nm2 this compname
//This algorithm tracks the last matching letters
//i.e. smith and smath have 4 same and 1 diff
//and smith and smeeth also have 4 same and 2 diff
                  float compname(char *nm1,char *nm2)
                  {   int i,j,k,l1,l2,len;
                    float same,diff;

                    //initialize same and diff letter counters
                    //and nm lengths
                    //initialize j: last matched position
                    same=diff=0.;
                    l1=strlen(nm1);
                    l2=strlen(nm2);
                    j=0;

                    //for all letters in nm1
                    for(i=0; i<l1; ++i)
                    {   //check letters in nm1 versus nm2
                        //if they're the same
                        //   increment same and j
                        if(compchar(nm1[i],nm2[j]))
                        {   same+=1.;
                            ++j;
                        }

                        //else
                        //  compare current nm1 character
                        //  against all characters in nm2
                        //  from last matched position to end
                        //  if match found
                        //    increment same
                        //    increase diff by number of
                        //       letters skipped
                        //       set j to new last matched
                        else
                        {   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
                            if(k<l2)
                            {   same+=1.;
                                diff+=(float)(k-j);
                                j=k+1;
                            }
                        }
                    }

                    //if null case and both same and diff
                    //are 0, return match value of 0
                    //else return same / (same + diff)
                    if(same==0. && diff==0.)return(0.);
                    return(same/(same+diff));
                  }


/* file3 */
//This one looks the same as file1 to me
int compname(char *nm1,char *nm2)
                {   int i,l1,l2,len;
                   float same,diff;

                   l1=strlen(nm1);
                   l2=strlen(nm2);
                    len=(l1>l2?l1:l2);
                    same=0.;
                    diff=0.;
                   for(i=0; i<len; ++i)
                   {   if(i<l1 && i<l2 && compchar(nm1[i],nm2[i]))same+=1.;
                       else diff+=1.;
                    }
                   return(same/(same+diff)>0.5);
                }

0
 
korsilaAuthor Commented:
oops sory "file3" is look like this ..a bit different from "file2"

                float compname(char *nm1,char *nm2)
                 {   int i,j,k,l1,l2,len;
                   float same,diff;

                   same=diff=0.;
                   l1=strlen(nm1);
                   l2=strlen(nm2);
                   j=0;
                   for(i=0; i<l1; ++i)
                   {   if(compchar(nm1[i],nm2[j]))
                       {   same+=1.;
                           ++j;
                       }
                       else
                       {   for(k=j+1; k<l2 && !compchar(nm1[i],nm2[k]); ++k);
                           if(k<l2)
                           {   same+=1.;
                               diff+=(float)(k-j);
                               j=k+1;
                           }
                       }
                   }
                   if(j<l2)diff+=(float)(l2-j);----here the different from file 2
                   return(same/(same+diff));
                 }

0
 
korsilaAuthor Commented:
explain here a bit more when u get back from holiday...
  if(j<l2)diff+=(float)(l2-j);

thanxxxxxxxxxxxxxxx....
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
imladrisCommented:
Ah, j is the last matched letter. That line takes care of the case when a trailing set of letters in name2 produces no matches. In that case all the letters from j until the end of the string are different and should be added to diff.
0
 
korsilaAuthor Commented:
0
 
korsilaAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.