Why Is My Pi Estimation Code Producing Incorrect Results?

In summary, the conversation is about a code that simulates the estimation of Pi using a dart throwing method. The code is tested and found to have errors, such as incorrect values for the estimation of Pi and percent error. The errors are fixed, and the code is now able to produce accurate results. The conversation also includes some discussions on the functions used in the code and their purpose.
  • #1
vladittude0583
40
0
Here is what I have coded so far:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>

int randomNumber(void); // Function prototype for random number generator
void srand(unsigned int seed); // Function prototype for seeding the RNG

int main()
{
// Declaration of double variables

double x; // coordinate of dart
double y; // coordinate of dart
double v; // coordinate of dart
double estimatedPi;
double Pi;

// Declaration of long variables

long startValue;
long seed;
long impact;
long target;

// Declaration of initialization values

startValue = 100;
seed = time(NULL);
impact = 0;
target = 0;
Pi = 3.14159265358979;

srand(seed);

printf("\t\t\tPi Dart Simulator v1.2 \n\n");
printf("True Pi to 8 decimal places: 3.14159265358979 \n");
printf("Random number seed: 1225850161 \n\n");

printf(" Throws Hits Esimated Pi %%Error \n");
printf("====================================================\n\n");

while (impact < startValue && impact < 10000000)
{
x = (double)rand()/RAND_MAX;
y = (double)rand()/RAND_MAX;
impact += 1;

if ((sqrt(x*x + y*y)) <= 1.00)
{
target += 1;
estimatedPi = ((double)target / (double)impact)*4;
}

while (impact >= startValue)
{
printf("%10d %10d %10d %10d %10d \n", impact, target, estimatedPi,
(((estimatedPi) - Pi) / Pi)*100);
startValue *= 10;
impact = target = 0;
}
}

system("pause");
return 0;
}

unsigned long random = 1;

void srand(unsigned int seed)
{
random = seed;
}

int randomNumber()
{
random = random * 1103515245 + 12345;
return (unsigned int)(random / 65536) % 327768;
}


What I cannot figure out is why my values for the estimation of Pi and percent error is coming out wrong - PLEASE HELP
 
Physics news on Phys.org
  • #2
I tested your code, and there are many errors. I'm very curious whether it could compile on your system. Which compiler did you use? 
from the
Code:
system("pause");
, I guess that you are using Microsoft windows.:smile:, and I'm not sure a it is a legal statement in gcc under linux.

Code:
int randomNumber(void); // Function prototype for random number generator
What does this function use for?

Code:
void srand(unsigned int seed); // Function prototype for seeding the RNG
srand() is a function supplied by standard c library. So, you needn't implement it yourself.

Code:
printf("%10d %10d %10d %10d %10d \n", impact, target, estimatedPi,
(((estimatedPi) - Pi) / Pi)*100);
You have Five "%", but you have only four associated parameters. And there is a mismatch of types. Because " estimatedPi " is declared as a double format variable, you should use "%lf" instead. So as the expression "(((estimatedPi) - Pi) / Pi)*100)".

After modifying the whole bugs, it works.
Here is the result in my system
Code:
Pi Dart Simulator v1.2

True Pi to 8 decimal places: 3.14159265358979
Random number seed: 1225850161

 Throws Hits Esimated Pi %Error
========================================== ==========

       100         78   3.120000  -0.687316
      1000        811   3.244000   3.259727
     10000       7793   3.117512  -0.766519
    100000      78629   3.145191   0.114553
   1000000     785078   3.140312  -0.040764
  10000000    7852331   3.140933  -0.021007
 
  • #3


I would first like to commend you for your efforts in coding and attempting to simulate the estimation of Pi through darts. It is clear that you have put a lot of thought and effort into this project.

Upon reviewing your code, I can see that you have correctly declared and initialized your variables. However, there are a few areas where you may have made some errors.

Firstly, in your randomNumber() function, you are using an unsigned long variable to store the random number. However, in your main() function, you are casting this variable to a double when generating the coordinates for the darts. This may result in loss of precision and affect the accuracy of your estimation.

Additionally, in your while loop, you are incrementing the impact variable before checking if the dart has hit the target. This may result in a higher number of impacts than actual hits, leading to an inaccurate estimation of Pi.

Furthermore, in your printf statement, you are using the incorrect variable for the estimated Pi value. You are using "estimatedPi" instead of "estimatedPi" which may also lead to incorrect values being displayed.

To improve the accuracy of your simulation, I would suggest reviewing your code and making the necessary corrections to ensure the correct data types are used and variables are incremented and displayed accurately.

Lastly, I would also recommend including comments in your code to make it easier for others to understand and debug any errors.

I hope this helps and good luck with your project!
 

FAQ: Why Is My Pi Estimation Code Producing Incorrect Results?

What is C+ programming?

C+ programming is a high-level programming language that is used to develop a wide range of applications, from operating systems to video games. It is an extension of the C programming language, with additional features such as object-oriented programming.

Why do I need help with my C+ programming homework?

C+ programming can be complex and challenging, especially for those who are new to programming. Getting help with your homework can save you time and frustration, as experienced programmers can help you understand difficult concepts and guide you through the problem-solving process.

Where can I find reliable C+ programming homework help?

There are many online resources and tutoring services that offer C+ programming homework help. It is important to do your research and choose a reputable and experienced provider. You can also ask for recommendations from your classmates or professors.

How can C+ programming homework help improve my grades?

By receiving help with your C+ programming homework, you can gain a better understanding of the subject and improve your skills. This can lead to better grades on assignments and exams, as well as a stronger overall understanding of programming concepts.

Is getting help with my C+ programming homework cheating?

No, getting help with your programming homework is not considered cheating. It is important to remember that programming is a collaborative field and seeking assistance is a common practice. However, it is important to always cite your sources and not copy someone else's work without proper attribution.

Back
Top