How to Solve this function F=4(3n+m)!! +6 , if "n" and "m" are given , by using "goto" command.
Printable View
How to Solve this function F=4(3n+m)!! +6 , if "n" and "m" are given , by using "goto" command.
Eh, are you supposed to write a C++ program that computes that double factorial? If so, the requirement to use goto is very strange, unless you already know how to do this without using goto and can show us a program in which you do so.
The use of the goto statement is strongly discouraged from use and should only even be considered for use in quite exceptional circumstances.
Use of the goto statement is definitely not required for computation of factorials.
Is this a homework exercise?
See http://en.wikipedia.org/wiki/Factorial for details
There are two simple ways to evaluate a factorial - iteration or recursion.
A possible implementation of these is
See if you can understand how these work. Try stepping through the code using the debugger.Code:#include <iostream>
using namespace std;
unsigned int ifact(int n)
{
unsigned int f = 1;
for (; n > 1; f *= n--);
return f;
}
unsigned int rfact(int n)
{
return (n > 1) ? n * rfact(n - 1) : 1;
}
int main()
{
int f;
cout << "Enter number for factorial: ";
cin >> f;
cout << "Factorial " << f << " by iteration is " << ifact(f) << endl;
cout << "Factorial " << f << " by recursion is " << rfact(f) << endl;
return 0;
}
So from this it is trivial to evaluate your original expression. Note that for large values factorial evaluation can quickly overflow the maximum value that can be contained in a 32 bit int.
Then please post the whole original question exactly as it was given to you. What's the name of the course and at what educational level?
My guess is that you are holding back information or have misunderstood something. Not even a stone age professor would require you to write a program consisting of goto's alone because that's not possible.
What do you mean, "not possible"?
Agreed. I'm also wondering if there isn't a typo in there. If both n and m are 1, then you'll overflow your integers (4! => 24, 24! > 2^^64 ).
Also, what exactly does it mean to "Solve" this function?
Code:void your_professor()
{
cout << "no kidding";
}
int main()
{
goto idiot;
idiot:
your_professor();
return 0;
}
I mean that restricting a language to the use of goto's only would render it not Turing complete. For Turing completeness an imperative language requires conditional branching. It means in addition to goto's the professor must also allow if's to be used otherwise a program in the language is not possible.
Yeah, it isn't clear if "by using goto command" means that there has to be sensible use of a goto somewhere, or if it means that only goto can be used. However, without a label, one cannot use a goto, so presumably razzle's interpretation was not intended otherwise not even the use of goto would be possible (besides the fact that one would not be allowed to define the global main function, or declare any variables, etc).
In C++, it IS entirely possible to write conditional code without ever using the the keyword if. It just needs some nifty use of what's available. however, it does require being able to evaluate both sides of the branch. but with lambda's you can circumvent even that.
turing completeness btw, has NOTHING to do with using or not using goto. What you do need is conditional branching commongly described as the language having "if" and "goto" or at the very least, a "branch on zero". (every conditional can be delegated as a branch on zero type test).
a program is not turing complete or not
combining conditional-without-if with conditional-goto however... now that's a challenge I'm not sure is solvable.
can you goto out of a lambda ? would have to try.
Below you'll find the function to calculate a factorial using goto.
call as
int f = fac(7); // calculate 7! which is 5040
it's 3x as awesome because it uses 3 goto's !!! (yes 3 exclamations there).
Give this to your professor and watch his head explode.
note it uses int, so it'll overflow when you call it to calculate more than 12!Code:int fac(int it) // fac (it)
{
int fac(it); // fac (it) again
if (!it) // if not it ?
goto it; // then it anyway
fac_it: // fac it !
--it; // we have too much it
fac*=it? // fac it ?
it: // it !
1; // one? yes, one.
if (!it) // if not it ?
goto it; // then it anyway
goto fac_it; // fac it
it: // it !
return fac; // fac
}
modify the 3 (yes 3 !!! (yes 3 exclamations)) occurences of int to __int64 to allow to calculate up to 24!
if you need more than 24!, then you'll beed a big int library of some sorts since no integer types are available. (you could use double, but that'll cause inaccuracies).
fac it competition !!!
possibly overflowing, fully portable, just one goto and no 'if' or '?:' :D :
Code:int fac( int it )
{
int fac(it);
struct fac { virtual void it(){} } again, *and_again_and[] = { &again, 0 };
try { it: typeid(**( and_again_and + !--it ));
fac*=it;
goto it;
} catch(...){}
return fac;
}