May 14, 2009 at 12:24am UTC
this is my final version of C++ code (it is working) I need another version, recursion (recursive) version. I tried to do it using this code, but compiler says I have EOF error and recursiveCos couldn't compile. can someone help?
#include<iostream>
#include<cstring>
#include<cstdlib>
#include <cmath>
#include <iomanip>
#include <exception>
using namespace std;
#define _USE_MATH_DEFINES
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
double recursiveCos(double angleNumber, double pieAngle);
int main (void)
{
double tmp = 0.0;
double radianAngle = 0.0;
char again = 'Y';
do
{
cout << "Input an angle in radians: ";
cin >> tmp;
while(!cin)
{
cout << "Input an angle in radians: ";
cin.clear();
cin.ignore(255,'\n');
cin >> tmp;
}
cin.ignore(255,'\n');
try
{
radianAngle = fmod( tmp, 2 * M_PI );
cout << fixed << showpoint << setprecision(3) << tmp << "as a first period angle is " << setprecision(3) << radianAngle << " radians" << endl;
}
catch (exception e)
{
cout << "Exception:" << e.what() << endl;
cout <<"Press the \"Enter\" key to continue";
cin.get();
return EXIT_FAILURE;
}
cout << endl;
double result = cos(tmp);
cout << "cos<" << tmp << setprecision(3) << ">" << " = " << result << endl;
cout << "cosine<" << radianAngle << setprecision(3) << ">" << " = " << result << endl;
cout << "Calculate another cosine? (Y/N): Y\b";
if((again = toupper (cin.get()))== '\n')
again = 'Y';
else
cin.ignore(256,'\n'); // read past CR
}
while(again == 'Y' || again == 'y');
cin.get(); // hold window open
return EXIT_SUCCESS;
}
Last edited on May 14, 2009 at 4:02am UTC
May 14, 2009 at 12:43am UTC
Doesn't that make it 3 decimal points?
May 14, 2009 at 12:49am UTC
Found the problem:
x - (x /(2.0*3.14)) * (2.0*3.14)
This will always equal 0. What equation is this supposed to be?
Simply simplify the expression and it actually comes out to 0 or x - x.
Last edited on May 14, 2009 at 12:55am UTC
May 14, 2009 at 2:24am UTC
this is my radian equation.
if you input 24 radian and you get 5.15 radian.
so cos(24)= .424
cos(5.15) = .424
Last edited on May 14, 2009 at 4:01am UTC
May 14, 2009 at 2:56am UTC
As computerquip pointed out, your formula is useless. Calculating the first period angle is a modulus operation. Use fmod, like so:
radianAngle = fmod( tmp, 2 * M_PI );