Code aprox. of pi does not run :(
I had to write a code that approximated pi. The code compiles, but nothing happens when I run it. What should I change.
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 37 38 39 40 41 42 43
|
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
float xloc, yloc, sum, pi, ratio;
int incount = 0, outcount = 0, totalcount =0;
char cAgain;
while (totalcount < 1000000);
{
xloc = -1.0 + (2.0 * (rand() * 1.0 / RAND_MAX));
yloc = -1.0 + (2.0 * (rand() * 1.0 / RAND_MAX));
sum = sqrt(xloc*xloc + yloc*yloc);
if (sum <=1){
incount = incount + 1;
}else
outcount = outcount + 1;
totalcount = incount + outcount;
}
{
ratio = incount / totalcount;
pi = ( incount * 4 ) / 1000000;
cout << "Ratio of in circle to in square is " << ratio << endl;
cout << "Pi was calculated to be" << pi << endl;
cout << "Like to try again? (Y/y/N/n)" <<endl << endl;
cin >> cAgain;
} while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
|
Last edited on
Remove the semicolon from line 13,
while (totalcount < 1000000);
At lines 33 and 34 there is integer division, which won't give the result you are expecting.
Topic archived. No new replies allowed.