SOURCE FILE: tryMatch.pcode


/* 
       What function tryMatch does:

       Attempt to match the client in position place of clientD[genderID]
       with a member of the opposite sex.  A match occurs if and when this
       function finds a previously unmatched client with three or more of the
       same interests.
    
       Two interests are "the same" if they are exactly equal as strings.

       This function only attempts to find the first person who matches the
       client.

       If a match is found this function designates both persons as matched,
       prints the name of the client, the name of his or her match, and both
       of their phone numbers.

       If no match is found, this function prints an appropriate message.  

       NOTE: in the version of tryMatch below, the pseudo code is
       between Pascal-style comment markers -- (* and *).

*/

        
void tryMatch(clientData clientD, int genderID, int place)
{

  int opSex = 1-genderID ;
  int listLength = (* length of the list clientD[opSex] *) ;
  int listPos = 0 ;
  clientItemType newbie, candidate ;

  (* retrieve "newbie" from position "place" in list clientD[genderID]. *)

  bool foundMatch = false ;

  while (!foundMatch && listPos < listLength)
 {
   listPos++ ;
  (* retrieve "candidate" from position "listPos" in list clientD[opSex]. *)
  if (isMatch(newbie, candidate)) foundMatch = true ;
 }

 if (foundMatch) 
   makeMatch(clientD, genderID, place, listPos) ;
 else (* output failure message *)
   
}

Notes:
  1. isMatch returns true if candidate is previously unmatched and has three or more interests that match newbie. Otherwise isMatch returns false.
  2. isMatch can work with a double for-loop, like this pseudo code:
       numMatches = 0 ;
       for (i = 1 upto length of newbie list)
           for (j = 1 upto length of candidate list)
               if (newbie interest #i == candidate interest #j) numMatches++ ;
       return (numMatches >= 3) ;
    
  3. makeMatch changes the list elements so that the person in position "place" of list clientD[genderID] is matched with the person in position "listPos" of clientD[1-genderID].

    makeMatch also prints the names and phone numbers of the matched pair.

    (To modify an element of a list, you need to retrieve it, change the retrieved copy, delete the element from the list, and insert it back into the same position it was in before.)