• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

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????
0
greeek_god
Asked:
greeek_god
  • 7
  • 3
  • 2
  • +2
1 Solution
 
unknownmatCommented:
Could you post your code?  I'm curious about why yours would only work to 50.
0
 
greeek_godAuthor Commented:
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.
0
 
unknownmatCommented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
greeek_godAuthor Commented:
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!
0
 
greeek_godAuthor Commented:
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...
0
 
greeek_godAuthor Commented:
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...
0
 
unknownmatCommented:
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

0
 
greeek_godAuthor Commented:
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...
0
 
greeek_godAuthor Commented:
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...
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> int array[maxLimit];

int * array = new int[maxLimit] ;

For finding palindromes, you need to reverse the number and then check if its equal to the original number:

bool is_palindrome ( int num )
{
  int reverse = 0, digit = 0, temp = num ;

  while ( temp != 0 )
  {
    digit = temp % 10 ;
    reverse = 10 * reverse + digit ;
    temp = temp / 10 ;

  } // end while

  return ( reverse == num ) ;

} // end of is_palindrome ()


To check for the first n palindromes, you can do:

int * array = new int[n], i, count ;

for ( i = 1, count = 0 ; count < n ; i ++ )
  if ( is_palindrome ( i ) )
    array[count++] = i ; // end if, for

cout << "\n The first " << n " << palindromes are: "

for ( i = 0 ; i < count ; i ++ )
  cout << array[i] << endl ; // end for


Hope that helps!

Mayank.
0
 
bkrahmerCommented:
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
0
 
bkrahmerCommented:
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
0
 
yukapapaCommented:
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,
...

0
 
greeek_godAuthor Commented:
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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now