Coding issue

Discussion in 'Programming General' started by Darkgroove, Mar 15, 2010.

Coding issue
  1. Unread #1 - Mar 15, 2010 at 1:00 AM
  2. Darkgroove
    Referrals:
    0

    Darkgroove Guest

    Coding issue

    Ok I need to code a program that uses arithmetic operations to reverse an integer input by the user and display it to them.

    Eg. 1234 needs to be output back to the user as 4321.

    So I use the modular operation to get each digit:
    1234 % 10 = 4
    123 & 10 = 3
    12 % 10 = 2
    1 % 10 = 1

    So I have the pattern 4,3,2,1. Without predefining a set number of digits for a user to enter, and without using loops, how would I display the number 4321 back to the user?

    For example, I'd need to keep doing the modular of the function until the remainder is 0, but we aren't allowed to use loops. We also can't use any predefined java classes (like stringbuffer's .reverse()). Can anyone suggest some code?

    Jimmy suggested using bit shifts but I can't understand it all that well and neither does he.
     
  3. Unread #2 - Mar 15, 2010 at 8:49 AM
  4. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Coding issue

    How in fuck are you supposed to do it without a loop? >_>. I can't think of any way to do it without a loop.

    Do you have to use the remainder to make it go backwards?
     
  5. Unread #3 - Mar 15, 2010 at 8:51 AM
  6. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Coding issue

    Jesus.

    Read. Understand. Use. >_>.
     
  7. Unread #4 - Mar 15, 2010 at 10:10 AM
  8. Darkgroove
    Referrals:
    0

    Darkgroove Guest

    Coding issue

    I tried to understand. When I tried to use it it was bad.

    I don't know how I'm supposed to do it without a loop...

    and yeah the remainder is the number backwards.
     
  9. Unread #5 - Mar 15, 2010 at 10:28 AM
  10. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Coding issue

    Code:
        private static int reverse(int i)
        {
    	int returnVal = 0;
    
    	returnVal += ((i / 1) % 10) * 100000000;
    	returnVal += ((i / 10) % 10) * 10000000;
    	returnVal += ((i / 100) % 10) * 1000000;
    	returnVal += ((i / 1000) % 10) * 100000;
    	returnVal += ((i / 10000) % 10) * 10000;
    	returnVal += ((i / 100000) % 10) * 1000;
    	returnVal += ((i / 1000000) % 10) * 100;
    	returnVal += ((i / 10000000) % 10) * 10;
    	returnVal += ((i / 100000000) % 10) * 1;
    
    	return returnVal;
        }
    The above works, but looks like shit. The problem it has, though, is that it adds trailing zeros (123456 [AKA 000123456] becomes 654321000). It would be perfect if you made a method to remove the trailing zeros (normally I would, but I'm a bit strapped for time atm, will probably make that method when I get home from school).
     
  11. Unread #6 - Mar 15, 2010 at 11:00 AM
  12. SuF
    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary

    SuF Legend
    Pirate Retired Global Moderator

    Coding issue

    Wait. Dur. Ints have a max number they can be... Forgot about that. Good job Jimmmy.

    But. You fail. Read your post. >_<.
     
  13. Unread #7 - Mar 15, 2010 at 6:39 PM
  14. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Coding issue

    Oops.

    Wrote another shitty method to remove the trailing zeros from an int. Should be fully working, now.
    http://pastebin.com/cctmDE4Z
     
< Runescape Scripting Help :) | Need help understanding this Code. >

Users viewing this thread
1 guest


 
 
Adblock breaks this site