Recursive Function equation












0















Forewarning, this is a homework assignment.
I am supposed to create a recursive function but I am doing this wrong. When I enter a 4 I am supposed to get a result of 16 from f(x) but I get -2. I don't really understand where I went wrong. Also I don't know if I am supposed to print my results inside of main or in f.



Write a program that queries the user for an integer value and uses a recursive



function that returns the value of the following recursive definition:




f(x) =x+3 if x <=0

f(x)=f(x-3)+(x+5) otherwise



My attempt:



#include <stdio.h>
int f(int x); //Prototype to call to f

int main(void) {
int n; //number the user will input

//Ask user to input data an reads it
printf("Enter a whole number: ");
scanf("%d", &n);

//Pointer for f
f(n);

//Prints results
printf("nn is %dn", n);
printf("f(x) is %dn", f(n));
return 0;
}

int f(int x) {
//Checks if equal to zero
if (x <= 0) {
x + 3;
}
//If not equal to zero then do this
else {
f(x - 3) + (x + 5);
}
}


Thank you all for the help, learned a lot from your comments and suggestions.
I was able to get it work I believe https://pastebin.com/v9cZHvy0










share|improve this question

























  • Every recursive function has (1) an exit condition, and (2) a recursive call. You provide no exit condition. Further, you need scanf("%d", &n) (note the '&') and you must validate the return, e.g. if (scanf("%d", &n) != 1) { /* handle error */ }. Your call to f(n); is not a // Pointer for f it is simply a function call (that makes little sense there). Think through how your set of recursive calls must exit. As it is the computation of (x-3)+(x+5) grows in size so x <= 0 cannot be the exit condition.

    – David C. Rankin
    Nov 28 '18 at 4:33













  • @DavidC.Rankin: The exit condition is x <= 0. It seems you may have missed the fact that f(x-3) calls f and (x+5) does not.

    – Eric Postpischil
    Nov 28 '18 at 4:36













  • Yes, I read it as f (x-3 + x + 5).

    – David C. Rankin
    Nov 28 '18 at 4:37
















0















Forewarning, this is a homework assignment.
I am supposed to create a recursive function but I am doing this wrong. When I enter a 4 I am supposed to get a result of 16 from f(x) but I get -2. I don't really understand where I went wrong. Also I don't know if I am supposed to print my results inside of main or in f.



Write a program that queries the user for an integer value and uses a recursive



function that returns the value of the following recursive definition:




f(x) =x+3 if x <=0

f(x)=f(x-3)+(x+5) otherwise



My attempt:



#include <stdio.h>
int f(int x); //Prototype to call to f

int main(void) {
int n; //number the user will input

//Ask user to input data an reads it
printf("Enter a whole number: ");
scanf("%d", &n);

//Pointer for f
f(n);

//Prints results
printf("nn is %dn", n);
printf("f(x) is %dn", f(n));
return 0;
}

int f(int x) {
//Checks if equal to zero
if (x <= 0) {
x + 3;
}
//If not equal to zero then do this
else {
f(x - 3) + (x + 5);
}
}


Thank you all for the help, learned a lot from your comments and suggestions.
I was able to get it work I believe https://pastebin.com/v9cZHvy0










share|improve this question

























  • Every recursive function has (1) an exit condition, and (2) a recursive call. You provide no exit condition. Further, you need scanf("%d", &n) (note the '&') and you must validate the return, e.g. if (scanf("%d", &n) != 1) { /* handle error */ }. Your call to f(n); is not a // Pointer for f it is simply a function call (that makes little sense there). Think through how your set of recursive calls must exit. As it is the computation of (x-3)+(x+5) grows in size so x <= 0 cannot be the exit condition.

    – David C. Rankin
    Nov 28 '18 at 4:33













  • @DavidC.Rankin: The exit condition is x <= 0. It seems you may have missed the fact that f(x-3) calls f and (x+5) does not.

    – Eric Postpischil
    Nov 28 '18 at 4:36













  • Yes, I read it as f (x-3 + x + 5).

    – David C. Rankin
    Nov 28 '18 at 4:37














0












0








0








