C语言 迭代法求double类型数组的总和
答案:1 悬赏:80 手机版
解决时间 2021-02-15 01:58
- 提问者网友:伴风望海
- 2021-02-14 20:20
C语言 迭代法求double类型数组的总和
最佳答案
- 五星知识达人网友:轻雾山林
- 2021-02-14 21:26
#include
int main()
{
double data[] = {12.5, 67.9, 12.0, 9.8, 5};
double sum = 0;
int i = 0;
for (i = 0; i < sizeof(data)/sizeof(double); i++)
{
sum += data[i];
}
printf("sum:%f
", sum);
return 0;
}追问那递归法呢?要怎么写?追答#include
#define SIZE 5
double total(double data[], int n)
{
double result = 0;
int i = 0;
if (n < SIZE)
result =data[n] + total(data,n+1);
return result;
}
int main()
{
double data[SIZE] = {1.0,2.1,3.1,4.1,5.1};
double sum = 0;
sum = total(data, 0);
printf("sum:%f
", sum);
return 0;
}
int main()
{
double data[] = {12.5, 67.9, 12.0, 9.8, 5};
double sum = 0;
int i = 0;
for (i = 0; i < sizeof(data)/sizeof(double); i++)
{
sum += data[i];
}
printf("sum:%f
", sum);
return 0;
}追问那递归法呢?要怎么写?追答#include
#define SIZE 5
double total(double data[], int n)
{
double result = 0;
int i = 0;
if (n < SIZE)
result =data[n] + total(data,n+1);
return result;
}
int main()
{
double data[SIZE] = {1.0,2.1,3.1,4.1,5.1};
double sum = 0;
sum = total(data, 0);
printf("sum:%f
", sum);
return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