Thread: Why zeros?

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Why zeros?

    Code:
    I attempted to post this same request earlier but i am not sure if it went through. I appologise for dublication but, I need help please.
    
    // factorial program
    
    #include <iostream>
    using namespace std;
    
    int factorial(int num);
    
    int main(){
    
    int n;
    cout << "Enter a number and press ENTER: "
    cin >> n;
    cout << " Factorial of this number is: " << factorial(n);
    return 0;
    }
    int factorial(int n){
    int i;
    int fact = 0;
    for(i = 0; i <= n; i++)
    fact = fact * i;
    return fact;
    }
    
    although the program runs without an error message it produces zero for a result.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    The first time thru the loop i is zero and when multiplying by zero the result is zero. In subsequent runs thru the loop fact is zero meaning multiplying fact by i produces zero everytime.

  3. #3
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    You really should be looping from the n value, down, too...

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    I vote for recursion: (by herbert Schildt)
    Code:
    int factorial(int n)
    {
    int answer;
    if(n==1) return 1;
    answer = factorial(n - 1) * n;
    return answer;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by CodeMonkey
    I vote for recursion: (by herbert Schildt)
    Code:
    int factorial(int n)
    {
    int answer;
    if(n==1) return 1;
    answer = factorial(n - 1) * n;
    return answer;
    }
    Good call!

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by dalibi
    I attempted to post this same request earlier but i am not sure if it went through. I appologise for dublication but, I need help please.
    Look at your code. Run through a loop or two by hand. Start with zero, multiply by anything you want to: result will always be zero. Right?


    It wouldn't, if you change this:
    Code:
    int fact = 0;
    for(i = 0; i <= n; i++)
    to this:

    Code:
    int fact = 1;
    for(i = 1; i <= n; i++) /* actually could use i = 2; i <= n; i++ */
    Regards,

    Dave

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Note also that this is basically the reason that 0! = 1, so that the recursive definition makes sense N! = N*(N-1)! Without a base, we would have no idea about the value of any factorial.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    2
    Thank you all very much. I appreciate it.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by CodeMonkey
    I vote for recursion: (by herbert Schildt)
    Code:
    int factorial(int n)
    {
    int answer;
    if(n==1) return 1;
    answer = factorial(n - 1) * n;
    return answer;
    }
    If you want to illustrate use of recursion, this might be a place to do it since this is relatively easy to understand (unlike lots of examples of recursion). Unfortunately this program has a bug that gives no answer for n = 0. What do I mean "no answer". Well run it.

    How you you debug it? Put something like this in the function:
    Code:
    int factorial(int n)
    {
      int answer;
      if(n==1) {
        return 1;
      }
      printf("In factorial(): n = %d\n", n);
      answer = factorial(n - 1) * n;
      return answer;
    }
    Now run it with n = 0.
    Of course it is easy enough to fix it to give the correct answer for n = 0.

    One reason that recursive functions are sometimes hard to debug:
    the program just runs and runs and runs until stack space is totally consumed, then the program exits ungracefully. (Sometimes leaving dynamically allocated memory just hanging there and unobtainable until you reboot your computer.)

    Maybe beginners are better off with iteration. Recursion can come later, when it is really appropriate.

    Regards,

    Dave
    Last edited by Dave Evans; 04-01-2005 at 10:45 PM.

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    That's what I get for using another's code.
    Code:
    int factorial(int n)
    {
    int answer;
    if(!n || n == 1) return 1;
    else if(n < 0) exit(EXIT_FAILURE); //or throw exception
    answer = factorial(n-1) * n;
    return answer;
    }
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by CodeMonkey
    That's what I get for using another's code.
    More specifically: that's what you get for using Herbert Schildt's code.

    Regards,

    Dave

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make itoa write the left zeros?
    By pajaro in forum C Programming
    Replies: 5
    Last Post: 03-10-2009, 12:48 PM
  2. leading zero's..... ( reading int )
    By sh4k3 in forum C Programming
    Replies: 4
    Last Post: 06-12-2007, 09:03 AM
  3. Write zeros in ansi c
    By deus in forum C Programming
    Replies: 14
    Last Post: 08-14-2006, 06:23 AM
  4. File contains only zero's.
    By hans19hans19 in forum C Programming
    Replies: 1
    Last Post: 07-21-2006, 10:09 AM
  5. display double w/o trailing zeros?
    By Navar in forum C Programming
    Replies: 10
    Last Post: 02-11-2006, 03:27 PM