I am constantly getting an error message that says -> (error: implicit conversion from '_Complex int' to 'int' is not permitted in C++)
i don't know how to fix this implicit conversion problem and i would appreciate either a link or any tips as to what i can do with my code. Or, if my function is a mess, letting me know that it wont work for this exercise.
Here is the assignment:
(Estimate π) π can be computed using the following summation:
Write a function that returns m(i) for a given i and write a test program that displays the following table:
i m(i)
1 4.0000
101 3.1515
201 3.1466
301 3.1449
401 3.1441
501 3.1436
601 3.1433
701 3.1430
801 3.1428
901 3.1427
Well, your immediate compilation issue would be solved by writing 2*i rather than 2i.
However, that doesn't sum your series. You need to initialise result (to 0) and, on each loop, add the appropriate term (which you are some way from at present).
the problem i have now is that i get the following as an output and the homework system gives me an error message the says 'Your standard output is not what was expected.' ... Here is my output:
i m(i)
1 4
101 3.15149
201 3.14657
301 3.14491
401 3.14409
501 3.14359
601 3.14326
701 3.14302
801 3.14284
901 3.1427
I do not know how to round to four decimal places or why the last one rounded to four decimal places by itself.
Any advice?
To get fixed-point output with 4 decimal places put cout << fixed << setprecision( 4 );
in int main(), before any of your output.
You will need the header #include <iomanip>
for setprecision.
BTW:
(1) Your inner (j) loop is completely pointless - you simply create the same term multiple times. Just use the i loop.
(2) It is unnecessary to use pow(-1,n) to get alternating signs in your series. Just use a single variable that you invert the sign of on each pass through the loop. No derivation of that series would actually have used (-1)^n - it is just a mathematically convenient way of writing "alternating signs". Say:
sign = 1;
for(int i = 1; i <= num; i++){
result += sign * ....
sign = -sign; // switches sign