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 need to write a function to find factorials of numbers the catch is i can only use + or ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Unhappy Function To Find Factorials!!! Please Help

    i need to write a function to find factorials of numbers the catch is i can only use + or - for and while loops..

    eg 5! = 5 x 4 x 3 x 2 x 1 = 120

    i cannot use any * in it;

    this is what i have so far


    the factorial has been entered in another function and is passed to this one

    void find_factorials(int factorial)
    {
    int count = 0;
    int answer = 0;
    int i = 1;
    int n = factorial;


    for(n = factorial ; n>0 ;n--)
    {


    for(count = factorial - i; count >0; count--)
    {
    answer = answer + factorial;

    }
    i++ ;

    }
    }


    can someone please help me thanks heeps

  2. #2
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    well lets take this code segment and plug in some values to see what happens.
    if you enter 5 for the factorial the outer loop sets up the decreasing values the inner loop would set count to 5-1 or 4 but i don't see where factorial is ever decreased?
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Not a good use of recursion, but it works
    Code:
    int calc_fact(int num)
    {
      if(num < 2)
        return 1;
      return num*calc_fact(num-1);
    }
    If you follow the code you can get a good sense of how to do it iteratively anyway. I couldn't make it too easy for you
    If you understand what you're doing, you're not learning anything.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Please dont cross post as it's against board rules.

    All threads mow merged to this one

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    sorry about that Fordy im new to this

    itsme86 :

    i can see how your program works but im not aloud to use any multiplication in the function or pointers as i have to chage it into MIPS assembely language later:

    therefore

    - i have to make the function only use addition or subtractions

    -i cant use precompliled c functions such as pow()

    - and im only aloud to use while loops, if statements and\or for loops


    any ideas people please help


    thanks alot

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Smile

    Code:
    for(n = factorial ; n>0 ;n--)
    	{
    
    
    		for(count = factorial - i; count >0; count--)
    		{
    			answer = answer + factorial;
    			
    		}
    		i++ ;
    		
    	}
    my find_factorial function isnt workin


    i need to do something like this to calculate the factorial

    5+5+5+5 = 20 ive done this step
    20+20+20=60
    60+60=120
    120

    note that any factorial can be entered and it should go right down to the bottom step


    please reply if you can help me in any way to fix my function

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,308
    Are you allowed to cheat by writing your own multiplication function?

    Code:
    int multiply(int a, int b) {
    	int ret = 0;
    	for (int i = 0; i < a; i++)
    		ret += b;
    	return ret;
    }
    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

  8. #8
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    Code:
    for(n = factorial ; n>0 ;n--)
    	{
    
    
    		for(count = factorial - i; count >0; count--)
    		{
    			answer = answer + factorial;<-factorial variable
                                          is never decremented maybe u want to use n instead?  
    			
    		}
    		i++ ;
    		
    	}
    Last edited by manofsteel972; 08-17-2004 at 07:40 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It's a tad off topic, but here's a cool and handy trick. These two lines do the same thing:

    Code:
    answer = answer + factorial;
    answer += factorial

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Your problem lies in the fact that you are adding the wrong number. Instead of

    5+5+5+5=20
    20+20+20=60 etc

    you get

    5+5+5+5=20
    20+5+5=30 etc

    Changing to n instead of factorial doesn't help, then you just get

    5+5+5+5=20
    20+4+4=28 etc

    Keep the value of answer and only add that, like so:

    Code:
    for(n = factorial ; n>0 ;n--)
    {
                                    
         for(count = factorial - i; count >0; count--)
         {
             answer = answer + temp;		
         }
         i++ ;
         temp=answer;
    		
    }
    Haven't tested it, but it seems to me something like this should work.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Smile

    hey guys thanks for all your help... ive tryed all the codes and pj yeltons seems to be the one which is almost working but still coming up with the wrong results

    5! should =120 showing 125
    4! should = 24 showing 64
    etc

    ive tryed playing around with it, changing the values and for loops but still no succes. ive put it into a seperale little main function below, just incase anyone has some free time to cut n paste it into their c++ compiler..

    thanks guys


    Code:
    void main()
    {
    	printf("enter a num:");
    	scanf("%d", &factorial);
    
    	int n, count, answer;
    	int i = 1;
    	int temp = factorial;
    	
    
    	for(n = factorial ; n>0 ;n--)
    	                       
                             for(count = factorial - i; count >0; count--)
                             {
                                         answer = answer + temp;	
    		 
                             }
    
    	          i++;
                              temp = answer;
    	
    	          printf("\n\nThe factorial of %d! is %d\n\n",factorial, answer);
    
    }
    Last edited by adzza69; 08-18-2004 at 03:55 AM.

  13. #13
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I was just trying to give you a hint earlier instead of just giving you an answer.
    analyze the problem and break it up into smaller chunks.
    first you have a number that you start with in this case 5
    second you want to mulitiply times (itself-1) 5*(5-1) to generalize just replace with variable
    so you have result= x*(x-1);
    next you want to decrease the original value by 1 (x--; )and multiply it times the result

    result=result*(x-1)
    repeat the process until x=0;


    input x;
    result=x;
    loop-test( x>0)
    result=result*(x-1)
    x--;

    now just break the multiplication down into multiple additions
    the number of additions is (x-1)
    so loop x-1 times
    sum=sum+result

    when you put it all together it should give you a better picture
    Code:
    psuedo code
    
    input factorial;
    result=factorial;
    while(factorial>0) 
    {
          for (int count=(factorial-1);  count>0; count--)
               {
                  int sum=sum+result;
                }
    
                result=sum;
                sum=0;--  clear value stored in sum;
                factorial--;
     }
    [edit]
    my bad since 1 multiplied times any number is that number we only need to decrement to (factorial>1) instead of (factorial>0)
    [/edit]
    Last edited by manofsteel972; 08-18-2004 at 07:31 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  14. #14
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    [edit]Im dumb and can't read questions[/edit]
    Oh and while we are at it void main???
    Last edited by prog-bman; 08-18-2004 at 06:55 AM.
    Woop?

  15. #15
    Registered User
    Join Date
    Aug 2004
    Posts
    11
    thanks heaps eveyone.....

Page 1 of 2 12 LastLast
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