Thread: Function To Find Factorials!!! Please Help

  1. #16
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i would like to build off this post and ask if it is possible to write a division funciton using only +,-,while,do,for,if,else?
    I like the witches math function, and saw the answer for the prior request at that point.
    Code:
    int multiply(int a, int b) {
    	int ret = 0;
    	for (int i = 0; i < a; i++)
    		ret += b;
    	return ret;
    }
    How could you modify that to do division?
    The only way i can think of would be to turn the value into a string array by using modulus by 10 or something like that. Is there a simple way to calculate this?
    Last edited by xviddivxoggmp3; 08-19-2004 at 09:20 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #17
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Actually, that code to multiply only works with two positive integers. For a more general solution:
    Code:
    int multiply(int a, int b) {
    	int ret = 0;
    	bool neg = false;
    	if (a<0) {
    		a = -a;
    		neg = !neg;
    	}
    	for (int i = 0; i < a; i++)
    		ret += b;
    	if (neg)
    		ret = -ret;
    	return ret;
    }
    To divide, you more or less reverse the process, except 0 is a special case.
    Code:
    int divide (int a, int b) {
       int ret = 0;
       bool neg = false;
       if (a == 0)
    	return 0;
       if (b == 0)
    	return 0;
       if (a<0) {
    	a = -a;
    	neg = !neg;
       }
       if (b<0) {
    	b = -b;
    	neg = !neg;
       }
       for(;a >= b;ret++)
          a -= b;
       if (neg)
    	ret = -ret;
       return ret;
    }
    Of course, note that dividing by 0 returns 0 instead of undefined. You'd probably want to throw an exception if you were going to use this for real.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #18
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You would add something like this to the very beginning of the function

    Code:
    if (a==0) ThrowException(DivideByZero)
    Except that looks like a function you'd find in Java...

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    that code to multiply only works with two positive integers
    For 2 non-negative integers, actually.
    Which makes sense, since the domain of the factorial function is the set of non-negative integers.

    I was hoping that adzza69 would see that if it was possible to implement one's own multiplication function, that it would be possible to 'manually inline' that function into one's own function for calculating factorials in this strange way.

    I think that using a form of linear interpolation would be faster than pianorain's algorithm, but then that would also be more difficult to implement, and choosing initial values would be tricky.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    pianorain,

    please correct me if i'm wrong, but looking at your solution for division.
    It seems that we do not concern ourselves with the remainder.
    Am I reading this correctly? I guess that would match the current division process w/o modules.
    I know nothing about assembley, so would that language allow the use of pointers?
    then we could set the pointers to the division value and the remainder.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  6. #21
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Aye, that's correct. Integer division returns the quotient without the remainder.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #22
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You can use % (modulus) to get just the remainder.

  8. #23
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    any assembly guru know if pointers or modules is allowed in that language.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM