473,661 Members | 2,464 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 1218
* 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.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?
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*******@gmai l.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*******@gmai l.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
2826
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
27
3251
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- -------- part10_iti_r01_ch10_verbo_menu_driven_programs.c: In function `main': part10_iti_r01_ch10_verbo_menu_driven_programs.c:6: warning: declaration of `argc' shadows a parameter part10_iti_r01_ch10_verbo_menu_driven_programs.c:7: warning:
92
9845
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 someone helps me... Urgent - Please reply soon ! Thanks, Raghu
11
2263
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 backend. The program works but the performance is really bad. I have been reading as much as I can on optimizing performance but still it isn't very reasonable. At one point I had my program able to process 175,000 records in 16min 10sec on a...
27
2071
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 exe is still in use--I find it is still a process in Task Manager. What do I need to do to make clicking that X actually end the program? Public Class Form1 Inherits System.Windows.Forms.Form
0
2418
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 price of a stock. The program will allow users to query the stock price of a particular date and output its statistics (mean price, median price and standard deviation) for any specified period. Your program must be menu driven and works as follows.
0
1081
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 set the shortcut files target to the .exe file. I've also set the shortcut icons and the add/remove program icon for the project to the icon in the target .exe file as well. When I install the product on the target machine, all of the shortcuts...
2
1565
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 error, why is that? someone help, i dont know if theres a logical error or just some other error, someone please help..... this is the program
14
2650
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 run from within the editor or is that not how development is done? I ask because I was using Visual Studio with C# and, if you're familiar, you just hit run and it works. On Python do I use the editor for editing only and then run the program from...
4
1961
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 that line, it is written. i wanna understand what's wrong and fix it, but can't find what is wrong. also this line is mandatory in my code. the code attached is in the link, minimized to the needed info, a bit different then the way it's...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8343
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8855
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7364
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.