[英]Recursive implementation of Fibonacci function
I have written code to find both the Fibonacci number & the number of times the call is made to find the same for the recursive version in C. I am unable to remove compilation errors.我已经编写了代码来查找斐波那契数和调用的次数,以在 C 中找到相同的递归版本。我无法删除编译错误。 Please help.
请帮忙。
The code is given below:代码如下:
#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);
}
}
The compilation errors are given below upon running on reptl.it site, as shown.在 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
>
I pasted your code on https://tio.run/#c-gcc , and here's the result:我将您的代码粘贴到https://tio.run/#c-gcc 上,结果如下:
main
prototype main
原型.code.tio.c:3:7: warning: ‘main’ takes only zero or two arguments [-Wmain]
int main(int fib) {
^~~~
the main
function prototype is int main(void)
or int main(int argc, char *argv[])
main
函数原型是int main(void)
或int main(int argc, char *argv[])
Since you want user to input a number, you can choose the first form.由于您希望用户输入一个数字,因此您可以选择第一种形式。
count
type 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
^~~~~
You must give a type to count
variable, like你必须给一个类型来
count
变量,比如
int count = 0;
fib_rec
declaration fib_rec
声明.code.tio.c:8:12: warning: implicit declaration of function ‘fib_rec’ [-Wimplicit-function-declaration]
return fib_rec(n, &count);
^~~~~~~
You have not declared the function before using it.在使用该函数之前,您尚未声明该函数。
You can declare it this way: int fib_rec(int n, int *count)
for instance before the main
definition.你可以这样声明:
int fib_rec(int n, int *count)
例如在main
定义之前。
printf
usage 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);
^~~~~
The printf
function ask for some formatting. printf
函数要求进行一些格式化。 If you want to display a integer value, use %d
:如果要显示整数值,请使用
%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);
^~~~~~
Here count
is already a pointer on integer, *
is unnecessary:这里
count
已经是一个整数指针, *
是不必要的:
return fib_rec(n-1, count)+ fib_rec(n-2, count);
Your code return the computed value, but doesn't display it.您的代码返回计算值,但不显示它。 To do so, replace
return fib_rec(n, &count);
为此,请替换
return fib_rec(n, &count);
with和
printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
return 0;
So the corrected code could be:所以更正后的代码可能是:
#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);
}
}
in main
在
main
main
has a prototype of int main(void)
or int main(int argc, int argc *[])
. main
有一个int main(void)
或int main(int argc, int argc *[])
的原型。 fib
is not a valid input for main
. fib
不是main
的有效输入。
main
will typically return 0
. main
通常会返回0
。 So the line fib_rec(n, &count);
所以这行
fib_rec(n, &count);
should be put before the return.应该放在返回之前。
variable count
was not declared.变量
count
未声明。 Variable m
is unused.变量
m
未使用。
Before the main
function the declaration for fib_rec
should be there.在
main
函数之前, fib_rec
的声明应该在那里。 int fib_rec(int n, int *count);
In fib_rec在 fib_rec
Function for printf
is not correct. printf
不正确。 - It should be %d
with the *count
- 它应该是带有
*count
%d
The recursive call is not correct, Since count
in fib_rec
is a pointer, it can be directly passed to the function again without taking the value.递归调用是不对的,由于
fib_rec
中的count
是一个指针,可以不取值直接再次传给函数。 Like this - return (fib_rec(n-1, count)+ fib_rec(n-2, count));
像这样 -
return (fib_rec(n-1, count)+ fib_rec(n-2, count));
Unused variable b
and c
未使用的变量
b
和c
This solves the compilation issues.这解决了编译问题。 Code is below.
代码如下。
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.