Thread: Reverse of an integer

  1. #31
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Maybe a better way to describe it is:
    What math do we need to do in each step to make this happen:
    Code:
    r = 0;    // reverse number
    a = 123;    // original number
    
    r = 3;
    a = 12;
    
    r = 32;
    a = 1;
    
    r = 321;
    a = 0;
    --
    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.

  2. #32
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    So what you mean is that I treat the final output as a whole integer instead of individual digits printed side by side?

  3. #33
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    r = 0; // reverse number
    a = 123; // original number

    r = 3;
    a = 12;

    r = 32;
    a = 1;

    r = 321;
    a = 0;
    Nice!!!

  4. #34
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by freddyvorhees View Post
    So what you mean is that I treat the final output as a whole integer instead of individual digits printed side by side?
    Yup, that's what your function should be doing - it returns ONE integer, right? At least how I understand it, and I'm pretty sure I'm not alone here - if you have any doubt about that, please ask your tutor - we wouldn't be wanting to guide you in the wrong direction.

    --
    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.

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It depends on what you mean by final output.

    The function's prototype is
    int reverse(int);

    This means you can return one number. So yes, you want to return a single number.

    You cannot even return "individual digits printed side by side" because the function is only supposed to return something, not print it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #36
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I this ok?
    Code:
    #include <iostream>
    using namespace std;
    
    int reverse(int x)
    {
    	int reverse = 0,exp = 1,counter = x;
    		while (counter != 0)
    		{
    		counter = counter /10;
    		reverse = (((x&#37;(exp*10))/exp) + reverse)*10;
    		exp = exp * 10;
    		}
    		return reverse/10;
    }
    int main()
    {
      int num;
      cin >> num;
      cout << "Reverse of " << num << " is " << reverse(num) << endl;
      return 0;
    }
    I think its working.

  7. #37
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by freddyvorhees View Post

    I think its working.
    You have an interesting concept of working:
    Code:
    C:\>temp
    1919191910
    Reverse of 1919191910 is 191919190
    I think your scheme of way-too-many-10s works out except when you get close to overflow like this.

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That looks very convoluted to me. A simpler solution that also works for non-negative integers only would be:

    EDIT:
    Wait a minute, this is not just self-learning? I feel that you have a simple idea struggling to get out, but have the basic idea there, hence I posted a solution... but if this really is to be submitted as homework, then you might want to think a little further before looking at my solution.

    One way to help you think is for you to explain to us what is the idea that you are currently using.
    Last edited by laserlight; 08-23-2008 at 12:24 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #39
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    I made a new one but I can only input numbers with 8 or less digits, just like the other one but it has less variables now.
    Code:
    #include <iostream>
    using namespace std;
    
    int reverse(int x)
    {
    	int reverse = 0;
    		while (x != 0)
    		{
    		reverse = ((x &#37; 10) + reverse)*10;
    		x = x / 10;
    		}
    		return reverse/10;
    }
    int main()
    {
      int num;
      cin >> num;
      cout << "Reverse of " << num << " is " << reverse(num) << endl;
      return 0;
    }
    And please, I really appreciate all your help but its much better if you can answer me without sarcasm, it doesn't really boost my moral

  10. #40
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I made a new one but I can only input numbers with 8 or less digits, just like the other one but it has less variables now.
    You will reach the limit of int eventually, though in this case you are even more limited since your algorithm is still slightly more convoluted than necessary. Nonetheless, I feel that you have come close enough, so here is the solution I had in mind:
    Code:
    int reverse(int x)
    {
        int x_reversed = 0;
        while (x > 0)
        {
            x_reversed *= 10;
            x_reversed += x &#37; 10;
            x /= 10;
        }
        return x_reversed;
    }
    The main difference between my code and yours is that my code multiplies the partially reversed result by 10 before adding the next digit, whereas your code adds the next digit and then multiplies by 10, thus forcing you to divide the result by 10 at the end, which in turn reduces the range that can be handled by a magnitude.

    To be rid of the limit of ints, change the problem into one of reading a string of characters and printing the string in reverse.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #41
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Nice thanks, and maybe I could just remove the quantity. Like this:

    Code:
    int reverse(int x)
    {
    	int reverse = 0;
    		while (x != 0)
    		{
    		reverse = (x &#37; 10) + reverse*10;
    		x = x / 10;
    		}
    		return reverse/10;
    }
    It's the same right?

  12. #42
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Now the division at the end looks superfluous.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #43
    Registered User
    Join Date
    Jul 2008
    Posts
    91
    Oh yeah, I forgot. Fixed it already

  14. #44
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Minor detail - it is always a good idea to use different names for variables and functions - your function and your variable are both called "reverse".

    And it only took about 40 posts to get there....

    --
    Mats
    Last edited by matsp; 08-24-2008 at 12:45 PM.
    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