#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Help with pointers


    Hi, is there any way to divide two numbers and module two numbers without using arithmetic operators ('%' and '/') ?
    I need to use just pointers..
    TNX :hi:
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by Jan4185
    Hi, is there any way to divide two numbers and module two numbers without using arithmetic operators ('%' and '/') ?
    I need to use just pointers..
    TNX :hi:
    No.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Yes. Actually, there are still processors out there that don't implement division in hardware. To divide two arbitrary numbers on such a processor, one has to implement division using the other operations (meaning without '%' and '/', and sometimes even without multiplication). (Compilers should do this for you, should you encounter such a chip in real life.)

    I have no idea what "I need to use just pointers" means. Can you provide a more accurate description of your problem?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by bdb
    No.
    Are you sure ? this is my HW..
    tnx anyway.. :confused:
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by Jan4185
    Are you sure ? this is my HW..
    tnx anyway.. :confused:
    I think you miscopied your HW.
    It is impossible to calculate division and/or modulus with just pointers.

    Maybe you want to calculate without the operators / and % but with all the other operators available in C (including + and - and * ...)? ???
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0
    Originally Posted by bdb
    I think you miscopied your HW.
    It is impossible to calculate division and/or modulus with just pointers.

    Maybe you want to calculate without the operators / and % but with all the other operators available in C (including + and - and * ...)? ???
    Yes, I'm sorry. I need to divide and module 2 numbers without using '/' or '%'. But I must use pointers.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Originally Posted by Jan4185
    Yes, I'm sorry. I need to divide and module 2 numbers without using '/' or '%'. But I must use pointers.
    Division (and modulus) aren't but "glorified" subtraction.
    So just divide using subtraction and use a pointer:

    Code:
    int main(void) {
        printf("42 / 13 is %d\n", divide(42, 13));  /* functions divide() and modulus() */
        printf("42 % 13 is %d\n", modulus(42, 13)); /* left as an exercise for the reader */
        int *pointer; /* <== look: a pointer! */
        return 0;
    }
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    62
    Rep Power
    4
    You could also try using bitwise operators, for instance, this code here. I guess that you could send a memory pointer that points to an integer and do some low-level stuff with it, depending on the compiler, you can do something like this - remembering that the C convention means that the top of the stack will be y and x respectively. You may also need to consider word boundaries depending on the host processor.
    Code:
     
    void multiply(int &x, int &y)
    {
           #asm
            PUSH    DE
            PUSH    HL
            // Do low-level operations here
            POP      HL
            POP      DE
            RET
            #endasm
    }
    That's Z80, but x86 assembly should be similar. I think all processors have the equivalent of push and pop anyway, and modern processors will have multiply instructions, though it's usually quicker to use bitwise operators. Remember, most processors use the first in, last out thingy when pushing and pulling from the stack.

    Oh, and the equivalent of the #asm tag on some comilers is __asm( /** code here */ ); or something, but don't quote me on that.

    Good luck!

    Shaun.
    Last edited by Shaun_B; October 31st, 2012 at 03:06 PM. Reason: Forgot the closing tag... and forgot with registers I used too :-\
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by Jan4185
    Yes, I'm sorry. I need to divide and module 2 numbers without using '/' or '%'. But I must use pointers.
    No, thank you. My opinion on this type of task is that asking students to implement something without using X, or only using X, Y, and Z, make for good problem-solving exercises. (Heck, they even sort of prepare you for real life, where you might have to work under restrictions.) However, saying that you must use X is idiotic. I'd be willing to help you understand division algorithms, but I have no desire to play the game where we're forced to use pointers somewhere in our implementation.

IMN logo majestic logo threadwatch logo seochat tools logo