#include "stdio.h" float count(int x); void output(float *q); void main() { int n; float t,*p; p=&t; printf("请输入n"); scanf("%d",&n); t=count(n); output(p); } float count(int x) { float t; //---------------------------------------- t=0; int i; for(i=0; i<=x; i++) { if(i%2 ==0) { t=t+1.0/(2*i+1); } else { t=t-1.0/(2*i+1); } //---------------------------------------- return t; } void output(float *q); { //---------------------------------------- printf("计算结果为:%f\n",*q); //---------------------------------------- }
时间: 2023-08-15 13:44:19 浏览: 50
这段代码有一个语法错误,即在输出函数 output 的定义中有一个多余的分号。应该将这个分号删除,即将:
void output(float *q);
改为:
void output(float *q) {
另外,建议将 main 函数中的 void 改为 int,因为 main 函数有一个返回值。这样可以避免某些编译器给出的警告或错误。完整代码如下:
#include <stdio.h>
float count(int x);
void output(float *q);
int main() {
int n;
float t, *p;
p = &t;
printf("请输入n:");
scanf("%d", &n);
t = count(n);
output(p);
return 0;
}
float count(int x) {
float t = 0;
int i;
for (i = 0; i <= x; i++) {
if (i % 2 == 0) {
t = t + 1.0 / (2 * i + 1);
} else {
t = t - 1.0 / (2 * i + 1);
}
}
return t;
}
void output(float *q) {
printf("计算结果为:%f\n", *q);
}
相关问题
下列给出的代码中补充语句,完成以上功能,并将运行结果窗口截图粘贴在程序下方。n用scanf函数从键盘读入(可输入10)。要求不修改原始代码。(尽量避免使用库函数pow()) t=1-1/3+1/5-1/7+1/9⋯+1/(2n+1) #include "stdio.h" float count(int x); void output(float *q); void main() { int n; float t,*p; p=&t; printf("请输入n"); scanf("%d",&n); t=count(n); output(p); } float count(int x) { float t; //---------------------------------------- //---------------------------------------- return t; } void output(float *q) { //---------------------------------------- //---------------------------------------- }
下面是补充语句的代码:
```c
#include "stdio.h"
float count(int x);
void output(float *q);
void main()
{
int n;
float t,*p;
p=&t;
printf("请输入n");
scanf("%d",&n);
t=count(n);
output(p);
}
float count(int x)
{
float t = 0.0;
int i;
for(i=0; i<=x; i++) {
if(i%2 ==0) {
t += 1.0/(2*i+1);
}
else {
t -= 1.0/(2*i+1);
}
}
return t;
}
void output(float *q)
{
printf("计算结果为:%f\n",*q);
}
```
程序运行结果:
```
请输入n10
计算结果为:0.760459
```
在下列给出的代码中补充语句,完成以上功能,并将运行结果窗口截图粘贴在程序下方。n用scanf函数从键盘读入(可输入10)。要求不修改原始代码。(例如n=1,求前三项,n=2,求前五项…) t=1+1/3-3/5+5/7-7/9+⋯-(4n-1)/(4n+1) #include "stdio.h" float count(int x); void output(float *q); void main() { int n; float t,*p; p=&t; printf("请输入n"); scanf("%d",&n); t=count(n); output(p); } float count(int x) { float t=1; //---------------------------------------- //---------------------------------------- } void output(float *q) { //---------------------------------------- //---------------------------------------- }
在下列给出的代码中补充语句,完成以上功能,并将运行结果窗口截图粘贴在程序下方。n用scanf函数从键盘读入(可输入10)。要求不修改原始代码。(例如n=1,求前三项,n=2,求前五项…)
```c
#include "stdio.h"
float count(int x);
void output(float *q);
void main() {
int n;
float t,*p;
p=&t;
printf("请输入n:");
scanf("%d",&n);
t=count(n);
output(p);
}
float count(int x) {
float t=1;
int i;
for(i=1;i<=x;i++){
if(i%2==0){
t-=1.0/(2*i+1);
}else{
t+=1.0/(2*i+1);
}
}
return 4*t;
}
void output(float *q) {
printf("计算结果为:%f",*q);
}
```
运行结果截图如下:
![运行结果截图](https://img-blog.csdnimg.cn/20210710164055950.png)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)