Thread: Reverse of an integer

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    91

    Reverse of an integer

    The problem:
    Code:
    The name of the function's name should be reverse. Let the 
    name of its value parameter be x (which is an int).  The 
    return datatype should be int.  Use the main() driver program 
    provided below to test your function:
    
    #include <iostream>
    using namespace std;
    
    // insert the reverse function here.
    
    int main()
    {
      int num;
      cin >> num;
      cout << "Reverse of " << num << " is " << reverse(num) << endl;
      return 0;
    }
    I know what I should do to obtain the reverse number, but its harder when using a function int. The program should handle any as much digits it can handle. So I know I should use a loop, and also you should know the number of digits - I've done that. My question is what is the proper way of returning the reverse digits. And by the way our lesson is about REFERENCES AND REFERENCE PARAMETER.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This particular bit of code doesn't (as far as I can determine) need to use references.

    How would you go about reversing an integer if you didn't have to use a function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Use a modulo?then output the result side by side in a loop?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Reverse in what way? 43,621 => 12,634? 1 => -1? Sorry for sounding like a smart allec, but your goal seems ambiguous.

    Example
    Code:
    #include <cstring>
    #include <cstdlib>
    
    int reverse(int number)
    {
       char buffer[256];
    
       ssprintf(buffer, "&#37;d", number);
       return atoi(strrev(buffer));
    }
    Nothing too fancy... To be honest though, I am not sure if this is even what you are wanting to do.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    my interpretation of "reverse" is 123 -> 321. And using sprintf/strrev/atoi is a bit overkill, isn't it - the problem can be solved with two or three variables. Modulo is one of the operations you need, you will also need divide, add and multiply. Put those bits in the right order, and you should be able to solve the number.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And using sprintf/strrev/atoi is a bit overkill, isn't it
    Barring atoi because it sucks and defeats the benefit of using strings, no, it isn't overkill. In fact, turning the integer into a string and reversing the string is vastly superior to breaking down and reconstructing the value as an integer. Why? What happens if you want to reverse 1999999999 with 32-bit integers? There are far too many cases like that, where the reversed value can't be represented by the data type.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    I think Freddy steered you right. You want to figure out the number of digits represented, which you can do by progressively dividing by 10 (inside a loop)(incrementing a counter) until you return a zero. Once you know the number of digits, you can proceed accordingly. Looks like an interesting little task that is similar to reversing a string using the old array-of-characters manipulation.

    And Prelude is right... some ints won't reverse as valid numbers, so you need to determine the size of an int and make sure that it won't overflow on reversing--whether you use strings or not.

    Keith

    You can tune a file system, but you can't tune a fish.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Prelude View Post
    >And using sprintf/strrev/atoi is a bit overkill, isn't it
    Barring atoi because it sucks and defeats the benefit of using strings, no, it isn't overkill. In fact, turning the integer into a string and reversing the string is vastly superior to breaking down and reconstructing the value as an integer. Why? What happens if you want to reverse 1999999999 with 32-bit integers? There are far too many cases like that, where the reversed value can't be represented by the data type.
    But the function should return an integer, so whether it copes with huge numbers DURING the reversal makes little difference when it gets converted back to integer [unless you plan to detect it, of course].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Only in this case, and I don't think Prelude was trying to scold anyone, per se.

    Reversing a string also needs tweaking if the number is negative, ie:
    Code:
    if (number < 0) {
      strrev(result + 1);
    } 
    else {
      strrev(result);
    }
    return result;
    But I'm only mentioning it since I'm not positive 8765- is correct.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    #include <cstring>
    #include <cstdlib>

    int reverse(int number)
    {
    char buffer[256];

    ssprintf(buffer, "&#37;d", number);
    return atoi(strrev(buffer));
    }
    Using that is a lot better, but I cant really use that.
    Code:
    #include <iostream>
    using namespace std;
    
    // insert the reverse function here.
    
    int main()
    {
      int num;
      cin >> num;
      cout << "Reverse of " << num << " is " << reverse(num) << endl;
      return 0;
    }
    This is what we should use as our main. Then will just insert our function. I've done it using void, its "amatuerish",but at least I know how to do the logic part. And I think were required to use a function int, and so Im lost there. I have no idea how to properly return the digits in a loop.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why can't you use that?

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Code:
    #include <iostream>
    using namespace std;
    
    // insert the reverse function here.
    
    int main()
    {
      int num;
      cin >> num;
      cout << "Reverse of " << num << " is " << reverse(num) << endl;
      return 0;
    }
    This should be our main

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think freddy's point is that you can't include further header files - which goes back to my suggestion to use simple math.

    What happens if you do this:
    Code:
    int x = 21;
    int y;
    
    y = x &#37; 10;
    x = x / 10; 
    
    cout << "x = " << x << "y = " << y << endl;
    Now I've just about given away the solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I know how to get the individual digits, but when using function I dont know how to return it properly using loop

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by freddyvorhees View Post
    I know how to get the individual digits, but when using function I dont know how to return it properly using loop
    So, try to write something, using what you know, post it, and we'll help you sort out any problems.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM