I don't know whats wrong with this

This is a discussion on I don't know whats wrong with this within the C Programming forums, part of the General Programming Boards category; I've been working at this code for several days trying to work out Kaprekars constant (see 6174 (number) - Wikipedia, ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    7

    I don't know whats wrong with this

    I've been working at this code for several days trying to work out Kaprekars constant (see 6174 (number) - Wikipedia, the free encyclopedia for an explanation) and I'm almost there, but the program is not spitting out what I'd like it too.

    from what I can tell the code seems correct and I've been going over it all day trying to figure out what the problem is with no success

    I know that the problem is located in kaprekar, but I can't find the specific tree within the forest.
    Here is the code

    Code:
    # include <stdio.h>
    int inputVariables[4]; //where userinput goes after being broken up
    int i,j;
    int userInput, remainders; //used to break up input
    int numberX = 0; // users input arranged forwards
    int numberY = 0; // users input arranged backwards
    
    
    //function declaration follows
    int sort(int i);
    int backsort(int numberX);
    int kaprekar(int userInput);
    
    
    main ()
    {
    userInput = 1000;
    kaprekar(userInput);
    }
    
    
    int sort( int userInput)
    {
    
    
    remainders = userInput % 1000;
    inputVariables[0] = userInput / 1000;
    userInput = remainders;
    
    
    remainders = userInput % 100;
    inputVariables[1] = userInput / 100;
    userInput = remainders;
    
    
    remainders =userInput % 10;
    inputVariables[2] = userInput / 10;
    userInput = remainders;
    inputVariables[3] = remainders;
    
    
    for (i = 0; i < 3; ++i)
    	{
    	for (j = 0; (j < 4 - 1 - i); ++j)
    		{
    			if (inputVariables[j] > inputVariables[j+1])
    			{
    				remainders = inputVariables[j+1];
    				inputVariables[j+1] = inputVariables[j];
    				inputVariables[j] = remainders;	
    			} 
    		}
    	}
     
    numberX = (inputVariables[0]*(1000))+(inputVariables[1]*(100))+(inputVariables[2]*(10))+(inputVariables[3]); 
    
    
    return numberX;
    	
    }
    
    
    int backsort(int numberX)
    {
    remainders = userInput % 1000;
    inputVariables[0] = userInput / 1000;
    userInput = remainders;
    
    
    remainders = userInput % 100;
    inputVariables[1] = userInput / 100;
    userInput = remainders;
    
    
    remainders =userInput % 10;
    inputVariables[2] = userInput / 10;
    userInput = remainders;
    inputVariables[3] = remainders;
    
    
    numberY = (inputVariables[0]*(1))+(inputVariables[1]*(10))+(inputVariables[2]*(100))+(inputVariables[3]*(1000)); 
    
    
    return numberY;
    }
    
    
    int kaprekar(int userinput)
    {
    
    
    for (i = 1000; i <=1001; i++)
    {
     
     sort(i);
     backsort(numberX);
      j = 1;
    	if (i % 20 == 0)
    	{
      printf("\f");
     }
    
    
    		do
    		{
    			if(j>7)
    			{break;}
    
    
    			if (numberX == 6174 | numberY ==6174)
    				{
    				printf("6174 \n");
    				}
    				do {
    								
    							if (numberX > numberY)
    							{
    							
    							userInput = numberX-numberY; 
    							printf ("%d \t ", userInput);
    							j++;
    							}
    							else 
    							{
    							userInput = numberX-numberY; 
    							printf ("%d \t ", userInput);
    							j++;
    							}
    								if (numberX == numberY)
    								{
    								break;
    								}
    								
    				} while ((numberY > numberX)| (numberX > numberY));
    			
    				
    			
    			
    				
    		if (userinput != 6174)
    		{		
    		sort(userInput);
    		backsort(numberX);
    		j++;
    		}
    		
    		}while (userinput != 6174);
    		
    if (numberX == numberY)
    {continue;}
    }
    }
    please keep in mind that program currently loops indefinitely

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    Well first of all, line 134 has an error, because | should not be a valid C operator. You were probably wanting to use the logical OR operator, which is ||. Also, try making your functions in a better order. Sort should be in front of kaprekar because it is called 2nd, and then you would need the last function at the end, since it would make sense if they were organized from top to bottom.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    55
    Well never mind... I forgot that | is bitwise OR, I still think you should use logical OR though.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Fear_Impurity View Post
    from what I can tell the code seems correct and I've been going over it all day trying to figure out what the problem is with no success

    I know that the problem is located in kaprekar, but I can't find the specific tree within the forest.
    One big problem is that your current code is a big mess (no consistent indentation, global variables) which makes it hard to read and to follow the flow. I'm personally not in the mood right now to dig into it.

    Just one tip: Use a debugger and set breakpoints at strategic lines (e.g. at the beginning of a loop) to find out where and why the values don't change as expected.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this??
    By Hugo716 in forum C++ Programming
    Replies: 5
    Last Post: 06-07-2006, 05:32 AM
  2. Whats wrong with my IRC Bot??
    By peradox in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-23-2005, 05:16 PM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-27-2005, 11:48 PM
  4. can you tell me whats wrong(C)
    By Oid21 in forum C Programming
    Replies: 2
    Last Post: 03-19-2003, 08:39 AM
  5. whats wrong here??
    By pancho in forum C Programming
    Replies: 3
    Last Post: 03-20-2002, 09:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21