Function To Find Factorials!!! Please Help

This is a discussion on Function To Find Factorials!!! Please Help within the C++ Programming forums, part of the General Programming Boards category; i would like to build off this post and ask if it is possible to write a division funciton using ...

  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 10: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,399
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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
    22,264
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,399
    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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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

Page 2 of 2 FirstFirst 12
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, 04:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 06:45 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21