Why Is My Pi Estimation Code Producing Incorrect Results?

Click For Summary
SUMMARY

The forum discussion addresses issues with a Pi estimation code written in C, specifically in version 1.2 of the Pi Dart Simulator. Key problems identified include incorrect usage of the printf function, where five format specifiers are provided for only four variables, and the incorrect format specifier for the double variable estimatedPi, which should use "%lf" instead of "%d". After correcting these errors, the code successfully estimates Pi with minimal error, demonstrating the importance of proper syntax and type matching in C programming.

PREREQUISITES
  • Understanding of C programming syntax and data types
  • Familiarity with the standard C library functions, particularly printf and srand
  • Knowledge of random number generation techniques in C
  • Basic understanding of mathematical concepts related to Pi estimation
NEXT STEPS
  • Research proper usage of printf in C, focusing on format specifiers for different data types
  • Learn about random number generation in C, including the use of the standard library's rand and srand functions
  • Explore Monte Carlo methods for estimating mathematical constants like Pi
  • Investigate debugging techniques in C to identify and resolve common coding errors
USEFUL FOR

Programmers, particularly those working with C, students learning about random number generation and Monte Carlo simulations, and anyone interested in numerical methods for estimating mathematical constants.

vladittude0583
Messages
40
Reaction score
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
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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
5
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K