Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <math.h>
- #define FRANDOM ((double) random()/RAND_MAX)
- #define N 1e4 // não poderia inicializar vetor
- #define M 10000
- int main(void)
- {
- double x, y, nacertos, pi_soma = 0.0, pi2_soma = 0.0;
- for (double n = 0; n<M; n++) {
- nacertos = 0.0;
- for (int i = 0; i < N; i++)
- {
- x = FRANDOM;
- y = FRANDOM;
- double f = x*x + y*y;
- if (f<1)
- nacertos++;
- }
- // printf("%lf\n", nacertos/N*4);
- pi_soma += nacertos/N*4;
- pi2_soma += (nacertos/N*4)*(nacertos/N*4);
- }
- double pi_medio = pi_soma/M, pi2_medio = pi2_soma/M;
- printf("Pi Médio: %lf\nErro calculado: %e\nErro real: %e\n", pi_medio, sqrt(pi2_medio-pi_medio*pi_medio)/sqrt(M), fabs((pi_medio-M_PI)));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement