#include <stdio.h> void fun() { int a[3][3], sum; int i, j; /********found********/ ________; for (i=0; i<3; i++) for (j=0; j<3; j++) /********found********/ scanf("%d", a[i][j]); for (i=0; i<3; i++) sum = sum+a[i][i]; printf("%d\n", sum); } int main() { fun(); }
时间: 2023-07-23 12:38:47 浏览: 58
这段代码的功能是输入一个 $3 \times 3$ 的矩阵,计算其主对角线上的元素之和,并输出该值。但是代码有一处错误:
1. 第一处错误是在第 6 行,变量 `sum` 的初始值应该为 0。
正确的代码应该是:
```c
#include <stdio.h>
void fun() {
int a[3][3], sum = 0;
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 3; i++) {
sum = sum + a[i][i];
}
printf("%d\n", sum);
}
int main() {
fun();
return 0;
}
```
相关问题
题目:通过函数求级数a=2/1+ 3/2+5/3+8/5+13/8+21/13.的 前n项之和,n通过函数参数传递。在主函数中输入n,调用 函数输出结果。 如输入4,结果为:6.77 程序开头为#include <stdio.h> #include <stdlib.h> void PutRes(void); float fun(int);float fun(int n);
好的,我已经明白了你的要求。以下是你所需的代码:
```c
#include <stdio.h>
#include <stdlib.h>
float fun(int n)
{
float sum = 0, a = 2, b = 1, c;
int i;
for(i = 1; i <= n; i++)
{
sum += a / b;
c = a + b;
b = a;
a = c;
}
return sum;
}
void PutRes(void)
{
int n;
float result;
printf("请输入n的值:");
scanf("%d", &n);
result = fun(n);
printf("前%d项之和为:%.2f\n", n, result);
}
int main()
{
PutRes();
return 0;
}
```
在这个程序中,我们先定义了一个 `fun` 函数来计算级数的前 n 项之和。在 `fun` 函数中,我们用 `sum` 来存储前 n 项之和的值,用 `a` 来存储每一项的分子,用 `b` 来存储每一项的分母。在 `for` 循环中,我们先将当前项的值加入 `sum` 中,然后计算下一项的分子和分母,并更新 `a` 和 `b` 的值。
在主函数中,我们调用 `PutRes` 函数来获取用户输入的 n 值,并计算前 n 项之和。计算完成后,我们输出结果并结束程序。
希望这个程序能够满足你的需求。如果还有其他问题,欢迎随时向我提出。
#include<stdio.h> #include<math.h> double fun(double x){ double f,t; int n; /************found*********/ f=1.0+___1___; t=x; n=1; do { n++; /************found**********/ t*=x/___2___; /************found**********/ f+=___3___; } while(fabs(t)>=1e-6); return f; } void main(){ double x,y; scanf("%lf",&x); y=fun(x); printf("%-12.6f",y); }
以下是填空后的代码:
```c
#include<stdio.h>
#include<math.h>
double fun(double x){
double f,t;
int n;
f=1.0+x; // 填空1
t=x;
n=1;
do {
n++;
t*=x/n; // 填空2
f+=pow(-1,n+1)*t; // 填空3
} while(fabs(t)>=1e-6);
return f;
}
void main(){
double x,y;
scanf("%lf",&x);
y=fun(x);
printf("%-12.6f",y);
}
```
这个程序实现了一个函数 $f(x)=\sum\limits_{n=1}^{\infty} \dfrac{(-1)^{n+1}x^{n}}{n}$ 的计算。该函数的特点是在区间 $(-1,1]$ 内收敛,且 $x=1$ 时取到最大值 $\ln{2}$。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)