0

I'm a beginner at programming and I've never posted on here before. I've had a search for answers to my question and one such thread was here ...but I can't get their method to work.

I'm trying to learn C by doing some of the Project Euler challenges. For one of the challenges I need to check if a number is palindromic. I thought I'd do this by loading my target integer, called product, into an array before checking pairs of elements against each other (looking for symmetry).

First I check to see how long my product is:

productLength = ceil(log10(product)); 

Then I have a loop to try and load that integer into my array called inspection:

for(x = productLength; x <= 0; x--) //decrement from product length and loop
{
    inspection[x - 1] = product % 10; //transfer product values to inspection array
    product /= 10; //prepare product value for next pass (move digits one place to the right)
}

This doesn't seem to work. I'm aware it's probably some silly mistake but I can't spot it.

Community
  • 1
  • 1
sprogg
  • 23
  • 1
  • 4
  • 1
    This might help: http://stackoverflow.com/questions/1114741/how-to-convert-int-to-char-c – DLeh Jan 07 '14 at 17:43
  • 2
    You already got answers, but generally you should explain your problem better, not just *"this doesn't seem to work"*. What result do you expect and what do you actually get? - Also single-stepping in the **debugger** would probably reveal the mistake quickly. – Martin R Jan 07 '14 at 17:50
  • Did you take into consideration debugging this you own using a real debugger? – alk Jan 07 '14 at 18:07

4 Answers4

1

This line

for(x = productLength; x <= 0; x--)

should be:

for(x = productLength; x >= 0; x--)

However, here you have another problem.

    inspection[x - 1] = product % 10; //transfer product values to inspection array

The loop will reach x = 0 inclusive, so when x = 0, this will cause an invalid index.

So the loop should look like this:

for(x = productLength-1; x >= 0; x--)
{
    inspection[x] = product % 10;
    product /= 10;
}
inspection[productLength] = 0;
Devolus
  • 21,661
  • 13
  • 66
  • 113
0

Your for loop should be:

for(x = productLength; x >= 0; x--)

instead of for(x = productLength; x <= 0; x--)

0

So this line

productLength = ceil(log10(product)); 

should easier be

productLength = log10(product) + 1;

As

unsigned x = <some value > 0>;
size_t s = log10(x);

is

0 for all values of x < 10     
1 for all values of x < 100
2 for all values of x < 1000
...

Using ceil() works, but I feel using it is overkill.


The solution:

size_t productLength = log10(product) + 1; 
unsigned char inspection[productLength];

for (size_t x = productLength - 1; x >= 0; --x) //decrement from product length and loop
{
    inspection[x] = product % 10; //transfer product values to inspection array
    product /= 10; //prepare product value for next pass (move digits one place to the right)
}
alk
  • 69,737
  • 10
  • 105
  • 255
0

If it's an integer you need to palindrome check, then you can just print it in a string and start comparing the characters from both ends:

int palindromic(int n)
{
    char str[20];
    int  result;
    int  i;

    sprintf(str, "%d", n);
    int length = strlen(str);

    for (result = 1, i = 0; i < length / 2 && result == 1; i++)
    {
        result = (str[i] == str[length - 1 - i]);
    }

    return result;
}
meaning-matters
  • 21,929
  • 10
  • 82
  • 142