George,Y wrote:If both of them succeeded, the mistake is located in main
I do not get your point, can you please explain it?
The program now needs an input "accuracy" to start running.
I suggest to cancel this input first and set an accuracy inside the code.
Compile and test it.
]]>Will using this formula in the code have a lesser complexity than the code originally posted?
\pi = 4 \sum_{k \geq 0} (-1)^k {1\over 2k + 1}
]]>If both of them succeeded, the mistake is located in main
I do not get your point, can you please explain it?
]]>Just change the code to compulsively calculate the function on a certain accuracy you put in the code.
And compile it to see if it runs properly.
Then alter the code with another value of accuracy, and test again.
If both of them succeeded, the mistake is located in main
]]>Thanks for pointing out that the sone and stwo's would have been floats.
hi Agnishom;
That was not intentional. Thanks for pointing out.
However, can anyone please explain why the code was not working properly when I entered the datatype of sone and stwo as "int"?
]]>pitwo = pitwo - 1/double(stwo);
]]>For "not working properly", I mean that the program is getting frozen and not printing anything at all.
Can you please point out my code for bugs?
What exactly do you mean by "it's not working properly" ? This series converges only very slowly. I needed over 1200 terms before I got 3.14....
Bob
]]>I have written a code in C++ for calculating the approximate value of
based on the formula:#include <iostream>
double CalculatePi(int acc)
{
int sone;
int stwo;
double pione = 0;
double pitwo = 0;
for (sone = 1; sone < acc; sone += 4) {
pione = pione + 1/sone;
}
for (stwo = 3; stwo < acc; sone += 4) {
pitwo = pitwo - 1/stwo;
}
double potentialpi;
potentialpi = pione + pitwo;
std::cout << potentialpi * 4 << std::endl;
return 0;
}
int main()
{
std::cout << "Enter accuracy: ";
int accuracy;
std::cin >> accuracy;
CalculatePi(accuracy);
return 0;
}
However, this code is not working properly and is not returning anything when I am inputting "acc"(for accuracy) more than 2.
Can anyone please help me out here?