By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

this program is wrong ?

P: n/a
#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

Jun 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* goosen_cug:
#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>.

--
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?
Jun 28 '06 #2

P: n/a

goosen_cug 写道:
#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.

Jun 28 '06 #3

P: n/a
"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
Jun 28 '06 #4

P: n/a
co******@gmail.com wrote:
goosen_cug 写道:
#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.


I think it only loops once.

Ben
Jun 28 '06 #5

P: n/a
Robbie Hatley wrote:
"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.

Unless it has unit tests..

--
Ian Collins.
Jun 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.