hello everyone, i wrote a code in C that suppose to calculate COS of an angle.
i dont need help with the writing, i just have one problem, for some reason the program doesnt enter the function cosApprox, it suppose to enter the function and save the result in sum_column , but it just skips it, does someone knows why?
Thanks.
Code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.1415927
double power(double a, int b);
long factorial(int n);
double cosElement(double x, int i);
double cosApprox(double x, int i);
int main()
{
double x; /* Angle variable */
int k; /* Number of elements variable */
double sum_column; /* Sum of all elements variable */
clrscr();
printf("\nEnter an angle (in Radians):");
scanf("%lf", &x);
printf("\nEnter number of elements:");
scanf("%d", &k);
while(k<0)
{
printf("Erroneous Input. Please enter number of elements (k>=0):");
scanf("%d", &k);
}
sum_column=cosApprox(x, k); /* a function to calculate the sum of elements */
printf("The value of Cos(x) is: %.4lf", 1+sum_column);
getch();
return 0;
}
double cosApprox(double x, int i)
{
double sum=0;
for(i=i;i>0;i--)
{
sum=sum+cosElement(x, i);
}
return sum;
}
double cosElement(double x, int i) /* a function to calculate the element in the i place of the column */
{
double element;
element=pow((-1),i)*power(x,i)/factorial(i);
return element;
}
long factorial(int n) /* a function to calculate the factorial of a number */
{
long sum_f=2*n;
int i;
for(i=2*n;i>1;i--)
{
sum_f=sum_f*(2*n-1);
}
return sum_f;
}
double power(double a, int b) /* a function to calculate x^2k */
{
double power;
power=pow(a,2*b);
return power;
}