0
#include <iostream>
#include <vector>
#define MAXX 1000
using namespace std;

int number[MAXX], digits=0;
int adjust(int i)
{
    if(number[i]<9)
    {
        cout<<"i = "<<i<<" "; //PROBLEM
        (number[i])++;
        return i;
    }

    number[i]=0;
    adjust(i-1);
}

void makePalindrome(int head,int tail)
{
    int revert;
    if(head>tail)
        return;

    if(number[head]==number[tail])
    {
        makePalindrome(head+1,tail-1);
    }
    if(number[tail]<number[head])
    {
        number[tail]=number[head];
        makePalindrome(head+1,tail-1);
    }
    if (number[tail]>number[head])
    {
        number[tail]=number[head];
        revert=adjust(tail-1);
        if(revert<=head)
        {
            makePalindrome(revert,digits-revert-1);
        }
        else
        {
            makePalindrome(head+1,tail-1);
        }
    }
}

int main(int argc, char const *argv[])
{
    long long int num,num_copy;
    int head,tail;
    int number_reverse[MAXX];
    cout<<"Enter the number whose next greater palindrome you want to find"    <<endl;
    cin>>num;
    num_copy=num;

    while(num_copy>0)
    {
        number_reverse[digits]=num_copy%10;
        digits++;
        num_copy=num_copy/10;
    }

    //cout<<"Digits = "<<digits<<"\n";
    for (int i = digits-1; i >=0; --i)
    {
        number[digits-i-1]=number_reverse[i];
        //cout<<number[digits-i-1]<<" ";
    }
    head=0; tail=digits-1;
    makePalindrome(head,tail);
    cout<<"Answer : ";
    for (int i = 0; i < digits; ++i)
    {
        cout<<number[i];
    }
    cout<<"\n";
    return 0;
}

When I am running with an input : 94187978322, it is giving two different answers with and without a "cout" line (line with comment "PROBLEM").

Here's the output:

ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp 
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
Answer : 94188078149
ishivendra:code shivendraagrawal$ g++ next_palindrome.cpp 
ishivendra:code shivendraagrawal$ ./a.out
Enter the number whose next greater palindrome you want to find
94187978322
i = 7 i = 6 i = 4 Answer : 94188088149

The second output is the desired output. Can you point out the cause of this difference and incorrectness for the first one ?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
Shivendra
  • 1,076
  • 2
  • 12
  • 26
  • 2
    A **`return`** statement in `adjust()` would considerably increase its predictability. – WhozCraig Oct 30 '13 at 17:15
  • Didn't your compiler tell you about that? – Beta Oct 30 '13 at 17:18
  • You should specify which compiler you are using. And you should compiler warnings enabled. – Adam Burry Oct 30 '13 at 17:23
  • No, since there was no compilation error or runtime error. I figured it out, it was a logical error. I should have written 'return adjust(i-1)' I still don't know the cause for the peculiarity but at least my code is running as desired now. – Shivendra Oct 30 '13 at 17:23
  • Pleeaaaaaaase change it to `void adjust(int i)`. – jrok Oct 30 '13 at 17:27

1 Answers1

0

I figured half of it out, it was a logical error. I should have written return adjust(i-1). I still don't know the cause for the peculiarity (There was no compilation or runtime error) but at least my code is running as desired now.

Shivendra
  • 1,076
  • 2
  • 12
  • 26