#include <conio.h> #include <stdio.h> double fun(int n) { int a,b,c,k; double s; s=0.0; a=2; /***************found***************/ ___1___; for (k=1;k<=n;k++) { s=s+(double)a/b; c=a; a=a+b;b=c; } /***************found***************/ ___2___; } main() { int n; /***************found***************/ printf("\n n="); scanf("%d",0); printf("\n the value of function is: %lf\n", fun(n)); }
时间: 2023-07-05 16:32:25 浏览: 92
下面是完整的代码,并且已经填好了空缺的部分:
```c
#include <conio.h>
#include <stdio.h>
double fun(int n)
{
int a,b,c,k;
double s;
s=0.0;
a=2;
b=1; // 填空1
for (k=1;k<=n;k++)
{
s=s+(double)a/b;
c=a;
a=a+b;
b=c; // 填空2
}
return s; // 修正:添加返回值
}
main()
{
int n;
printf("\n n=");
scanf("%d", &n); // 修正:添加取地址符号 &
printf("\n the value of function is: %lf\n", fun(n));
getch(); // 修正:添加等待输入
}
```
该程序的功能是计算下面这个数列的前n项和:2/1 + 3/2 + 5/3 + 8/5 + 13/8 + ...
其中,每一项的分子为前两项的和,分母为前一项的值。
相关问题
#include <stdlib.h> #include <conio.h>
#include <stdlib.h> 是C语言中的一个头文件,它包含了一些常用的函数和宏定义,用于进行内存分配、类型转换、随机数生成等操作。其中一些常用的函数包括malloc()、free()、atoi()等。
#include <conio.h> 是C语言中的另一个头文件,它主要用于实现控制台输入输出相关的功能。其中一些常用的函数包括getch()、putch()等,可以用于获取键盘输入、输出字符到控制台等操作。
这两个头文件通常在C语言的程序中使用,可以根据需要选择性地包含它们来使用相应的函数和宏定义。
#include <conio.h> 告诉我用法
`<conio.h>` 是 C 语言中的一个头文件,它提供了一组常见的用于控制台 I/O 的函数,例如 `getch()` 和 `clrscr()` 等。
`getch()` 函数用于从控制台读取一个字符,而不需要用户按下回车键。它的用法如下:
```c
#include <conio.h>
int getch(void);
```
`getch()` 函数返回读取到的字符的 ASCII 码值。
`clrscr()` 函数用于清空控制台的屏幕。它的用法如下:
```c
#include <conio.h>
void clrscr(void);
```
该函数没有返回值,它只会清空控制台的屏幕。
需要注意的是,`<conio.h>` 是一个非标准的头文件,它只在一些特定的编译器中可用。在一些操作系统或编译器中,这些函数可能不存在或者有不同的实现。因此,如果你想编写跨平台的程序,最好避免使用 `<conio.h>`。
相关推荐
![application/x-gzip](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)