Sign in to follow this  

output not desirable

This topic is 4305 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
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

This topic is 4305 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this