"goosen_cug" <go*******@gmail.com> wrote:
#include <iostream>
using namespace std;
int fn(int a);
void main(void)
{
cout<<"the n is ";
cout<<fn(5);
}
int fn(int a)
{
int n;
for(int i=5;;i++)
{
n=i;
if(n==(n-n%5)/5+n%5+fn(n-1))
cout<<n<<endl;
break;
}
return n;
}
computer the the value of n
That's "wrong" in many ways. BY FAR THE WORST WAY in which it is
"wrong" is its lack of any comments. What does it do??? How does
it do it??? Without that, it's completely unmaintainable, because
the maintainer hasn't a clue as to what constitutes "desired" vs.
"undesired" behavior.
Secondly, main() must return int:
int main(void)
int main(int ArgCount, char* ArgStrings[])
Thirdly, your function int fn(int a) doesn't actually use it's
parameter a. So fn(5), fn(32), or fn(-8763) would all return
the same thing, if they return at all... which they won't.
Fourthly, n mirrors i on the very first line of fn(), and yet
i is never used after that; so why not just use i instead of
n?
Fifthly, since the last line of your for loop is "break;",
the loop will only execute once, so why have a for loop at all?
Sixthly, the condition of your if() evaluates to 1-fn(n-1),
which evaluates to 1-(1-(1-(1-(... (to infinity). Even if
you get this ill-begotten mess to compile, it will crash
your stack within microseconds. If you're going to use
recursion, you MUST establish a fool-proof limit on number
of recursive levels, or you'll get run-away recursion, as
you do here.
--
Robbie Hatley
Tustin, CA, USA
lonewolfintj atsign pacbell period net
home period pacbell period net slantbar earnur slantbar