Hello everyone, I am a noob at programming and am having trouble finishing an assignment. The problem is to write a function that calculates the sine of a number using a Taylor series expansion up to the 10th term. This is the code I have written so far:
Code:
#include <stdio.h>
int main(int argc, char **argv)
{
float x, y;
long exp(int a, char b)
{
int i;
long result=1;
for (i=0; i<b; i++)
{
result=result*a;
}
return result;
}
int factorial(int a)
{
int i, result=1;
for (i=1; i<=a; i++)
{
result=result*i;
}
return result;
}
float sin(float a)
{
float result=0;
int i;
char n=1;
for (i=1; i<20; i=i+2)
{
if (n%2==1)
{
result=result + exp(a,i)/factorial(i);
}
else
{
result=result - exp(a,i)/factorial(i);
}
n++;
}
return result;
}
x=0.524;
y=sin(x);
printf("sine is %f", y);
}
We were given specific decimal numbers which correspond to radians (0.524=pi/6) to test the program. In its current state I get 0.000000 regardless of my input for x.
Any help is appreciated.