What's wrong with my code

The code should calculate the inverse tangent of the x-radian value entered by the user. It uses the following equation:

The summation from n=0 to N of (((-1)^n)*(x^(2*n+1))/(2*n+1)



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 #include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{

char y = 'y';

do
{
	int n = 0, N = 3000, j = 1, i = 1; double answer = 0, res1 = 1, res2 = 1, res3 = 1, x = 0;
	cout << "Enter a real number in radians to compute the inverse tangent (-1 to 1): "; cin >> x;

	while ((x>1) || (x < -1))
	{
	cout << "Invalid! Enter a valid number please: ";  cin >> x;
	}
	for (n = 0; n <= N; n++)
	{
	for (i = 1; i <= n; i++)
		{
		res1 = res1*(-1);
		}
		for (j = 1; j <= res2; j++)
		{
		res3 = res3*x;
		}
		res2 = (2 * n + 1);
		answer = answer + (res1)*(res3) / (res2);
		}
		cout << "The answer is: " << answer << endl;
		cout << "Would you like to continue? (y/n): "; cin >> y;

} while (y == 'y');
	cout << "Good-bye!" << endl;
	return 0;
}

Help Plz!!!!!
What is the exact problem?
* Does it fail to compile?
* Does it crash, while running?
* Does it produce unexpected results?

Please use more systematic/intuitive indentation.


Note that arctan apparently returns an angle.
See https://www.rapidtables.com/calc/math/Arctan_Calculator.html
Your program asks an angle as input ...

Your equation has mismatching parentheses:
( ((-1)^n) * (x^(2*n+1) ) / (2*n+1)

foo = 2*n+1
( (-1)^n * x^foo / foo



Note that (-1)^n is
* -1, if n is odd
* 1, if n is even

1
2
3
4
5
6
7
8
9
Note that when
f(k) = x^(2*k+1)
and
f(k+1) = x^(2*(k+1) + 1)

f(k+1) = x^(2*k + 2*1 + 1)
       = x^(2*k + 1 + 2)
       = x^(2*k+1) * x^2
       = f(k) * x^2
Last edited on
You don't re-initialise each of res1 and res3 before you try to compute them. Also, res2 would have to be set correctly before use on line 25.

Your code will "work" (in the sense that it will produce a vaguely credible answer) if:
(1) You put res1 = 1; between lines 20 and 21.
(2) You put
1
2
res2 = 2 *n + 1;
res3 = 1;

between lines 24 and 25. (Note that you will be moving the setting of res2 forward.)

As @Keskiverto pointed out, your indentation is all over the place.

There are better and more efficient ways of summing power series like this. Note that the terms are
x
-x3/3 = (previous term) * (-x2) * 1 / 3
x5/5 = (previous term) * (-x2) * 3 / 5
-x7/7 = (previous term) * (-x2) * 5 / 7
Spot a pattern?
You should compute each new term as a simple multiple of the previous one, not try to work out things like x3000 or, worse, (-1)3000, using for-loops.

Incidentally, 3000 is a magic number. This series is, strictly, a sum of an infinite number of terms that you must truncate. Note that, as it has terms of monotonically-decaying magnitude and alternating signs, the truncation error will always be less in absolute magnitude than the last computed term.


Topic archived. No new replies allowed.