Skip to content

Commit 8d01d9b

Browse files
committed
Change return value
monte_carlo should return a double, printing code should be in main
1 parent 0e3e665 commit 8d01d9b

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

contents/monte_carlo_integration/code/c/monte_carlo.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ bool in_circle(double x, double y, double radius) {
88
return x * x + y * y < radius * radius;
99
}
1010

11-
void monte_carlo(int samples) {
11+
double monte_carlo(int samples) {
1212
double radius = 1.0;
1313
int count = 0;
1414

@@ -21,10 +21,7 @@ void monte_carlo(int samples) {
2121
}
2222
}
2323

24-
double estimate = 4.0 * count / (samples * radius * radius);
25-
26-
printf("The estimate of pi is %f\n", estimate);
27-
printf("Percentage error: %0.2f%\n", 100 * fabs(M_PI - estimate) / M_PI);
24+
return 4.0 * count / samples;
2825
}
2926

3027
int main() {

0 commit comments

Comments
 (0)