I am having difficulty determining what is wrong with my code. This code is calculated the sin function without actually using the function.
Any help or advice?
Code:
# include <stdio.h>
# include <math.h>
double mysin(double* x, int* n){
int j, i;
int count=0;
int factorial=1;
int addorsub=0;
double sin=0;
for(j=1; j<= *n; j+=1){
if(j==1){
i=1;
}
else{
i=j*2-1;
}
factorial=1;
if(addorsub==0){
for(count=1; count<=i; count+=1){
factorial*=count;
}
sin+=pow(*x,i)/factorial;
addorsub+=1;
}
else{
for(count=1; count<=i; count+=1){
factorial*=count;
}
sin-=pow(*x,i)/factorial;
addorsub-=1;
}
}
return sin;
}
int main(){
int x=0;
double n=0;
double computedsin;
double actualsin;
printf("Please input x and n:\n");
scanf("%d %lf, &x, &n");
computedsin=mysin(&n,&x);
actualsin=sin(n);
printf("mysin=%lf\n",computedsin);
printf("Real Sin=%lf\n", actualsin);
}