Discussion about math, puzzles, games and fun. Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ π -¹ ² ³ °

You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

**Amartyanil****Member**- From: Universe
- Registered: 2013-05-27
- Posts: 68

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

**bob bundy****Administrator**- Registered: 2010-06-20
- Posts: 8,371

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

Offline

**Amartyanil****Member**- From: Universe
- Registered: 2013-05-27
- Posts: 68

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

**Amartyanil****Member**- From: Universe
- Registered: 2013-05-27
- Posts: 68

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

Pages: **1**