Results 1 to 13 of 13

Thread: Anyone know a algorithm to reverse a number's order?.:Resovled:.

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Question Anyone know a algorithm to reverse a number's order?.:Resovled:.

    Hello everyone,
    I'm trying to figure out how i'm suppose to ouput a number reversed, such as, 123 output it as 321.
    I'm not allowed to use arrays so i'm thinking there has to be a mathamatical way of doing this. There is no fixed amount of numbers, it can be 2 to whatever the user wants to enter, any idea's? Like this almost worked....
    input: 155
    155%10 = 5;
    15%10 = 5;
    10%10 = 0;
    almost got 551

    % is mod in C++ but u all probably knew that just making sure!
    Thanks for listening
    Last edited by voidflux; Aug 17th, 2003 at 12:40 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    This is VB but...

    VB Code:
    1. Private Sub Form_Load()
    2. Dim Inp As String
    3. Dim i As Integer
    4. Dim Res As String
    5.  
    6. Inp = InputBox("Enter a number")
    7. Res = ""
    8. For i = Len(Inp) To 1 Step -1
    9.     Res = Res & Mid$(Inp, i, 1)
    10. Next
    11.  
    12. MsgBox Res
    13. End Sub


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Thumbs up

    Thanks for your reply, here's what comerak showed me:
    Code:
    #include <iostream> 
    using namespace std; 
    
    int main() 
    { 
        int number; 
        cout << "Please enter a number (integer): "; 
        cin >> number; 
    
        while (number) 
        { 
            cout << number%10 << " "; 
            number/=10; 
        } 
        cout << "\n"; 
        return 0; 
    }
    works like a charm!
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  4. #4
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Talking hmm...

    interersting example you give:

    155%10 = 5
    15%10 = 5
    10%10 = 0

    just wondering why you divided by 10 between steps one and two, but not between two and three. If you had, then i guess it would have worked.

    Anyway, if you are using C++, then you may be able to save it into a string, then just either use STL to reverse it (there might be some reverse() algorithm), or just use a stack, push the digits (or characters) on, then pop off and store the new order.
    sql_lall

  5. #5

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Smile

    If it was up to me I would have just stored each digit into an array and then started with the last element and iterate to the first element but arrays weren't allowed nor STL
    THanks for the advice though!
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Anyone know a algorithm to reverse a number's order?

    Originally posted by voidflux
    Hello everyone,
    I'm trying to figure out how i'm suppose to ouput a number reversed, such as, 123 output it as 321.
    I'm not allowed to use arrays so i'm thinking there has to be a mathamatical way of doing this. There is no fixed amount of numbers, it can be 2 to whatever the user wants to enter, any idea's? Like this almost worked....
    input: 155
    155%10 = 5;
    15%10 = 5;
    10%10 = 0;
    almost got 551

    % is mod in C++ but u all probably knew that just making sure!
    Thanks for listening
    10%10 = 0 -- yep, that's true.... mod returns the remainder after division
    1%10 = 1
    2%10 = 2
    3%10 = 3
    4%10 = 4
    5%10 = 5
    6%10 = 6
    7%10 = 7
    8%10 = 8
    9%10 = 9
    10%10 = 0
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    What if you are told its a number, but its base is not fixed at ten,

    ie, WIF its base is 9, 12, 16, anything? And, they use Alpha Chars for numerical representation?

    I'd stick with mid if I were you.


    -Lou

  8. #8
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Hmmm, makes me wonder:

    If we have some base ten number, perhaps 123456, and represent it as K in hex,
    if we say that 654321 = reversed{123456},
    is 654321 = Reversed{K}?

    -Lou

  9. #9
    Fanatic Member bugzpodder's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    787
    C++ strings arent usually considered arrays, so you can use strings, and STL reverse command. or if you want mathematics,

    int reverse(int n){
    int t=0;
    for (;n>0;n/=10) t=t*10+n%10;
    return t;
    }
    doesnt work with negative numbers or decimals
    Massey RuleZ! ^-^__Cheers!__^-^ Massey RuleZ!


    Did you know that...
    The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!

  10. #10
    New Member
    Join Date
    Sep 2003
    Posts
    1

    Reversing the Digits

    Reversing the digits (in binary) is an important step in unscrambling the output of the Fast Fourier Transform. The only fast way I know to do it is the memory-consuming table lookup. Some compromises are possible, such as limiting the table size to 256 entries to give instant reversal of 8 bits at a time, then reversing the bytes (instead of bits) in software.

    Reversing the bytes might be done by strings, as previously shown. But in your case, you are working with integers in number base ten, so you could construct while decimating:
    R is the result, N is the number to be reversed.

    R=0
    while N>0
    R:=10*R+mod(N,10)
    N=N/10
    wend

    Only integer arithmetic is used here, and minimal memory... HOWEVER, EXTREME WARNING!!!! NOT VALID FOR NEGATIVE NUMBERS! I suspect that using the string method is equivalent to this, with the loop and logic efficiently coded by VB and invisible to you.

    I apologise for any contamination by HotPaw basic. My Palm is my main machine.

  11. #11
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    One final word on the string reverse: you can use the StrReverse function instead of a loop
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  12. #12
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171
    Originally posted by jemidiah
    One final word on the string reverse: you can use the StrReverse function instead of a loop
    Not always. StrReverse was implemented in VB 6 for the first time...


    Has someone helped you? Then you can Rate their helpful post.

  13. #13
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431
    From his sig.

    Visual Studio.Net EE
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

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