tags:

views:

85

answers:

3

Similar to: http://stackoverflow.com/questions/3250957/finding-the-largest-palindrome-of-the-product-of-two-three-digit-numbers-problem The IsPalindrome condition always is true.Please guide me to the error.

#include <stdio.h>
#include <malloc.h>
int IsPalindrome(int num);
int main()
{
    int i,j,temp;
    for(i=10;i<99;i++)
    {
        for(j=10;j<99;j++)
        {
            if(IsPalindrome(i*j))
            temp=i*j;
        }
    }
    printf("%d\n",temp);

    getch();
    return 0;
}
int IsPalindrome(int num)
{
    int x,i,j,flag;
    char p[50];
/*Convert the number into an array*/
/*Avoid p index 0 for simplicity*/
/*Converting number into array*/
    for(i=1;x==0;i++)
    {
        x=num%10;
        p[i]=x;
    }

/*Checking for palindrome,length of array is i*/
    for(j=0;j<i/2;j++)
    {
        if(*(p+j)!=*(p+i-j))
        flag=0;
    }


    return flag;
}
+1  A: 
  • One issue I can immediately see is flag is not initialized.
  • Since you want the largest palindromes its better to run the loops from 99 till 10 and stop when you find the first palindrome.
  • Your existing loop is not running till 99. You need a i<=99 and j<=99
  • In the part where you convert the number to char array, num is not being changed and the loop should continue till num is non-zero.

    for(i=1;num;i++) {
       x=num%10;
       p[i]=x;
       num /= 10; // Add this.
    }
    
codaddict
When did that happen?
fahad
Oh sorry,got it.
fahad
Take a look at this: http://www.ideone.com/u8z0v
codaddict
Thanks,I read a comment on the link above my code,he said that thinking that multiplying the largest number will give me the biggest palindrome is wrong approch.
fahad
A: 

I have noticed that you don't change n in your loop, so get the last digit every iteration. Next, you fill p array from index 1, but then read it from index 0.

So, if you simplify your code and fix these errors you might get something like this:

#include <stdio.h>
#include <malloc.h>
int IsPalindrome(int num);
int main()
{
    int i,j,temp;
    for(i=10;i<99;i++)
    {
        for(j=10;j<99;j++)
        {
            if(IsPalindrome(i*j)) {
                temp=i*j;
                printf("  %d %d %d\n", i, j, temp);
            }
        }
    }
    return 0;
}

int IsPalindrome(int num)
{
    int i, j;
    char p[50];

    for(i = 0; num > 0; i++) {
        p[i] = num % 10;
        num = num / 10;
    }

    for(j=0; j<i/2; j++) {
        if(p[j] != p[i - j - 1])
            return 0;
    }
    return 1;
}
zserge
+1  A: 

codaddict has answered you already.

In the absence of a specific initialization, variables start their life with unusable values. That happens for your variables flag, x and many others in the IsPalindrome function

int IsPalindrome(int num) /* num sort of initialized automatically */
{
    int x,i,j,flag;       /* x, i, j, and flag have unusable values right now */
    char p[50];           /* p[0], p[1], ... are unusable values right now    */
    /* ... */
    for(i=1;x==0;i++)

That last for line I copied initializes i and tries to use x which is a no-no because x's value is unusable.

pmg
One more question.In the similar question whose link I have posted,the array length was 7 and he knew that the value would be 7 int's long.How did he knew that?
fahad
If I understand your question ... maybe because the largest numbers you're dealing with are 999 and 999*999 is 998001, which when written as a zero-terminated string needs 7 bytes.
pmg
@Pmg:Thanks for the help
fahad