Forewarning, this is a homework assignment.
I am supposed to create a recursive function but I am doing this wrong. When I enter a 4 I am supposed to get a result of 16 from f(x) but I get -2. I don't really understand where I went wrong. Also I don't know if I am supposed to print my results inside of main or in f.



Write a program that queries the user for an integer value and uses a recursive



function that returns the value of the following recursive definition:




f(x) =x+3 if x <=0

f(x)=f(x-3)+(x+5) otherwise



My attempt:



#include <stdio.h>
int f(int x); //Prototype to call to f

int main(void) {
int n; //number the user will input

//Ask user to input data an reads it
printf("Enter a whole number: ");
scanf("%d", &n);

//Pointer for f
f(n);

//Prints results
printf("nn is %dn", n);
printf("f(x) is %dn", f(n));
return 0;
}

int f(int x) {
//Checks if equal to zero
if (x <= 0) {
x + 3;
}
//If not equal to zero then do this
else {
f(x - 3) + (x + 5);
}
}


Thank you all for the help, learned a lot from your comments and suggestions.
I was able to get it work I believe https://pastebin.com/v9cZHvy0










share|improve this question
















Forewarning, this is a homework assignment.
I am supposed to create a recursive function but I am doing this wrong. When I enter a 4 I am supposed to get a result of 16 from f(x) but I get -2. I don't really understand where I went wrong. Also I don't know if I am supposed to print my results inside of main or in f.



Write a program that queries the user for an integer value and uses a recursive



function that returns the value of the following recursive definition:




f(x) =x+3 if x <=0

f(x)=f(x-3)+(x+5) otherwise



My attempt:



#include <stdio.h>
int f(int x); //Prototype to call to f

int main(void) {
int n; //number the user will input

//Ask user to input data an reads it
printf("Enter a whole number: ");
scanf("%d", &n);

//Pointer for f
f(n);

//Prints results
printf("nn is %dn", n);
printf("f(x) is %dn", f(n));
return 0;
}

int f(int x) {
//Checks if equal to zero
if (x <= 0) {
x + 3;
}
//If not equal to zero then do this
else {
f(x - 3) + (x + 5);
}
}


Thank you all for the help, learned a lot from your comments and suggestions.
I was able to get it work I believe https://pastebin.com/v9cZHvy0







c recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 4:51







Alec Mauro

















asked Nov 28 '18 at 4:03









Alec MauroAlec Mauro

42




42













  • Every recursive function has (1) an exit condition, and (2) a recursive call. You provide no exit condition. Further, you need scanf("%d", &n) (note the '&') and you must validate the return, e.g. if (scanf("%d", &n) != 1) { /* handle error */ }. Your call to f(n); is not a // Pointer for f it is simply a function call (that makes little sense there). Think through how your set of recursive calls must exit. As it is the computation of (x-3)+(x+5) grows in size so x <= 0 cannot be the exit condition.

    – David C. Rankin
    Nov 28 '18 at 4:33













  • @DavidC.Rankin: The exit condition is x <= 0. It seems you may have missed the fact that f(x-3) calls f and (x+5) does not.

    – Eric Postpischil
    Nov 28 '18 at 4:36













  • Yes, I read it as f (x-3 + x + 5).

    – David C. Rankin
    Nov 28 '18 at 4:37



















  • Every recursive function has (1) an exit condition, and (2) a recursive call. You provide no exit condition. Further, you need scanf("%d", &n) (note the '&') and you must validate the return, e.g. if (scanf("%d", &n) != 1) { /* handle error */ }. Your call to f(n); is not a // Pointer for f it is simply a function call (that makes little sense there). Think through how your set of recursive calls must exit. As it is the computation of (x-3)+(x+5) grows in size so x <= 0 cannot be the exit condition.

    – David C. Rankin
    Nov 28 '18 at 4:33













  • @DavidC.Rankin: The exit condition is x <= 0. It seems you may have missed the fact that f(x-3) calls f and (x+5) does not.

    – Eric Postpischil
    Nov 28 '18 at 4:36













  • Yes, I read it as f (x-3 + x + 5).

    – David C. Rankin
    Nov 28 '18 at 4:37

















