Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created November 5, 2010 07:20
Show Gist options
  • Save johnhmj/663771 to your computer and use it in GitHub Desktop.
Save johnhmj/663771 to your computer and use it in GitHub Desktop.
#include<iostream>
#include<cstdlib>
#include<cmath>
#define PI acos(-1.0)
#define Rad PI / 180.0
using namespace std;
double hw1104(int n);
int main(int argc, char* argv[])
{
int uN = 0;
cout << "Input N value: ", cin >> uN;
cout << hw1104(uN) << endl;
system("PAUSE");
return 0;
}
double hw1104(int n)
{
int a = 0;
double dRet = 0.0, dRet1 = 0.0, dRet2 = 0.0, x = 0.0;
for (int i = 0; i < n; i ++)
{
a = i + 1, x = (double)a;
if ( (a & 0x1) == 0x1 )
{
dRet1 += (cos(x * Rad) / x);
}
else
{
dRet2 += (sin(x * Rad) / x);
}
}
dRet = (dRet1 - dRet2) / PI;
return dRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment