这是我的程序。我不明白这里面有什么问题。输入之后什么也没发生。我猜程序在计算循环中卡住了,或者我无法在循环中调用事实函数。问题出在哪里?
#include<iostream>
#include<math.h>
using namespace std;
int fact(int a)
{
int f=0;
for(int i=1; i<=a;i++)
f=f*i;
}
main()
{
double x,temp1,temp2,sine;
int p,n;
temp1=temp2=0;
cout<<"Enter the value of x: ";
cin>>x;
cout<<"nEnter the length of series: ";
cin>>n;
if(n%2==0)
{
for(p=1;p<=n-1;p+4)
{
temp1=temp1+(pow(x,p)/fact(p));
}
for(p=3;p<=n-1;p+4)
{
temp2=(temp2+pow(x,p)/fact(p))*(-1);
}
}
else
{
for(p=1;p<=n;p+4)
{
temp1=temp1+(pow(x,p)/fact(p));
}
for(p=3;p<=n;p+4)
{
temp2=-(temp2+pow(x,p)/fact(p));
}
}
sine=temp1+temp2;
cout<<"nsinx= "<<sine;
}
p+4
在for循环的增量部分做什么?此外,您没有从int fact()
返回任何值。
check this
int fact(int a)
{
int f=1;
for(int i=1; i<=a;i++)
f=f*i;
}