#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
#define LIMIT 100000000 //max value of n void main(){ double mypi,x,y ; int minus ; // 1 for pos., 2 for neg.
long int m,n;
time_t t;
n = 0; m = 0;
srand((unsigned) time(&t));
clrscr();
do{
n++; x = (rand()/(double)RAND_MAX); //pick a random X between 0..1
minus = 1+(rand()/(double)RAND_MAX);//pick a sign for X
switch (minus) {
case1 : x = x; case2 : x = -1*x; }y = rand()/(double)RAND_MAX;//pick a random Y between 0..1
minus = 1+rand()/(double)RAND_MAX;//pick a sign for Y
switch (minus) {
case1 : x = x; case2 : x = -1*x; }if (pow(x,2)+pow(y,2) < 1) m++; //the point is inside the unit circle mypi = 4.0*m/n; //calculate pi if (n % (LIMIT / 1000) == 0) {
gotoxy(1,1);
printf("X:%f ",x);
printf("Y:%f ",y);
printf("n:%ld ",n);
printf("m:%ld ",m);
printf(" - ");
printf("PI:%f ",mypi);
}
}
while (n != LIMIT); getch();

}

Dani AI

Generated

This thread is implementing Monte Carlo pi estimation (throw random points in the square [-1,1]x[-1,1], count how many fall inside the unit circle, then pi ≈ 4(inside/total)). The original post contains a mix of portability and logic errors that typically cause compile failures or wrong results: nonstandard DOS-era calls (clrscr, gotoxy, getch), malformed case labels, incorrect sign-selection logic, using pow() where `xx` is cheaper and clearer, and updating the wrong variable when assigning the Y sign. As pointed out, compiler error text and the compiler/IDE name are very helpful for diagnosis.

A short, portable example that fixes those issues and prints progress without nonstandard headers:

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

int main(void) {
    const unsigned long N_SAMPLES = 100000000UL;
    unsigned long inside = 0;
    srand((unsigned)time(NULL));

    unsigned long report = (N_SAMPLES >= 1000) ? (N_SAMPLES / 1000) : 1;
    for (unsigned long i = 1; i <= N_SAMPLES; ++i) {
        double x = ((double)rand() / (double)RAND_MAX) * 2.0 - 1.0;
        double y = ((double)rand() / (double)RAND_MAX) * 2.0 - 1.0;
        if (x*x + y*y <= 1.0) ++inside;
        if ((i % report) == 0) {
            double pi_est = 4.0 * (double)inside / (double)i;
            printf("\r%6.2f%%  samples=%lu  pi≈%.9f", (i/(double)N_SAMPLES)*100.0, i, pi_est);
            fflush(stdout);
        }
    }
    printf("\nFinal estimate: pi ≈ %.12f\n", 4.0 * (double)inside / (double)N_SAMPLES);
    return 0;
}

Key notes and troubleshooting tips:

  • Replace void main() with int main(void) and return 0 for portability.
  • To get +/- sign use arithmetic that produces [-1,1] directly; avoid assigning a double expression to an int and then using case1 (should be case 1:) — malformed labels cause compile errors.
  • Prefer x*x + y*y over pow(x,2) for speed and clarity.
  • conio.h functions are nonstandard; use printf with \r and fflush(stdout) for simple terminal progress.
  • Monte Carlo convergence is slow: error scales like 1/sqrt(N). ~2.7e6 samples give ~0.001 typical error; achieving many correct digits needs enormous N.
  • For repeatable debugging use a fixed seed (e.g., srand(12345)); for real runs seed from time.

As posted the original attempt, the above fixes address the common syntax and logic faults while making the program portable and easier to debug.

Recommended Answers

All 2 Replies

Is it showing a compiler error? post the errors
Is it showing unwanted output? post the output
Do you know how to properly format your code and wrap it it in code tags before you post? If yes then do so

Is it showing a compiler error? post the errors
Is it showing unwanted output? post the output
Do you know how to properly format your code and wrap it it in code tags before you post? If yes then do so

thank you very much for your reminding, I really have not wrap all of code before post. It disappear about 1 or 2 lines. Now I can solve it .
^^

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.