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.

3
  • 1
    This might help: stackoverflow.com/questions/1114741/… Commented Jan 7, 2014 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. Commented Jan 7, 2014 at 17:50
  • Did you take into consideration debugging this you own using a real debugger? Commented Jan 7, 2014 at 18:07

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

1 Comment

for( ...; x >= 0; ...
0

Your for loop should be:

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

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

3 Comments

The loop shall start with productLength - 1.
That is probably more standard, but he is setting: inspection[x - 1]
Then it shall also be x > 0, otherwise you get UB for x = 0.
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)
}

Comments

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.