Ask your own question, for FREE!
Computer Science 24 Online
OpenStudy (anonymous):

This C++ pi calculator program I made crashes when the amount of iterations are set to more than about 260000. Try it for yourself and you'll know what I'm talking about. Anybody know how I can fix this? I've played about with different variable types etc, but nothing seems to work. Here is the code: http://tinypaste.com/7644a5eb Thanks :)

OpenStudy (anonymous):

I got to 750000 with a simple change. It has to do with variable types, indeed. :-) Anyway, try to write your code more organized/cleaner, like indentation, positioning of the braces and so on. :-)

OpenStudy (anonymous):

Really? You aren't going to just tell me? It's only for a personal project, nothing related to school work. I'll have a go though, but just to make sure, the program you're using to run it won't make a difference will it? I'm using CodeBlocks.

OpenStudy (anonymous):

I am using g++ under Linux Ubuntu. Have you tried long double? Or unsigned long long for the integer parts? At least here, it worked great.

OpenStudy (anonymous):

http://tinypaste.com/8b97f30c Updated version with alternative variable types. Still not getting more than 260000 without a crash.

OpenStudy (anonymous):

That's strange: bmp@HAL9000:~$ ./a.out Enter number of iterations: 260000 3.141598695073463329719559711250553846184629946947098 bmp@HAL9000:~$ ./a.out Enter number of iterations: 750000 3.141594747980008819291525279382426560914609581232071 bmp@HAL9000:~$ ./a.out Enter number of iterations: 100000 3.141608361278176366144024300730563936667749658226967 bmp@HAL9000:~$ ./a.out Enter number of iterations: 1000000 3.141594224383371217450972023854660619690548628568649 Have you tried commenting out the cout at the loop and couting at the end of program only?

OpenStudy (anonymous):

Yep, even with no printing involved whatsoever I still get the same crash. Maybe it is something to do with the compiler..

OpenStudy (anonymous):

I even copied the whole program folder to my SSD and ran it from there, no improvements.

OpenStudy (anonymous):

There are a few other tries, like trying to avoid using arrays (it's an approximation of Pi through Series expansion, right? I think it's feasible) and maybe thinking about a better way to code the loops. :-)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!