Every recursive function has (1) an exit condition, and (2) a recursive call. You provide no exit condition. Further, you need scanf("%d", &n) (note the '&') and you must validate the return, e.g. if (scanf("%d", &n) != 1) { /* handle error */ }. Your call to f(n); is not a // Pointer for f it is simply a function call (that makes little sense there). Think through how your set of recursive calls must exit. As it is the computation of (x-3)+(x+5) grows in size so x <= 0 cannot be the exit condition.

– David C. Rankin
Nov 28 '18 at 4:33







Every recursive function has (1) an exit condition, and (2) a recursive call. You provide no exit condition. Further, you need scanf("%d", &n) (note the '&') and you must validate the return, e.g. if (scanf("%d", &n) != 1) { /* handle error */ }. Your call to f(n); is not a // Pointer for f it is simply a function call (that makes little sense there). Think through how your set of recursive calls must exit. As it is the computation of (x-3)+(x+5) grows in size so x <= 0 cannot be the exit condition.

– David C. Rankin
Nov 28 '18 at 4:33















@DavidC.Rankin: The exit condition is x <= 0. It seems you may have missed the fact that f(x-3) calls f and (x+5) does not.

– Eric Postpischil
Nov 28 '18 at 4:36







@DavidC.Rankin: The exit condition is x <= 0. It seems you may have missed the fact that f(x-3) calls f and (x+5) does not.

– Eric Postpischil
Nov 28 '18 at 4:36















Yes, I read it as f (x-3 + x + 5).

– David C. Rankin
Nov 28 '18 at 4:37





Yes, I read it as f (x-3 + x + 5).

– David C. Rankin
Nov 28 '18 at 4:37












2 Answers
2






active

oldest

votes


















2














as far as I can see first one is scanf



scanf("%d", &n);


second one is your function f is not returning anything, so this



int f(int x) {
//Checks if equal to zero
if (x <= 0)
{
return (x + 3);
}

return ( f(x-3) + (x+5) );
}


minor - the below statement is actually useless



//Pointer for f
f(n);





share|improve this answer
























  • The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

    – David C. Rankin
    Nov 28 '18 at 4:36



















0














As a student of life, I am always willing to help a fellow academic:



