3 function aside from main in factorial function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • masqwerty16
    New Member
    • May 2015
    • 8

    3 function aside from main in factorial function

    i have a problem on 3 function aside from main in factorial function and i wrote the code and the program worked perfectly on some numbers but it crashed on others this is the message i get: "MASONG11 cause a Stack Fault in module MASONG11.EXE at 0002:0076. Choose close. MASONG11 will close."

    any suggestions?? please answer.. i actually needed the answer please..

    here's my code:

    #include <iostream.h>

    int input (int n);
    int factorial(int n);
    int display (int n);

    int main() {
    int n;

    input (n);
    factorial (n);
    display (n);

    return 0;
    }

    int input (int n)
    {
    cout<<"Enter a number to find factorial: ";
    cin>>n;

    return n;
    }

    int factorial(int n)
    {
    if (n>1)
    {
    return n*factorial(n-1);
    }
    else
    {
    return 1;
    }
    }

    int display (int n)

    {
    cout<<"Factoria l of "<<n<<" = "<<factorial(n) ;

    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:
    Code:
    int factorial(int n)
    {
    	 if (n>1)
    	 {
    		  return n*factorial(n-1);
    	 }
    	 else
    	 {
    		  return 1;
    	 }
    }
    at the time n == 1 then n>1 is false so the function returns 1. This results in a function call to factorial(1) and the process repeats forever.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I don't see the infinite loop that @weaknessforcat s describes.
      • factorial(2): take the true leg and invokes factorial(1).
      • factorial(1): take the false leg and return 1.
      • The n==2 true leg then computes 2*1 and returns 2.


      I'm not familiar with C++, will the input(n) call in main change the value of n in main?

      Do you see a pattern for which inputs work and which cause it to crash?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        @donbock:
        You are right. It's not a recursive loop problem. Thank you for pointing that out.

        I did debug it and it's a problem of function arguments being copies of the variables used to make the call. I changed the code to use references and the program works fine. I did not do range testing.

        Revised code:
        Code:
        #include <iostream>
        #include <sstream>
        
        using namespace std;
        
        int input(int& n);
        int factorial(int& n);
        int display(int& n);
        
        int main() {
        	int n = 0;
        
        	input(n);
        	factorial(n);
        	display(n);
        
        	return 0;
        }
        
        int input(int& n)
        {
        	cout << "Enter a number to find factorial: ";
        	cin >> n;
        
        	return n;
        }
        
        int factorial(int& n)
        {
        	if (n>1)
        	{
        		int x = n;
        		--x;
        		return n*factorial(x);
        	}
        	else
        	{
        		return 1;
        	}
        }
        
        int display(int& n)
        
        {
        	cout << "Factorial of " << n << " = " << factorial(n);
        
        	return 0;
        }

        Comment

        • masqwerty16
          New Member
          • May 2015
          • 8

          #5
          @weaknessforcat s thank you..!!! maybe my formula is wrong...

          Comment

          Working...