斐波那契函数的递归实现

时间:2018-11-08 11:17:44

标签: c recursion compiler-errors

我已经编写了代码来查找Fibonacci编号以及进行调用以查找C语言中的递归版本的次数。我无法删除编译错误。请帮忙。

代码如下:

#include <stdio.h>

int main(int fib) {
  int n,m, count=0; //'count' counts #times function is called
  printf("enter n");
  scanf("%d",&n);
  return fib_rec(n, &count);
}

int fib_rec(int n, int *count)
{
  int b=0,c=1;
  *count = *count +1;
  if(n<=1)
  { 
    return n;
  }
  else
  {
    printf (count);
    return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
  }
}

下面在reptl.it站点上运行时给出了编译错误,如图所示。

main.c: In function 'main':
main.c:7:10: warning: implicit declaration of function 'fib_rec' [- 
Wimplicit-function-declaration]
   return fib_rec(n, &count);
      ^~~~~~~
main.c: In function 'fib_rec':
main.c:20:13: warning: passing argument 1 of 'printf' from incompatible 
pointer type [-Wincompatible-pointer-types]
   printf (count);
         ^~~~~
In file included from main.c:1:
/usr/include/stdio.h:364:43: note: expected 'const char * restrict' but 
argument is of type 'int *'
   extern int printf (const char *__restrict __format, ...);
                ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
main.c:21:25: warning: passing argument 2 of 'fib_rec' makes pointer from 
integer without a cast [-Wint-conversion]
   return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
                     ^~~~~~
main.c:10:25: note: expected 'int *' but argument is of type 'int'
   int fib_rec(int n, int *count)
                ~~~~~^~~~~
main.c:21:47: warning: passing argument 2 of 'fib_rec' makes pointer from 
integer without a cast [-Wint-conversion]
   return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
                                           ^~~~~~
main.c:10:25: note: expected 'int *' but argument is of type 'int'
  int fib_rec(int n, int *count)
                ~~~~~^~~~~
enter n 10
exit status -1
>

2 个答案:

答案 0 :(得分:3)

我将您的代码粘贴到https://tio.run/#c-gcc上,结果如下:

main原型

.code.tio.c:3:7: warning: ‘main’ takes only zero or two arguments [-Wmain]
   int main(int fib) {
       ^~~~

main函数原型是int main(void)int main(int argc, char *argv[])

由于您希望用户输入数字,因此可以选择第一种形式。

count类型

.code.tio.c: In function ‘main’:
.code.tio.c:7:5: error: ‘count’ undeclared (first use in this function)
     count = 0;  // counts the number of times the function is called
     ^~~~~

您必须为count变量指定类型,例如

int count = 0;

fib_rec声明

.code.tio.c:8:12: warning: implicit declaration of function ‘fib_rec’ [-Wimplicit-function-declaration]
     return fib_rec(n, &count);
            ^~~~~~~

在使用该函数之前,您尚未声明该函数。

您可以这样声明:int fib_rec(int n, int *count)例如在main定义之前。

printf的用法

.code.tio.c: In function ‘fib_rec’:
.code.tio.c:21:15: warning: passing argument 1 of ‘printf’ from incompatible pointer type [-Wincompatible-pointer-types]
       printf (count);
               ^~~~~

printf函数要求某些格式。如果要显示整数值,请使用%d

printf("count value is: %d\n", count);

不兼容的指针类型

.code.tio.c:22:27: warning: passing argument 2 of ‘fib_rec’ makes pointer from integer without a cast [-Wint-conversion]
       return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
                           ^~~~~~

这里count已经是整数的指针,*是不必要的:

return fib_rec(n-1, count)+ fib_rec(n-2, count);

显示计算值

您的代码返回计算值,但不显示它。 为此,用

替换return fib_rec(n, &count);
printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
return 0;

因此,更正后的代码可能是:

#include <stdio.h>

int fib_rec(int n, int *count);

int main(void) {
    int n;
    printf("enter n\n");
    scanf("%d",&n);
    int count = 0;  // counts the number of times the function is called
    printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
    return 0;

}

int fib_rec(int n, int *count)
{
    int b=0,c=1;
    *count = *count +1;
    if(n<=1)
    { 
       return n;
    }
    else
    {
      printf ("count: %d\n", *count);
      return fib_rec(n-1, count)+ fib_rec(n-2, count);
    }
}

答案 1 :(得分:1)

main

  1. main的原型为int main(void)int main(int argc, int argc *[])fib不是main的有效输入。

  2. main通常将返回0。因此,fib_rec(n, &count);行应放在退货单之前。

  3. 未声明
  4. 变量count。变量m未使用。

  5. main函数之前,应该存在fib_rec的声明。 int fib_rec(int n, int *count);

在fib_rec

  1. printf的功能不正确。 -应该是%d*count

  2. 递归调用不正确,由于count中的fib_rec是一个指针,因此可以直接将其直接传递给函数而无需获取值。像这样-return (fib_rec(n-1, count)+ fib_rec(n-2, count));

  3. 未使用的变量bc

这解决了编译问题。代码在下面。

int fib_rec(int n, int *count);

int main(void) {

  int n;
  int count;
  printf("enter n"); 
  scanf("%d",&n);
  count = 0;  // counts the number of times the function is called
  fib_rec(n, &count);
  return 0;

}

int fib_rec(int n, int *count)
{
   *count = *count +1;

   if ((n<=1) )
   { 
      return 1;
   }
   else
   {
     printf ("%d ", n);
     return (fib_rec(n-1, count)+ fib_rec(n-2, count));
   }
}