CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Apr 2005
    Posts
    38

    could you check this code?

    Hi guys and girls

    I've got another problem... I've coded the solution for this question but it's not working and i can't figure out why. Can you guys help me? Thanks if you can...

    *****************************************************************
    A factorial number is a number multiplied by every factor between 1 and the number, inclusive. For instance, 3 factorial is 3 * 2 * 1, which is equal to 6. Mathematically, the factorial function is denoted using an exclamation mark (!).

    A negative factorial number is undefined. Your task is as follows. Design a C++ program that prompts the user to enter a positive integer number. Use a for loop to calculate the factorial value of that number. If the user enters a negative number, display a message indicating that the program calculates only factorials of positive numbers. Otherwise, display the result of the calculation.

    You must define at least one function called:

    factorial(x)

    where this function will return an integer as the result of the computation of x!. Hence, calling this function with factorial(3) will return 6.

    Attached is what i coded
    Attached Files Attached Files

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    So, this time you have worked something:
    Code:
    #include <iostream>
    using namespace std;
    
    int fact(int num);
    
    int main()
    {
    	int num;
    	
    	cout << "Enter a number: ";
    	cin >> num;
    	cout << "num\n";
    	
    	if(num < 0)
    	
    		cout << "Factorial of a negative integer is undefined\n";
    	
    	else
    	
    		do
    			fact(num);
    		while(num > 0);
    		
    	return 0;
    }
    
    int fact(int num)
    {
    	if(num == 0)
    		return 0;
    	else
    		return num * fact(num);
    		
    }
    First error is here:
    Code:
    do
    	fact(num);
    while(num > 0);
    What is the point of the while? num never decreases, so you have an infinite loop.
    There you should put a that code between {}, to increase readability.
    Code:
    if(num < 0)
    {
    	cout << "Factorial of a negative integer is undefined\n";
    }	
    else
    {	
    	fact(num);
    }
    Now this:
    Code:
    int fact(int num)
    {
    	if(num == 0)
    		return 0;
    	else
    		return num * fact(num);
    		
    }
    Let's say we call it like func(3). What happens here? It enters the function, num is 3 so it goes to the else and calls fact(3) again, and then again, and then again. Another infinite loop. So you must call fact() with another parameter than num. What would that be?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2005
    Posts
    38

    Unhappy Re: could you check this code?

    hi cilu,

    so do i decrement num so that each time the functions is called the previous number will times one less than the number... do you get what i mean? because i sure don't

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    Code:
    int fact(int num)
    {
    	if(num == 0)
    		return 0;
    	else
    		return num * fact(num);
    		
    }
    First, you call it with fact(3), in my example. Then it should be called with fact(2), and then with fact(1).
    When the first time fact() is entered, num is 3, and fact should be called with argument 2, how should you call it? fact(num-1), obviously.

    And then, you must do something else. If you finally return 0, whatever you multiply to 0 gives 0. So the last call must return 1.
    Code:
    int fact(int num)
    {
    	if(num <= 1)
    		return 1;
    	else
    		return num * fact(num-1);
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Apr 2005
    Posts
    38

    Talking Re: could you check this code?

    I think i get it....you are an elite member...****

    By the way what does 'cilu' mean? I it your nickname?

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    n! = (n-0)(n-1)(n-2)...(n-(n-1))

    or

    (R1) n! = n*(n-1)!
    (R2) (n-1)! = (n-1)(n-2)!
    ...
    (Rn) (n-(n-2))! = (n-(n-2))*(n-(n-1))! or 2! = 2*1

    taking (R1), if you replace n with n-1 you get (R2)
    (R1) n! = n*(n-1)!
    n = n-1
    => (r2) (n-1)! = (n-1)(n-2)!

    if you put this into a function ou get

    fact(n) = n * fact(n-1)

    but you must stop when n becomes 1.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    Quote Originally Posted by blaqdawgdx_k9s
    By the way what does 'cilu' mean? I it your nickname?
    Cilu means Cilu. And yes, it is my nickname...
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    Join Date
    Apr 2005
    Posts
    38

    Re: could you check this code?

    so do i have to do something like

    while(num >= 1)

    or something like that and by the way could you tell me how to do that quote thing like you

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    and by the way could you tell me how to do that quote thing like you
    Use tags: [.QUOTE]XXX[./QUOTE] (but without the points)
    For code tags: [.CODE]XXX[./CODE] (but without the points)

    Quote Originally Posted by blaqdawgdx_k9s
    so do i have to do something like

    while(num >= 1)

    or something like that
    NO, No, No! No while. Where do you want to put that while? Here?
    Code:
    else
    {
    	do
    		fact(num);
    	while(num > 0);
    }
    I've already told you that this is an infinite loop. fact does not alter the num variable here, because it is passed by value, that means its value is assign to the parameter. num remains the same. Hre is something to understand passing by value and by reference:
    Code:
    void f_value(int n)
    {
      n++;
      cout << n << endl;
    }
    
    void f_ref(int& n)
    {
     n++;
     cout << n << endl;
    }
    
    int main()
    {
     int n = 0;
     cout << "before val: " << n << endl;
     f_value(n);  // print 1
     cout << "after val: " << n << endl; // prints 0
    
     n = 0;
     cout << "before ref: " << n << endl;
     f_ref(n);  // prints 1
     cout << "after ref: " << n << endl;  // prints1
    }
    Final:
    Code:
    #include <iostream>
    using namespace std;
    
    int fact(int num);
    
    int main()
    {
    	int num;
    	
    	cout << "Enter a number: ";
    	cin >> num;
    	cout << "num\n";
    	
    	if(num < 0)
    	{	
    		cout << "Factorial of a negative integer is undefined\n";
    	}
    	else
    	{
    		cout << fact(num) << endl;
    	}
    
    	return 0;
    }
    
    int fact(int num)
    {
    	if(num <= 1)
    		return 1;
    	else
    		return num * fact(num-1);
    }
    My advice: start reading some serious C++ documentation. Here are some good books.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  10. #10
    Join Date
    Apr 2005
    Posts
    38

    Re: could you check this code?

    Thanks cilu but you gotta understand this is my 1st year, and it's only 5 weeks in plus i have never done any programming before...

  11. #11
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    Quote Originally Posted by blaqdawgdx_k9s
    Thanks cilu but you gotta understand this is my 1st year, and it's only 5 weeks in plus i have never done any programming before...
    I understand. That's why I replyed so many times here. But you should start reading some good C++ books. Factorial is a classic example for learning recursion. There are hundreds of thousand of sample codes and debates on it on the net. Do some google research.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  12. #12
    Join Date
    Apr 2005
    Posts
    38

    Talking Re: could you check this code?

    hey cilu i think i got it man!
    Code:
    #include <iostream>
    using namespace std;
    
    int fact(int num);
    
    int main()
    {
    	int num;
    	int factorial;
    	
    	cout << "Enter a number: ";
    	cin >> num;
    	
    	if(num < 0)
    	
    		cout << "Factorial of a negative integer is undefined\n";
    	
    	else
    	{
    		factorial = fact(num);
    	}
    	
    	cout << "Factorial of " << num << " is " << factorial << endl;
    		
    	return 0;
    }
    
    int fact(int num)
    {
    	if(num > 0)
    		return num * fact(num-1);
    	else 
    		return 1;	
    }

  13. #13
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    Quote Originally Posted by blaqdawgdx_k9s
    hey cilu i think i got it man!
    Code:
    int main()
    {
    	int num;
    	int factorial;
    	
    	cout << "Enter a number: ";
    	cin >> num;
    	
    	if(num < 0)
    	
    		cout << "Factorial of a negative integer is undefined\n";
    	
    	else
    	{
    		factorial = fact(num);
    	}
    	
    	cout << "Factorial of " << num << " is " << factorial << endl;
    		
    	return 0;
    }
    Almost. Your program has a problem. It prints "factorial of .. is ..." no matter the number input by the user was ok or not. And if not, variable factorial is not assign to anything and and undefined value. That's way you should alsways initialize your varaibles. So, I would put you code like this:
    Code:
    int main()
    {
    	int num = 0;
    	
    	cout << "Enter a number: ";
    	cin >> num;
    	
    	if(num < 0)
    	
    		cout << "Factorial of a negative integer is undefined\n";
    	
    	else
    	{
    		int factorial = fact(num);
    		cout << "Factorial of " << num << " is " << factorial << endl;
    	}
    		
    	return 0;
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  14. #14
    Join Date
    Apr 2005
    Posts
    38

    Talking Re: could you check this code?

    I've modified it again a little bit, see for yourself.

    Code:
    #include <iostream>
    using namespace std;
    
    int fact(int num);
    
    int main()
    {
    	int num;
    	int factorial;
    	
    	cout << "Enter a number: ";
    	cin >> num;
    	
    	if(num < 0)
    	{
    		cout << "Factorial of a negative integer is undefined\n";
    	}
    	else
    	{
    		factorial = fact(num);
    		cout << "Factorial of " << num << " is " << factorial << endl;
    	}
    	
    		
    	return 0;
    }
    
    int fact(int num)
    {
    	if(num > 0)
    		return num * fact(num-1);
    	else 
    		return 1;	
    }

  15. #15
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: could you check this code?

    Quote Originally Posted by blaqdawgdx_k9s
    I've modified it again a little bit, see for yourself.
    That's better.

    Listen, you said you are a beginner, so take my advice. I tell you this from (very frustrating) experience. Always initialize your variables. This is a simple progran and will not cause you troubles. But when you write programs of thousands or lines, or hundreds of thousand of lines, uninitialize variables can cause you great headaches. So get used to do this, initialize your variables even if you think you don't have to.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured