Click to See Complete Forum and Search --> : could you check this code?
blaqdawgdx_k9s
April 8th, 2005, 04:59 AM
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
cilu
April 8th, 2005, 05:13 AM
So, this time you have worked something:
#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:
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.
if(num < 0)
{
cout << "Factorial of a negative integer is undefined\n";
}
else
{
fact(num);
}
Now this:
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?
blaqdawgdx_k9s
April 8th, 2005, 05:22 AM
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 :(
cilu
April 8th, 2005, 05:30 AM
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.
int fact(int num)
{
if(num <= 1)
return 1;
else
return num * fact(num-1);
}
blaqdawgdx_k9s
April 8th, 2005, 05:34 AM
I think i get it....you are an elite member...****
By the way what does 'cilu' mean? I it your nickname?
cilu
April 8th, 2005, 05:37 AM
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.
cilu
April 8th, 2005, 05:38 AM
By the way what does 'cilu' mean? I it your nickname?
Cilu means Cilu. And yes, it is my nickname...
blaqdawgdx_k9s
April 8th, 2005, 05:57 AM
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
cilu
April 8th, 2005, 06:12 AM
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)
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?
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:
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:
#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.
blaqdawgdx_k9s
April 8th, 2005, 06:19 AM
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...
cilu
April 8th, 2005, 06:34 AM
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.
blaqdawgdx_k9s
April 8th, 2005, 07:29 AM
hey cilu i think i got it man!
#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;
}
cilu
April 8th, 2005, 07:58 AM
hey cilu i think i got it man!
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:
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;
}
blaqdawgdx_k9s
April 8th, 2005, 08:26 AM
I've modified it again a little bit, see for yourself.
#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;
}
cilu
April 8th, 2005, 08:31 AM
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.
blaqdawgdx_k9s
April 8th, 2005, 09:02 AM
Thanks for the advice cilo...
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.