Thread: function that compares exact and inexact matches for two character arrays

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    7

    function that compares exact and inexact matches for two character arrays

    I'm writing a function that compares two different character arrays of 3 to 6 dimensions for exact and inexact matches. It returns the number of exact and inexact matches via output parameters.
    For some reason I'm getting huge numbers for the matches, can you help me figure out what is wrong?
    Code:
    int calcMatches(char answer[], int dim, Guess guess, int *exact, int *inexact)
    {
    	int j, count = 0, decount = 0;
    	/*inexact matches algorithm*/
    	if(dim == 3)
    	{
    		if(answer[0] == guess.letters[1]) /*first term*/
    			*inexact = count + 1;                     /*first term*/ 
    		else if(answer[0] == guess.letters[2]) /*first term*/
    			*inexact = count + 1;                     /*first term*/ 
    		
    		else if(answer[1] == guess.letters[0]) /*2nd term*/
    			*inexact = count+1;                    /*2nd term*/
    		else if(answer[1] == guess.letters[2]) /*2nd term*/
    			*inexact = count+1;                    /*2nd term*/
    			
    		else if(answer[2] == guess.letters[0])
    			*inexact = count + 1;
    		else if(answer[2] == guess.letters[1])
    			*inexact = count+1;	
    	}
    	
    	if(dim == 4)
    	{
    		if(answer[0] == guess.letters[1]) /*first term*/
    			*inexact = count + 1;                    /*first term*/ 
    		if(answer[0] == guess.letters[2]) /*first term*/
    			*inexact = count + 1;                    /*first term*/ 
    		if(answer[0] == guess.letters[3])
    			*inexact = count + 1;
    		
    		if(answer[1] == guess.letters[0]) /*2nd term*/
    			*inexact = count + 1;                     /*2nd term*/
    		if(answer[1] == guess.letters[2]) /*2nd term*/
    			*inexact = count + 1;                     /*2nd term*/						
    		if(answer[1] == guess.letters[3])
    			*inexact = count + 1;
    		
    		if(answer[2] == guess.letters[0])
    			*inexact = count + 1;
    		if(answer[2] == guess.letters[1])
    			*inexact = count + 1;	
    		if(answer[2] == guess.letters[3])
    			*inexact = count + 1;
    			
    		for(j=0; j<(dim-1); j++)
    			if(answer[3] == guess.letters[j])
    				*inexact = count + 1;	
    	}
    		
    
    	if(dim == 5)
    	{
    		for(j=1; j<dim; j++)
    			if(answer[0] == guess.letters[j]) 		/*first term*/
    				*inexact = count + 1;
    	
    		if(answer[1] == guess.letters[0]) 			/*2nd term*/
    				*inexact = count + 1;
    		for(j=2; j<dim; j++)
    			if(answer[1] == guess. letters[j])
    				*inexact = count + 1;
    			
    		if(answer[2] == guess.letters[0])
    			*inexact = count + 1;
    		if(answer[2] == guess.letters[1])		/*3rd term*/
    			*inexact = count + 1;	
    		for(j=3; j<dim; j++)
    			if(answer[2] == guess.letters[j])
    			*inexact = count + 1;
    				
    		for(j=0; j<3; j++)
    			if(answer[3] == guess.letters[j])	/*4th term*/	
    				*inexact = count + 1;
    		if(answer[3] == guess.letters[4])
    			*inexact = count + 1;
    		
    		for(j=0; j<(4); j++)
    			if(answer[4] == guess.letters[j]) /*5th term*/
    				*inexact = count + 1;	
    	}
    	
    	
    	if(dim == 6)
    	{
    		for(j=1; j<dim; j++)
    			if(answer[0] == guess.letters[j]) 		/*first term*/
    				*inexact = count + 1;
    				
    		if(answer[1] == guess.letters[0]) 			/*2nd term*/
    				*inexact = count + 1;
    		for(j=2; j<dim; j++)
    			if(answer[1] == guess.letters[j])
    				*inexact = count + 1;
    				
    		if(answer[2] == guess.letters[0])
    			*inexact = count + 1;
    		if(answer[2] == guess.letters[1])		/*3rd term*/
    			*inexact = count + 1;	
    		for(j=3; j<dim; j++)
    			if(answer[2] == guess.letters[j])
    				*inexact = count + 1;
    				
    		for(j=0; j<(3); j++)
    			if(answer[3] == guess.letters[j])	/*4th term*/	
    				*inexact = count + 1;
    		if(answer[3] == guess.letters[4])
    			*inexact = count + 1;
    		if(answer[3] == guess.letters[5])
    			*inexact = count + 1;
    			
    		for(j=0; j<4; j++)
    			if(answer[4] == guess.letters[j]) /*5th term*/
    				*inexact = count + 1;
    		if(answer[4] == guess.letters[5])
    			*inexact = count + 1;
    			
    		for(j=0; j<5; j++)
    			if(answer[5] == guess.letters[j]) /*6th term*/
    				*inexact = count + 1;
    	}
    	
    
    	/*exact matches algorithm*/
    	for(j=0; j<dim; j++){
    		if(answer[j] == guess.letters[j])
    			*exact = decount + 1;	
    		}
    }
    [code]

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    It's in your best interest to provide a clear description of the problem so folks can understand what exactly you are trying to solve instead of the entire code with little or no explanation. Perhaps include an example of what the input, output should look like along with an explanation of the processing.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Based on this code, the only value *exact could have would be 1, as you assign to it "decount + 1", and decount is always zero. If the last bit of logic runs so that there are no matches, the *exact will never be updated, and will contain the value that it had before this routine was called, which may or may not be valid, depending on whether or not you initialized it.

    If it is the purpose of this routine to set *exact and *inexact, then you need to set them, not "possibly" set them.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed