Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

output not desirable

This topic is 4521 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why is the output not coming as desired...I expected 54321 but its coming out to be 55555. #include <iostream> using namespace std; int f(int x[],int count) { x[0]++; count--; if (count==0) return x[0]; cout<<f(x,count); return x[0]; } int main() { int y=0,x[1]={0},count=5; cout<<f(x,count); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
You should be expecting 55555 from your code. The functions aren't returning (and thus cout<< isn't being called) until after the last iteration, so they all output 5. BUT, you should actually be expecting 12345, not 54321, since your adding, not subtracting.

To fix it just change the line

cout<<f(x, count);


to

cout<<x[0];
f(x, count);


I forgot to ask why you're using an array when you only use the first node?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!