I have to lines listed.. a cin >> x and a scanf("%f",&x). If I use cin program works perfectly.. change it to scanf and values arent' even corect any longer.. different decimal values all together.. anyone???
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
double ApproxCos(double,double);
int main()
{
double error = 10e-6; //varible to change error value
double x;
printf("Input x to approximate cos: ");
scanf("%f",&x); // this DOES NOT WORK
//cin >> x; // this line works
printf ("cos = %f\n", cos(x));
return 0;
}
double ApproxCos(double x, double error)
{
double sum=0;
double factorial = 1;
for (int n=0; abs(cos(x) - sum) > error; ++n)
{
if (n > 0) // only change value of factorial after first loop
factorial *= (2*n) * (2*n-1); //recursively calculate factorial
sum += pow(-1.0,n) * pow(x,n*2) / factorial; // summation of equation
}
return sum;
}