this program is wrong ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • goosen_cug

    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

  • Alf P. Steinbach

    #2
    Re: this program is wrong ?

    * goosen_cug:[color=blue]
    > #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[/color]

    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.c om/c++-faq-lite/newbie.html#faq-29.3>.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • counthlk@gmail.com

      #3
      Re: this program is wrong ?


      goosen_cug 写道:
      [color=blue]
      > #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[/color]

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

      Comment

      • Robbie Hatley

        #4
        Re: this program is wrong ?

        "goosen_cug " <goosen108@gmai l.com> wrote:
        [color=blue]
        > #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[/color]

        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


        Comment

        • benben

          #5
          Re: this program is wrong ?

          counthlk@gmail. com wrote:[color=blue]
          > goosen_cug 写道:
          >[color=green]
          >> #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[/color]
          >
          > I think the function "fn()" will go forever.
          >[/color]

          I think it only loops once.

          Ben

          Comment

          • Ian Collins

            #6
            Re: this program is wrong ?

            Robbie Hatley wrote:[color=blue]
            > "goosen_cug " <goosen108@gmai l.com> wrote:
            >
            >[color=green]
            >>#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[/color]
            >
            >
            > 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.
            >[/color]
            Unless it has unit tests..

            --
            Ian Collins.

            Comment

            Working...