Reversing numbers in c without arrays

This is a discussion on Reversing numbers in c without arrays within the C Programming forums, part of the General Programming Boards category; As the title says I need to know how to reverse numbers in c without using the arrays. Why? That ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    17

    Reversing numbers in c without arrays

    As the title says I need to know how to reverse numbers in c without using the arrays. Why? That is part of my assignment. My professor told me that there is a simpler way to do it but I don't know what she is talking about, and I've found nothing in my book about reversing until the array section. By reverse I mean take 123 and make a new number, 321. I need this last part for an assignment that I actually posted earlier. The assignment calls for me to input a number and solve by its reverse. Remember no arrays, thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    This counts as one of those things which is so annoyingly simple that you'll kick yourself for not seeing it earlier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by jp5a9852 View Post
    As the title says I need to know how to reverse numbers in c without using the arrays. Why? That is part of my assignment. My professor told me that there is a simpler way to do it but I don't know what she is talking about, and I've found nothing in my book about reversing until the array section. By reverse I mean take 123 and make a new number, 321. I need this last part for an assignment that I actually posted earlier. The assignment calls for me to input a number and solve by its reverse. Remember no arrays, thanks!
    Do you need to save the reversed number or print it to screen?

    If it is just printing out the reverse, I would just repetedly do a number % 10 and print it out. If you need to save it, I would do number % 10 and multiply each extracted digit by its weight.

    Eg.

    123 % 10 = 3
    12 % 10 = 2
    1 % 10 = 1

    reversed number would be

    (3 * 100) + (2 * 10) + (1)

    u can save this in a variable and print it out.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    Most of the questions I ask are like that unfortunately, I'm not finding this in my book though so I don't know where to start. So I must ask.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,625
    That's sign number one that you aren't learning programming sufficiently.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    17
    I don't need to print out the reverse just solve by it. Also I haven't seen the % used like this, what exactly is it doing?

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Quote Originally Posted by jp5a9852 View Post
    I don't need to print out the reverse just solve by it. Also I haven't seen the % used like this, what exactly is it doing?
    may be u should look up the Arithmetic operators in C section of the book.

    C - Overview of Operator Types, Arithmetic, Bitwise, Assignment, Precedence Table

    when u do x % y, u get the reamainder of the division.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,494
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    I, too, am fascinated by that use of the modulus operator. At first I was confused, but now I see why it should work on any number. It's because it's dividing by 10 every time. So the remainder will always be the digit you want next. Bravo, I think that is really smart.
    Last edited by gratiafide; 11-15-2012 at 08:17 PM.

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Quote Originally Posted by gratiafide View Post
    I, too, am fascinated by that use of the modulus operator. Surely that doesn't work for all numbers though?
    What do you mean by that?

    The modulus operator returns the remainder of a divide

    [edit] Original quote edited -> See below. [/edit]
    Last edited by Click_here; 11-15-2012 at 08:48 PM.
    Fact - Beethoven wrote his first symphony in C

  11. #11
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    I had edited my post almost immediately

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Quote Originally Posted by gratiafide View Post
    I had edited my post almost immediately
    No worries -> I've made a note in my post for you.
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Think of an int variable as a stack...
    The item on the top of the stack can be obtained from value%10
    You can remove an item off the top off the stack by doing value/=10
    The stack is empty when the value is zero
    You can push an item on by doing value=value*10+digit

    Now, simply do the following in pseudocode:
    while stack1 is not empty
    * pop top item off stack1
    * push item onto stack2

    (Note that this is the same procedure as reversing a linked-list in-place)
    Done!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    What did we do in the days before the modulo operator? (in another language such as BASIC). We were still able to solve these problems...
    X - X / 10 * 10
    This scales the number down for the next iteration if you save the intermediate result as well as obtains the remainder - the units digit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with counting numbers in arrays
    By gbp42 in forum C Programming
    Replies: 1
    Last Post: 11-03-2011, 11:40 AM
  2. Finding the sum of even numbers using arrays
    By Fox101 in forum C Programming
    Replies: 7
    Last Post: 12-03-2007, 01:20 PM
  3. sum of n numbers using arrays
    By IPCHU in forum C Programming
    Replies: 4
    Last Post: 12-07-2006, 05:05 AM
  4. Doing multiplication using numbers in arrays
    By neandrake in forum C++ Programming
    Replies: 17
    Last Post: 11-05-2004, 10:07 AM
  5. Reversing a set of numbers using recursion
    By Shikimo in forum C++ Programming
    Replies: 2
    Last Post: 02-14-2002, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21