简体   繁体   English

作业-C编程-递归程序

[英]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:编写并测试返回以下递归定义的值的递归 function:

 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;
    }
    return 0;
}

Am i seeing this incorrectly?我看错了吗? Why is there a为什么会有一个

f(x)=f(x-1)+2; f(x)=f(x-1)+2;

in your int factorial function?在你的 int 阶乘 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.您不能在 C 中的另一个内部定义一个 function,因此您需要在main()之外创建另一个 function 并调用它。

I suggest you remove the factorial() function entirely, since it does not appear to be relevant to this assignment.我建议您完全删除 factorial() function,因为它似乎与此分配无关。

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;赋值给你递归 function 的定义; you just have to translate it into C.您只需将其翻译成 C。

You have define factorial function within main which is not possible.Separate its definition from main() and call it from main().您已经在 main 中定义了阶乘 function,这是不可能的。将其定义与 main() 分开并从 main()中调用它。

Hope this works.First correct this then only something can be done.希望这可行。首先纠正这个然后只能做一些事情。

  1. You've declared factorial inside main.您已经在 main 中声明了阶乘。

  2. You're not calling factorial in main.您没有在 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);
}

PS Thanks for being honest about it being homework! PS感谢您诚实地说这是家庭作业!

#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.看一下这个。

You are asked to implement f(x) defined as:您被要求实施定义为的f(x)

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.所以,首先,忘记阶乘,我猜你把它当作例子或者递归 function,但这不是你在这里被要求做的。

You need to implement the function f , and an implementation would look like this:您需要实现 function f ,实现如下所示:

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.通过阅读您给出的f(x)的定义,您可以弄清楚/*something*//*something else*/应该是什么。

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:您可以通过查看fmain function 返回的值来做到这一点,如下所示:

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.您已经主 function 中定义了factorial () function。 This is not allowed.这是不允许的。 You need to put the entire function out of the main.您需要将整个 function 放在 main 之外。
  • You have done f(x) = f(x - 1) + 2;你已经完成了f(x) = f(x - 1) + 2; . . Here you have a function on the left side of the assignment which is incorrect.在这里,分配左侧有一个 function,这是不正确的。 Also i could not understant what was the cause of such an attempt.我也无法理解这种尝试的原因是什么。

The code which you require to computer the recursive function is:计算递归 function 所需的代码是:

#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.我不明白为什么阶乘 function 会碰到你。 May be probably you wanted to modify it and implement the given problem.可能您想修改它并实现给定的问题。

I think this is what u need.我认为这就是你所需要的。 #include int f(int x); #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.顺便说一句,这不是阶乘 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM