Computing pi in a function and returning it to main function

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:

m(i)=4(1−(1/3)+(1/5)−(1/7)+(1/9)−(1/11)+…+(−1)^(i+1)/2i−1)

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


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
#include <iostream>
#include <cmath>
using namespace std;

double m(double num)
{
  double result = 4.0000;

  for(double i = 1.0; i <= num; i++){
    
    result = 4.0 * (1.0(/*idk what to put*/)/2.0i-1.0);

  }

  return result;
}

int main(){

    cout << "i         m(i)" << endl;
    cout << "1             " << m(1) << endl;
    cout << "101             " << m(101) << endl;
    cout << "201             " << m(201) << endl;
    cout << "301             " << m(301) << endl;
    cout << "401             " << m(401) << endl;
    cout << "501             " << m(501) << endl;
    cout << "601             " << m(601) << endl;
    cout << "701             " << m(701) << endl;
    cout << "801             " << m(801) << endl;
    cout << "901             " << m(901) << endl;

}
Last edited on
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).
Thank you for pointing out the error in my code. Sometimes i have tunnel vision and fail to see the mistakes in my code.

So, now i reworked the function like so:
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
#include <iostream>
#include <cmath>
using namespace std;

double m(int num)
{
  double temp = 0;
  double result = 0;

  for(int i = 1; i <= num; i++){
    
    for(int j = 0; j < i; j++){
      temp = pow(-1,(i+1)) / (2*i-1);
    }
    result += temp;

  }
  result = 4 * result;
  return result;
}

int main(){

    cout << "i               m(i)" << endl;
    cout << "1               " << m(1) << endl;
    cout << "101             " << m(101) << endl;
    cout << "201             " << m(201) << endl;
    cout << "301             " << m(301) << endl;
    cout << "401             " << m(401) << endl;
    cout << "501             " << m(501) << endl;
    cout << "601             " << m(601) << endl;
    cout << "701             " << m(701) << endl;
    cout << "801             " << m(801) << endl;
    cout << "901             " << m(901) << endl;    

}


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?
Last edited on
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


Last edited on
I ended up using this code and it was accepted, which i laughed at because i literally just pasted the zero's onto the 4 :
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
#include <iostream>
#include <cmath>
using namespace std;

double m(int num)
{
  double temp = 0;
  double result = 0;

  for(int i = 1; i <= num; i++){
    
    for(int j = 0; j < i; j++){
      temp = pow(-1,(i+1)) / (2*i-1);
    }
    result += temp;

  }
  result = 4.0 * result;
  return result;
}

int main(){

    cout << "i               m(i)" << endl;
    cout << "1               " << m(1) << ".0000" << endl;
    cout << "101             " << ceil(m(101)*10000)/ 10000 << endl;
    cout << "201             " << ceil(m(201)*10000)/ 10000 << endl;
    cout << "301             " << floor(m(301)*10000)/ 10000 << endl;
    cout << "401             " << ceil(m(401)*10000)/ 10000 << endl;
    cout << "501             " << ceil(m(501)*10000)/ 10000 << endl;
    cout << "601             " << ceil(m(601)*10000)/ 10000 << endl;
    cout << "701             " << ceil(m(701)*10000)/ 10000 << endl;
    cout << "801             " << ceil(m(801)*10000)/ 10000 << endl;
    cout << "901             " << m(901) << endl;    

}

thanks for the help lastchance
I'm afraid this is one reason why I dislike these online systems for homework.

Another is the widespread occurrence of "cancellation of errors". Two wrongs make ... a right!

It encourages bad coding and, where error cancellation takes place, continued use of flawed routines without realising there is a problem.
Topic archived. No new replies allowed.