CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Apr 2009
    Posts
    32

    coding help, reverse array, check for palindrome

    Hey guys, I'm having a bit of trouble with an assignment that has been racking my brain. I'm taking a number from the user, using a function to print its reverse order, checking to see if it is a palindrome in the main program and also removing any leading zero's from the results. I'm having no trouble printing the reverse order but the palindrome check and leading zero removal are driving my crazy. I tried using a loop to check each digit for palindromes but this doesn't seem very effective and returns a reply for each digit for a single statement. Here is the code:

    #include<iostream>
    using namespace std;

    int reverse(int bef[], int &tot)
    {
    int i,j,t;

    for(i=0, j=tot-1; i<j; i++, j--)
    {t=bef[i]; bef[i]=bef[j]; bef[j]=t;}



    }

    int main()
    {


    int i;
    int totdig;
    cout << "Enter total number of digits in the number you would like reversed: ";
    cin >> totdig;
    int before[totdig];
    int orig[totdig];
    cout << "\nEnter the complete number, seperate each digit with a space: ";
    for(i=0;i<totdig;i++) cin >> before[i];
    for(i=0;i<totdig;i++) orig[i]=before[i];
    reverse(before, totdig);




    for(i=0;i<totdig;i++) cout << before[i];
    cout << endl;
    for(i=0;i<totdig;i++) cout << orig[i];
    cout << endl;



    return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: coding help, reverse array, check for palindrome

    Well, the palindrome check should be trivial once you've reversed the array; just check whether it's identical to the original array.

    To get rid of leading 0s in the reversed number, just convert it back from a digit array to a single int. The normal output formatting won't write leading 0s.

  3. #3
    Join Date
    Apr 2009
    Posts
    32

    Re: coding help, reverse array, check for palindrome

    To check for palindromes, a statement like if(orig==before) cout << "This is a palindrome!"; returns the incorrect result. I think an array needs to compared element to element and cannot be checked as a whole.

    I understand that a single integer automatically removes leading zero's but I could not find a way to convert an array into a single integer. Google searches haven't been kind to me.
    Last edited by dmitriylm; November 5th, 2009 at 02:40 PM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: coding help, reverse array, check for palindrome

    std::equal() could do the palindrome check. Internally, it's more or less doing a loop over the elements, so you could pretty trivially do that yourself if you prefer.

    If you use an array of chars rather than an array of ints, then you could simply use atoi() or strtol() to convert to a single int. The only thing to be careful about in that case is that the char '2' is not the same as the integer 2, but I don't think that directly affects anything you're doing right now.

  5. #5
    Join Date
    Apr 2009
    Posts
    32

    Re: coding help, reverse array, check for palindrome

    I added a loop in the main program that should effectively check for palindromes but for some reason it breaks the reverse function and the two final cout statements print the same result, nothing is reversed. The omission of this loop then returns expected results even though the loop itself isn't modifying anything relating to the array.

    #include<iostream>
    using namespace std;

    int reverse(int bef[], int &tot)
    {
    int i,j,t;

    for(i=0, j=tot-1; i<j; i++, j--)
    {t=bef[i]; bef[i]=bef[j]; bef[j]=t;}



    }

    int main()
    {


    int i,j;
    int totdig;
    cout << "Enter total number of digits in the number you would like reversed: ";
    cin >> totdig;
    int before[totdig];
    int orig[totdig];
    cout << "\nEnter the complete number, seperate each digit with a space: ";
    for(i=0;i<totdig;i++) cin >> before[i];
    for(i=0;i<totdig;i++) orig[i]=before[i];
    reverse(before, totdig);


    for(i=0,j=0;i<totdig;i++)
    { if (before[i]=orig[i]) j++;
    if (j==totdig) cout << "This is a palindrome!\n";
    }


    for(i=0;i<totdig;i++) cout << orig[i];
    cout << endl;
    for(i=0;i<totdig;i++) cout << before[i];
    cout << endl;

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: coding help, reverse array, check for palindrome

    The old assignment versus equality problem. Count your =.

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: coding help, reverse array, check for palindrome

    Code:
    int totdig;
    cout << "Enter total number of digits in the number you would like reversed: ";
    cin >> totdig;
    int before[totdig];
    int orig[totdig];
    This is not valid C++. You should switch to using a vector instead of a regular array.

    Viggy

  8. #8
    Join Date
    Apr 2009
    Posts
    32

    Re: coding help, reverse array, check for palindrome

    Quote Originally Posted by Lindley View Post
    The old assignment versus equality problem. Count your =.

    Ahhh! Thank you very much. Ok, so as it is now, the code is doing everything I need besides getting rid of leading zero's in cases where I would enter 1000 and the function would return 0001. Any hint as to how I would go about converting an integer array to a single integer?

  9. #9
    Join Date
    Apr 2009
    Posts
    32

    Re: coding help, reverse array, check for palindrome

    Quote Originally Posted by MrViggy View Post
    Code:
    int totdig;
    cout << "Enter total number of digits in the number you would like reversed: ";
    cin >> totdig;
    int before[totdig];
    int orig[totdig];
    This is not valid C++. You should switch to using a vector instead of a regular array.

    Viggy
    Anything specifically? I believe that is a perfectly legal method to initialize an array.

  10. #10
    Join Date
    Feb 2002
    Posts
    4,640

    Re: coding help, reverse array, check for palindrome

    Not in the current C++ standards:
    Code:
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    "ComeauTest.c", line 13: warning: missing return statement at end of non-void
              function "reverse"
      }
      ^
    
    "ComeauTest.c", line 23: error: expression must have a constant value
      int before[totdig];
                 ^
    
    "ComeauTest.c", line 24: error: expression must have a constant value
      int orig[totdig];
               ^
    
    2 errors detected in the compilation of "ComeauTest.c".
    Viggy

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: coding help, reverse array, check for palindrome

    Good catch.

    Quote Originally Posted by dmitriylm View Post
    Anything specifically? I believe that is a perfectly legal method to initialize an array.
    The size of an array must be a compile-time constant in C++.

    C99 relaxes this, and some compilers support the C99 approach as an extension. But it's not something you should rely on.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: coding help, reverse array, check for palindrome

    Quote Originally Posted by dmitriylm View Post
    Anything specifically? I believe that is a perfectly legal method to initialize an array.
    Your belief is wrong. Turn off your compiler extensions, and compile the code as ANSI C++.

    You will see that it is not legal to declare an array with a non-const expression.

    Speaking of such -- which version of Visual C++ allows this? This is the Visual C++ forum, and as far as I know, no version of Visual C++ allows this non-standard code.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured