?
Solved

Palindromes

Posted on 2003-03-24
14
Medium Priority
?
264 Views
Last Modified: 2010-04-01
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
Comment
Question by:greeek_god
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
  • 2
  • +2
14 Comments
 

Expert Comment

by:unknownmat
ID: 8199634
Could you post your code?  I'm curious about why yours would only work to 50.
0
 

Author Comment

by:greeek_god
ID: 8199923
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
 

Expert Comment

by:unknownmat
ID: 8199946
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:greeek_god
ID: 8199992
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
 

Author Comment

by:greeek_god
ID: 8200007
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
 

Author Comment

by:greeek_god
ID: 8200023
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
 

Expert Comment

by:unknownmat
ID: 8200024
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
 

Author Comment

by:greeek_god
ID: 8200027
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
 

Author Comment

by:greeek_god
ID: 8200062
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 180 total points
ID: 8200741
>> 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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8200766
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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8200799
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
 
LVL 1

Expert Comment

by:yukapapa
ID: 8200946
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
 

Author Comment

by:greeek_god
ID: 8201019
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question