Homework-C Programming- Recursive Program
Hey guys! I was assigned this program which was pretty simple and didn't take long to code, yet I can't get it to run. Nothing prints and I think it's because it goes into an endless loop. Just looking for a fix on this.
Assignment:
Write and test a recursive function that returns the value of the following recursive definition:
f(x) = 0 if x <= 0 f(x- 1) + 2 otherwise
My program:
#include <stdio.h>
int main(void)
{
int n, x;
int factorial(int n) {
if (x <= 0) {
printf("x equals: ");
return 1;
} else {
return n * factorial(n - 1); //error here
}
f(x) = f(x - 1) + 2;
开发者_StackOverflow中文版 }
return 0;
}
Am i seeing this incorrectly? Why is there a
f(x)=f(x-1)+2;
in your int factorial function?
That code should not compile as is. You can't define one function inside another in C, so you'll need to create another function outside main()
, and call that.
I suggest you remove the factorial() function entirely, since it does not appear to be relevant to this assignment.
The basic structure of your program should be:
#include <stdio.h>
int f(int x)
{
//definition of recursive function
}
int main(void)
{
//call to recursive function
return 0;
}
The assignment gives you the definition of the recursive function; you just have to translate it into C.
You have define factorial function within main which is not possible.Separate its definition from main() and call it from main().
Hope this works.First correct this then only something can be done.
You've declared factorial inside main.
You're not calling factorial in main.
You want something like this:
int factorial(int n) {
//calculate the factorial
return result;
}
int main() {
int result = factorial(10); // Calculate 10!
printf("10! is %d", result);
}
P.S. Thanks for being honest about it being homework!
You are asked to implement f(x)
defined as:
f(x) = 0 if x <= 0
f(x-1) + 2 otherwise
So, first of all, forget about the factorial, I'm guessing you grabbed it as the example or a recursive function, but this isn't what you're asked to do here.
You need to implement the function f
, and an implementation would look like this:
int f( int x ) {
if( x <= 0 ){
return /*something*/;
}else{
return /*something else*/;
}
}
From reading the definition of f(x)
that you're given, you can figure out what /*something*/
and /*something else*/
should be.
Then, you're asked to "test" your implementation. You do that by seeing what values f
returns from your main
function which would look like:
int main(void){
printf("f(1) is %d\n", f(1));
printf("f(13) is %d\n", f(13));
/* .. more tests here if you want .. */
return 0;
}
- You have defined the
factorial ()
function inside main function. This is not allowed. You need to put the entire function out of the main. - You have done
f(x) = f(x - 1) + 2;
. Here you have a function on the left side of the assignment which is incorrect. Also i could not understant what was the cause of such an attempt.
The code which you require to computer the recursive function is:
#include <stdio.h>
int main (void)
{
int x, y;
printf ("\nEnter x: ");
scanf ("%d", &x);
y = f (x);
printf ("\n%d\n", y);
return 0;
}
int f (int x)
{
if (x <= 0)
{
return 0;
}
else
{
return f (x - 1) + 2;
}
}
I could not understand why did the factorial function came into your way. May be probably you wanted to modify it and implement the given problem.
#include<stdio.h>
int fun(float i){
int p;
if(i<=0){
return 0;
}
else{
i-=1;
p=fun(i)+2;
}
return p;
}
void main(){
float i;
printf("Enter the number: ");
scanf("%f",&i);
printf("\nThe output is %d",fun(i));
}
Check this out.
I think this is what u need. #include int f(int x);
int main(void){
int result = f(x);
printf("10! is %d", result);
return 0;
}
int f(int x) {
if (x <= 0) return 0;
return x*f(x-1)+2; // No more error here. This is where recursion begins
}
By the way, that's not a factorial function.
精彩评论