Your code has a few errors that should be of note:




  1. int f(int x) has no return statement, even though it is expecting an integer. I assume you wish to return the result of the program (See Issue #3).


  2. You execute f(n) twice. First on line 12, then again on line 16. printf("f(x) is %dn", f(n)); actually executes F(n) in order to receive its return value to associate with the %d format specifier.


  3. You have not assigned x+3 OR f(x-3) + (x+5) to any integer. These statements do not save the return values of f(x) that you need to return.



This link may be of help to you:



https://www.geeksforgeeks.org/c-function-argument-return-values/



Notice specifically how the output of functions are captured.



Hope this helps (And I wish you academic success!)






share|improve this answer
























  • Also, the scanf statement misses a &

    – Canh
    Nov 28 '18 at 4:28











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511943%2frecursive-function-equation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














as far as I can see first one is scanf



scanf("%d", &n);


second one is your function f is not returning anything, so this



int f(int x) {
//Checks if equal to zero
if (x <= 0)
{
return (x + 3);
}

return ( f(x-3) + (x+5) );
}


minor - the below statement is actually useless



//Pointer for f
f(n);





share|improve this answer
























  • The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

    – David C. Rankin
    Nov 28 '18 at 4:36
















2














as far as I can see first one is scanf



scanf("%d", &n);


second one is your function f is not returning anything, so this



int f(int x) {
//Checks if equal to zero
if (x <= 0)
{
return (x + 3);
}

return ( f(x-3) + (x+5) );
}


minor - the below statement is actually useless



//Pointer for f
f(n);





share|improve this answer
























  • The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

    – David C. Rankin
    Nov 28 '18 at 4:36














2












2








2







as far as I can see first one is scanf



scanf("%d", &n);


second one is your function f is not returning anything, so this



int f(int x) {
//Checks if equal to zero
if (x <= 0)
{
return (x + 3);
}

return ( f(x-3) + (x+5) );
}


minor - the below statement is actually useless



//Pointer for f
f(n);





share|improve this answer













as far as I can see first one is scanf



scanf("%d", &n);


second one is your function f is not returning anything, so this



int f(int x) {
//Checks if equal to zero
if (x <= 0)
{
return (x + 3);
}

return ( f(x-3) + (x+5) );
}


minor - the below statement is actually useless



//Pointer for f
f(n);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 4:34









u__u__

2,5771029




2,5771029













  • The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

    – David C. Rankin
    Nov 28 '18 at 4:36



















  • The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

    – David C. Rankin
    Nov 28 '18 at 4:36

















The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

– David C. Rankin
Nov 28 '18 at 4:36





The additional return ( ... ) parenthesis are superfluous, but good call on the function setup.

– David C. Rankin
Nov 28 '18 at 4:36













0














As a student of life, I am always willing to help a fellow academic:



Your code has a few errors that should be of note:




  1. int f(int x) has no return statement, even though it is expecting an integer. I assume you wish to return the result of the program (See Issue #3).


  2. You execute f(n) twice. First on line 12, then again on line 16. printf("f(x) is %dn", f(n)); actually executes F(n) in order to receive its return value to associate with the %d format specifier.


  3. You have not assigned x+3 OR f(x-3) + (x+5) to any integer. These statements do not save the return values of f(x) that you need to return.



This link may be of help to you:



https://www.geeksforgeeks.org/c-function-argument-return-values/



Notice specifically how the output of functions are captured.



Hope this helps (And I wish you academic success!)






share|improve this answer
























  • Also, the scanf statement misses a &

    – Canh
    Nov 28 '18 at 4:28
















0














As a student of life, I am always willing to help a fellow academic:



Your code has a few errors that should be of note:




  1. int f(int x) has no return statement, even though it is expecting an integer. I assume you wish to return the result of the program (See Issue #3).


  2. You execute f(n) twice. First on line 12, then again on line 16. printf("f(x) is %dn", f(n)); actually executes F(n) in order to receive its return value to associate with the %d format specifier.


  3. You have not assigned x+3 OR f(x-3) + (x+5) to any integer. These statements do not save the return values of f(x) that you need to return.



This link may be of help to you:



https://www.geeksforgeeks.org/c-function-argument-return-values/



Notice specifically how the output of functions are captured.



Hope this helps (And I wish you academic success!)






share|improve this answer
























  • Also, the scanf statement misses a &

    – Canh
    Nov 28 '18 at 4:28














0












0








0







As a student of life, I am always willing to help a fellow academic:



Your code has a few errors that should be of note:




  1. int f(int x) has no return statement, even though it is expecting an integer. I assume you wish to return the result of the program (See Issue #3).


  2. You execute f(n) twice. First on line 12, then again on line 16. printf("f(x) is %dn", f(n)); actually executes F(n) in order to receive its return value to associate with the %d format specifier.


  3. You have not assigned x+3 OR f(x-3) + (x+5) to any integer. These statements do not save the return values of f(x) that you need to return.



This link may be of help to you:



https://www.geeksforgeeks.org/c-function-argument-return-values/



Notice specifically how the output of functions are captured.



Hope this helps (And I wish you academic success!)






share|improve this answer













As a student of life, I am always willing to help a fellow academic:



Your code has a few errors that should be of note:




  1. int f(int x) has no return statement, even though it is expecting an integer. I assume you wish to return the result of the program (See Issue #3).


  2. You execute f(n) twice. First on line 12, then again on line 16. printf("f(x) is %dn", f(n)); actually executes F(n) in order to receive its return value to associate with the %d format specifier.


  3. You have not assigned x+3 OR f(x-3) + (x+5) to any integer. These statements do not save the return values of f(x) that you need to return.



This link may be of help to you:



https://www.geeksforgeeks.org/c-function-argument-return-values/



Notice specifically how the output of functions are captured.



Hope this helps (And I wish you academic success!)







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 4:25









armitusarmitus

3159




3159













  • Also, the scanf statement misses a &

    – Canh
    Nov 28 '18 at 4:28



















  • Also, the scanf statement misses a &

    – Canh
    Nov 28 '18 at 4:28

















Also, the scanf statement misses a &

– Canh
Nov 28 '18 at 4:28





Also, the scanf statement misses a &

– Canh
Nov 28 '18 at 4:28


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53511943%2frecursive-function-equation%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks