I have to wirte a code for monte carlo pi approximation for a menu driven calculator. here is my code but when i run it it shows me 1 instead of the pi value. can someone tel me what is wrong.

double approximatepi(void)
{
  double x,y,z,pi,randomnumber;
  double r,p,in;
  int tries;
  srand(0);
while (tries!=p)
  {randomnumber=((double)rand())/((double)RAND_MAX)*2*r;
  x=randomnumber;
  y=randomnumber;
  z=r*r;
  pi=(((double)in/(double)p))*4;
 if (z>=x*x+y*y)
{in++;
}

}
return pi;
}

Recommended Answers

All 2 Replies

variables r, p and tries are used but never assigned a value

And your formatting makes the code very difficult to follow. Try a better fomrat and we can be much more helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.