473,507 Members | 6,459 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jun 28 '06 #1
5 1209
* 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

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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2818
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
27
3197
by: hpy_awad | last post by:
I wrote that program from a book that my compile that program correctly under C++ compiler but I got those errors for compiling under unix !! Errors- --------...
92
9771
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
11
2238
by: Madison Kelly | last post by:
Hi all, I am new to the list and I didn't want to seem rude at all so I wanted to ask if this was okay first. I have a program I have written in perl which uses a postgresSQL database as the...
27
2048
by: cj | last post by:
I run this program and to exit click the X in the upper right corner. But apparently it isn't really ending the program. If I return to VB and make changes then try to rebuild the app it says the...
0
2406
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical...
0
1063
by: Steve | last post by:
I've created a new VB 2005 project that I've been doing preliminary test installs for. In the development project, I've set the applications icon to an ico file. In the installation project I've...
2
1558
by: Chedda | last post by:
everytime i try to compile and run the program, on the compiler it states on line where it says ofstream outfile("c:\\Users\\Michael\\Desktop\\Statistics.txt");#include <iostream> that theres an...
14
2637
by: jmDesktop | last post by:
Hi, I'm trying to learn Python. I using Aquamac an emac implementation with mac os x. I have a program. If I go to the command prompt and type pythong myprog.py, it works. Can the program be...
4
1952
by: hirsh.dan | last post by:
i have a functions that writes information to a file. inside that function i have a line in which i call another function. if this line is executed, nothing is written to the file, but if i remark...
0
7110
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.