Link to home
Start Free TrialLog in
Avatar of greeek_god
greeek_god

asked on

Palindromes

Hi! How can I ouput the first M palindromic numbers(where int M is a variable, inputted by the user)? If the user wants the first 3 palindromes, then the output should be:

                                               0
                                               1
                                               2

You get the picture. I have already created a C++ program that does this, but for some unknown reason, it refuses to output numbers if M > 50. And this is the function I'm using:

                                  void PalNum(int array[]);

Can anyone help me????
Avatar of unknownmat
unknownmat

Could you post your code?  I'm curious about why yours would only work to 50.
Avatar of greeek_god

ASKER

I'm sorry to confuse you. I did not make myself clear at all. I have coded such that my program calculates ALL the palindromic numbers UNTIL 50. So even if the user wants the first 100 palindromes, he/she will get only the first 50. But if he/she wants the first M palindromes, where M < 50, my program outputs correctly. How can I rectify this? My code is really long.
Oh ... How about this?

Change your PalNum definition to:

void PalNum( int array[], int iSize ); //here size tells you how large a buffer you need to fill with palindromes

Without knowing how your code works, I cannot say for sure, but I find that extending code to an arbitrary size 'n' is usually not difficult.  If you wrote it correctly, it should be a matter of changing your for-loops to loop until iSize, rather than 50. <shrug>

Does that help at all?

Matt

Hi Matt, really appreciate your quick replies! well, this is what i'm trying to do.

///////////////////////////////////////////////////////
cout << "Enter max limit: ";
cin >> maxLimit;
int array[maxLimit];
PalNum(array);
for(int i=0;i<maxLimit;i++)
{
   cout << array[i] << endl;
}
///////////////////////////////////////////////////////

and PalNum(array) will calculate the first maxLimit palindromes.

Am I making sense? Thanks again!
and would anyone know if there's a formula for outputting palindromic numbers? cuz i'm using WAY too many ifs and elses and that's killing the purpose of why i even wanted to write the code!! a formula would be perfect. oh well...
and would anyone know if there's a formula for outputting palindromic numbers? cuz i'm using WAY too many ifs and elses and that's killing the purpose of why i even wanted to write the code!! a formula would be perfect. oh well...
It's no problem, EE has quickly become another way to distract myself from homework that I don't really want to do.

How does your PalNum algorithm know how many to return?  May I assume that maxLimit is global?  And that you've 'solved' the problem without using loops (something like a statically defined array of palindromes)... ?

Am I completely off-base here?

Matt

and would anyone know if there's a formula for outputting palindromic numbers? cuz i'm using WAY too many ifs and elses and that's killing the purpose of why i even wanted to write the code!! a formula would be perfect. oh well...
hey all...sorry about the multiple posts....i don't know how the heck it came out like that.

and yeah...that's what i'm going to do. make maxLimit global. and nopes, i didn't use any statically defined arrays of any sort. what i did was really long, time consuming and dumb. here's my algorithm:

1. Store  the first 10 palindromes in array[index], where  
   0<=index<=9, using loops.
2. Now, array[10] = array[9]+2; // 11 = 9+2
3. for (index=11;index<19;index++)
       array[index] = array[index-1] + 11;
4. array[20] = array[19] + 2;
5. Do exactly the same until array[50].

See the problem? My algorithm is messed up and will be way way way too long. I hope you can see the problem. Try as I might, I can't seem to think of any other method to find palindromes...yet this method is obviously the saddest and longest.

Sigh...
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
First of all, I see no reason to save the numbers.  It's a waste of memory.  If you need them, that's fine, but I don't think you do.  Second, using globals for controlling your program is sloppy, and it's a bad habit you'd be better off without.

Here's a solution I coded, thinking until about half way through that this was the 'C' forum.  Actually, I'll just give you the meat...

   bool isPalindrome(int input)
   {
      char buffer1[20], buffer2[20];

      itoa(input, buffer1, 10);
      strcpy(buffer2, buffer1);
      _strrev(buffer2);
      if (strcmp(buffer1, buffer2)==0)
      {
         return true;
      }
      else
      {
         return false;
      }
   }

brian
Of course, this is a rediculous way to find large numbers, but I like to choose the easiest solution possible for a given problem.  A more efficient way would be to generate them, instead of searching for them.

brian
Hi, all.

What is the palindromic number?

Numbers below palindromic?

0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
11, 22, 33, 44, 55, 66, 77, 88, 99
101, 111, ..., 191, 202, 212, ..., 292, 909, 919, 999,
...

Thanks to everybody!! Mayankeagle provided what I needed the most - a function to CHECK if a number is a palindrome!!! Now the code's perfect. Thanks everyone, and to Mayankeagle especially!