Thread: Why is my program not replicating the desired output?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Why is my program not replicating the desired output?

    I've finally gotten my program to run and work somewhat but now I can't figure out why the output is so weird. For example, with the put shown below I manage to come up with all the correct possible key patterns but don't get them in order. That's really weird because the way i am solving this problem is by generating all the possible outcomes first and then filtering out the ones (using the bPegs and wPegs functions) that don't follow the criteria.


    This is the kind of output that I should get:

    Enter the pattern length: 3
    Input the guess pattern: abc
    Enter the number of black pegs in the feedback: 2
    Enter the number of white pegs in the feedback: 0
    The possible key patterns are:
    aacaba
    abb
    abd
    abe
    abf
    acc
    adc
    aec
    afc
    bbc
    cbc
    dbc
    ebc
    fbc



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    
    
    void userEnter(int*pattern, int n);
    void print( int * s, int n);
    void recurs( int * s, int * a, int n, int wpegs, int bpegs);
    bool Done (int*s, int n);
    bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n);
    bool wPegs(int* modcom, int* modoriginal, int*s, int wpegs, int w);
    void change(int*modoriginal, int*modcom, int i, int k, int w);
    
    
    int main(void)
    {
    	int i, n, bpegs, wpegs;
    	
    	printf("Enter the pattern length: ");
    	scanf("%d",&n);
    	int *a = (int*)malloc((n)*(sizeof(int)));
    	printf("Input the guess pattern: ");
    	int pattern[n];
    	userEnter(pattern, n);	
    	printf("Enter the number of black pegs in the feedback: ");
    	scanf("%d",&bpegs);
    	printf("Enter the number of white pegs in the feedback: ");
    	scanf("%d",&wpegs);
    	printf("The possible key patterns are: ");
    	for(i=0; i<=n-1; i++)
    	{
    		a[i]=0;
    	}
    	recurs(a, pattern, n, wpegs, bpegs);
    		
    }
    
    
    
    
    void userEnter(int*pattern, int n)
    {
        char input[n+1];
        scanf("%s", input);
    
    
        int i;
        for(i = 0; i < n; i++)
        {
            pattern[i] = input[i]-97;
        }
    }
     
    void print( int * s, int n)
    {
        int i; 
        printf( "\n" );
        for( i = 0; i <n; i++ )
    	{
            printf( "%c", ( s[ i ] + 97 ) );
        }
    }
    
    
    
    
    
    
    
    
    
    
    void recurs( int * s, int * a, int n, int wpegs, int bpegs)
    {
       
        int i;
    
    
        if(Done(s, n))
    	{
            return;/*print( s, n);*/
           
        }
    
    
        else{
            s[ 0 ] += 1;
            for( i = 0; i < n; i++ )
    		{
                if( s[ i ] == 6 )
    			{
                    s[ i ] = 0;
                    s[ i + 1 ] += 1;
                }
            }
            if(bPegs(a ,s, bpegs, wpegs, n))
            {
            print( s, n);
        	}
        	
    		recurs(s, a, n, wpegs, bpegs);
        }
    }
    
    
    bool Done (int*s, int n)
        {
        	int i;
        	bool done=true;
        	for (i=0;i<n;i++)
        	{
        		if(s[i]!=5)
        		{
        			done=false;
        		}
        	}
        	return done;
        }
        
        
    bool bPegs(int*a ,int*s, int bpegs, int wpegs, int n)
    {
    	int i,j,c=0;
    	bool d = false;
    	for(i=0; i<n; i++)
    	{
    		if(a[i]==s[i])
    		{
    			c++;
    		}
    	}
    	int x =n-c;
    	int* modcom; 
    	int*modoriginal;
    	modcom=(int*)malloc((x)*(sizeof(int)));
    	modoriginal=(int*)malloc((x)*(sizeof(int)));
    	int w=0;
    	for(j=0; j<n-1; j++)
    	{
    		if(a[j]!=s[j])
    		{
    			modcom[w]=s[j];
    			modoriginal[w]=a[j];
    			w++;
    		}		
    	}
    	if(c==bpegs)
    	{
    		d = wPegs(modcom, modoriginal, s, wpegs, w);
    	}
    	free (modcom);
    	free (modoriginal);
    	return d;
    	
    }
    
    
    bool wPegs(int*modcom, int*modoriginal, int*s, int wpegs, int w)
    {
    	int i, k, count=0;
    	for(i=0; i<w; i++)
    	{
    		for(k=0; k<w; k++)
    		{
    			if (modoriginal[i]==modcom[k])
    			{
    				count++;
    				change(modoriginal, modcom, i, k, w);
    			}
    		}
    	}
    	
    	if(wpegs==count)
    	{
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    	
    }
    
    
    void change(int*modoriginal, int*modcom, int i, int k, int w)
    {
    	int c, o;
    	for(c=i-1; c<w-1; c++)
    	{
    		modoriginal[c]=modoriginal[c+1];
    	}
    	for(o=k-1;o<w-1;o++)
    	{
    		modcom[o]=modcom[o+1];
    	}
    }

  2. #2
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Two threads for the same problem?

    Why is my program not replicating the desired output?
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    11

    Smile

    Quote Originally Posted by cfanatic View Post
    Yes it is the same program but the problem is now different. In the last thread I was having trouble running the program. Now I the problem is with output. I believe the title I chose for both threads is best fit for this one. If I could, I would change the title of the previous thread to something like "Problem running recursive program".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is my program not replicating the desired output?
    By newbie2012 in forum C Programming
    Replies: 5
    Last Post: 11-16-2012, 11:53 AM
  2. Replies: 18
    Last Post: 08-31-2012, 10:17 AM
  3. Not Getting Desired Output
    By DevoAjit in forum C Programming
    Replies: 2
    Last Post: 03-09-2012, 05:46 AM
  4. Replies: 3
    Last Post: 01-08-2004, 09:43 PM
  5. not getting the desired output. WHY???
    By KristTlove in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2003, 02:08 PM

Tags for this Thread