C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 31st, 2012, 12:42 PM
Jan4185 Jan4185 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 Jan4185 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 24 sec
Reputation 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

Reply With Quote
  #2  
Old October 31st, 2012, 02:02 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
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


No.

Reply With Quote
  #3  
Old October 31st, 2012, 02:10 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
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?

Reply With Quote
  #4  
Old October 31st, 2012, 02:11 PM
Jan4185 Jan4185 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 Jan4185 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 24 sec
Reputation Power: 0
Quote:
Originally Posted by bdb
No.


Are you sure ? this is my HW..
tnx anyway..

Reply With Quote
  #5  
Old October 31st, 2012, 02:16 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
Originally Posted by Jan4185
Are you sure ? this is my HW..
tnx anyway..


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 * ...)? ???

Reply With Quote
  #6  
Old October 31st, 2012, 02:18 PM
Jan4185 Jan4185 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 7 Jan4185 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 57 m 24 sec
Reputation Power: 0
Quote:
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.

Reply With Quote
  #7  
Old October 31st, 2012, 02:30 PM
bdb bdb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 156 bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level)bdb User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 32
Quote:
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;
}

Reply With Quote
  #8  
Old October 31st, 2012, 03:02 PM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 62 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 1 h 9 m 54 sec
Reputation Power: 2
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 :-\

Reply With Quote
  #9  
Old October 31st, 2012, 07:34 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,938 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 43 m 16 sec
Reputation Power: 1312
Quote:
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help with pointers

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap