You are not logged in.
Pages: 1
Hi;
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?
Last edited by Amartyanil (2018-06-09 18:15:01)
"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon
Offline
hi Amartyanil
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
Children are not defined by school ...........The Fonz
You cannot teach a man anything; you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you! …………….Bob
Offline
hi bob bundy;
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?
"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon
Offline
In line 12, you are incrementing sone instead of stwo. Is this intentional?
'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.
Offline
hi Benice;
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"?
Last edited by Amartyanil (2018-07-17 15:52:09)
"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon
Offline
I suggest you test if the problem is a pure IO one.
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
X'(y-Xβ)=0
Offline
If both of them succeeded, the mistake is located in main
I do not get your point, can you please explain it?
"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon
Offline
hi;
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}
Last edited by Amartyanil (2018-07-29 14:47:36)
"Every place is the center of the universe. And every moment is the most important moment. And everything is the meaning of life." ~ Dan Harmon
Offline
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.
X'(y-Xβ)=0
Offline
Pages: 1