用户定义的函数错误(输出错误)[重复]

时间:2022-11-05 11:52:39

This question already has an answer here:

这个问题在这里已有答案:

#include <stdio.h>
int sum_series(float x, int n);

int main()
{
int n;
float x;
printf("Enter the value of x");
scanf("%f", &x);
printf("Enter the value of n");
scanf("%d", &n);
printf("SUM = %f", sum_series(x,n));
return 0;
}


int sum_series(float x, int n )
{
 int i;
 float a = 1,sum = 1.0,b;
 for(i=1;i<n;++i)
   {
      b = ((i+1)*i);
      a = -(a*x*x/b);
      sum = sum + a;
   }
return sum;
}

whatever i am putting the value of x and n, i am always getting the output zero. This is the code for finding the sum of series.My algorithm is right. there is error in user defined function..

无论我把x和n的值放在哪里,我总是得到输出零。这是查找系列之和的代码。我的算法是对的。用户定义的函数有错误..

1 个解决方案

#1


0  

The problem is the return type of the function sum_series. You are saying it is int, but the return value is of type float. Change the type to float and it should work.

问题是函数sum_series的返回类型。你说它是int,但返回值是float类型。将类型更改为float,它应该可以工作。

#1


0  

The problem is the return type of the function sum_series. You are saying it is int, but the return value is of type float. Change the type to float and it should work.

问题是函数sum_series的返回类型。你说它是int,但返回值是float类型。将类型更改为float,它应该可以工作。