0

this program is wrong ?

| |
2006/06/29    13:57    6176    生活 » 程序人生 不指定
Q:
this program is wrong ?





#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


* goosen_cug:



A:

- Hide quoted text -
- Show quoted text -

> #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



Yes, it's wrong, in the sense of "should not compile", if it's meant to
be C++.

However, some compilers will (incorrectly) let you get away with it.


See <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.3>.



goosen_cug 写道:




- Hide quoted text -
- Show quoted text -

> #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



I think the function "fn()" will go forever.





- Hide quoted text -
- Show quoted text -

"goosen_cug" <goosen...@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.
Tags: | 引用(0)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]