Programma con il metodo hit and miss

di il
3 risposte

Programma con il metodo hit and miss

Ho un problema con questo esercizio (se serve poi allego anche il testo).
Succede che quando voglio scrivere su file le coordinate di x e y
   f = fopen("coordinate.txt","w");  
   for(i=0; i<N; i++)
     {
       genconv(&x,&y);
       if (isnicirlce(x, y, N, XC2, YC2, R2))
         { 
           fprintf(f, "%lf %lf\n",x, y);
           bhit++;   
         }   
      }
   fclose(f);
e poi controllo sul file quali siano effettivamente noto una discrepanza.
Prima ho provato a farmele stampare e mi venivano molti valori diversi da 0, mentre ora controllando il file noto invece che ci sono moltissimi valori uguali a 0. Come mai? Sinceramente non credo sia normale

Svolgimento completo
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define XC1 0.0
#define YC1 0.0
#define R1 2.0
#define XC2 -0.8
#define YC2 0.0
#define R2 1.2
#define L 4.0
#define N 10000

void genconv(double *x, double *y);
int isnicirlce(double z, double k, int n, double xc, double yc, double R);
int main() 
{
   double F, x, y, pcoord[N][2], H;
   int i, hit=0, bhit=0;
   FILE *f;
   srand(time(NULL));
   for(i=0; i<N; i++)
     {
        genconv(&x,&y);
        pcoord[i][0] = x;
        pcoord[i][1] = y;
        if (isnicirlce(x, y, N, XC1, YC1, R1))
          {
            hit++;   
          }   
     }
   f = fopen("coordinate.txt","w");  
   for(i=0; i<N; i++)
     {
       genconv(&x,&y);
       if (isnicirlce(x, y, N, XC2, YC2, R2))
         { 
           fprintf(f, "%lf %lf\n",x, y);
           bhit++;   
         }   
      }
   fclose(f);
   F = (L*L) * hit/(double)N;
   H = F - (L*L) * bhit/(double)N;
   printf("Numero di Lanci: %d | Area della figura a: %lf | Area della figura b: %lf\n", N, F, H);
     
}

void genconv(double *x, double *y)
{
   double a,b;
   a = -2+((double)rand()/RAND_MAX)*4;
   b = -2+((double)rand()/RAND_MAX)*4;
   *y = b;
   *x = a;
}

int isnicirlce(double z, double k, int n, double xc, double yc, double R)
{
   double distanza;
   distanza = sqrt(pow(z - xc,2) + pow(k - yc,2));
   if (distanza < R)
     {
       return 1;   
     }
   else 
     {
       return 0;
     }
}

3 Risposte

  • Re: Programma con il metodo hit and miss

    Più che altro dovresti evidenziare meglio il problema.

    Mostraci il contenuto del file e i dati che, secondo te, non sono corretti.

    A me risultano tutti diversi da zero.

    Con cosa compili?
  • Re: Programma con il metodo hit and miss

    Le ultime coordinate che riesco a vedere facendomi stampare con printf
    -1.854122 -0.102115
    0.165227 -0.007141
    -0.075381 -0.389843
    -1.358135 1.041963
    -1.018525 0.905972
    -0.939421 1.092502
    -0.023377 0.096500
    -0.417310 0.982391
    -1.128391 -0.562944
    -0.750938 -0.377636
    -0.361888 -0.037660
    -1.163182 0.458693
    -0.973968 0.776818
    -0.410108 1.056856
    -1.655141 0.552324
    -1.012665 -0.113468
    -1.670522 -0.197943
    0.068056 0.280587
    -1.193701 0.409986
    -0.870815 0.198675
    -0.189764 0.927213
    -1.083590 -0.017151
    -1.265236 -0.834803
    -0.043275 -0.445265
    -0.003479 0.422559
    -1.257790 -0.149358
    -0.265816 1.005829
    -1.423811 -0.860561
    -0.723960 -0.607135
    -0.617512 0.175237
    -0.003479 -0.292062
    -0.595050 0.378979
    -0.452467 1.087619
    -0.540483 1.024140
    -0.988739 -1.102756
    -0.688314 -0.049623
    -0.724326 0.982269
    -0.158635 -0.767418
    -0.872524 0.055605
    -1.950682 -0.316965
    -0.716025 1.110446
    -1.181372 -0.182073
    -0.366405 0.801233
    0.294748 -0.442824
    -1.169774 0.185369
    -1.687613 0.499100
    -1.214454 -0.023377
    -1.349223 0.962493
    -1.113987 0.133732
    -0.226264 0.981414
    -0.791345 0.735679
    -0.150334 0.934172
    -1.119480 1.097873
    -1.028901 0.152776
    -0.312937 -0.532914
    -0.683920 0.192450
    -0.862026 -1.099948
    -0.816736 -0.477737
    -0.863124 0.131291
    -0.801111 -0.779138
    -1.354595 0.476882
    -0.610920 -0.972259
    -0.970672 1.062471
    -1.132908 -1.081271
    -0.555254 -1.031831
    -0.976043 0.121647
    -1.154515 0.024720
    -1.186865 -0.985076
    -0.629353 -0.495560
    -0.148991 -0.218574
    0.146306 -0.192450
    -1.349345 -0.733360
    -0.737999 -0.938078
    -0.888516 0.449538
    -1.526841 0.794397
    -1.644765 -0.806726
    -1.250587 0.642170
    -1.033662 0.930387
    -0.602130 -0.592730
    0.362621 -0.283639
    -1.106906 -0.970183
    -0.524003 -0.003723
    -1.477523 -0.823939
    -0.315622 -0.959685
    -1.175024 0.828455
    0.004212 0.518876
    -0.521683 -0.845424
    -1.507797 -0.409742
    -0.149480 0.318796
    -1.365093 0.561480
    -0.444166 -0.396435
    -1.332987 -0.406934
    -0.228095 0.803552
    0.185858 -0.627766
    -0.062563 -0.717368
    -0.941862 0.151311
    -1.797235 0.619953
    0.143254 -0.486404
    -1.175146 -0.713462
    -1.588733 -0.651326
    -1.807855 -0.049623
    -1.203345 -0.551469
    -1.317972 0.661336
    -0.403760 0.211249
    -1.255104 -0.715659
    0.126530 -0.728721
    -0.341014 0.547807
    -0.849330 0.995087
    -1.583117 -0.587848
    -0.723472 0.326365
    -0.737999 0.618732
    -0.206610 0.836390
    0.154607 -0.030580
    -0.997650 0.148625
    -0.274606 0.595416
    -1.389142 -0.151189
    0.035585 0.467483
    0.115055 0.582965
    -1.508164 0.082705
    -0.110782 -0.921964
    -0.481155 1.013520
    -1.545030 0.255196
    -0.159490 -0.538652
    -0.533891 -1.144627
    0.080142 -0.678549
    -1.587024 -0.745811
    -0.984588 0.931486
    -1.624500 -0.745689
    -1.376812 -0.414624
    -1.760247 -0.590533
    -0.220405 -0.236030
    -0.310007 -1.073458
    -0.744346 -0.748863
    0.227119 0.219184
    -1.328227 0.160100
    -1.118625 0.805506
    -0.401685 0.872280
    0.301218 -0.215766
    -0.673788 0.245674
    -0.224921 -0.050233
    -0.136906 0.245186
    -0.369579 1.093234
    0.063051 0.486038
    -1.471908 -0.807337
    -1.375103 0.726890
    0.070986 0.063051
    -0.909391 -0.231880
    0.394848 -0.074526
    -1.009980 0.858608
    -0.157659 0.706748
    -1.386944 0.479446
    -1.527451 -0.875820
    -1.540391 0.132878
    -1.244606 0.404248
    -1.417219 0.640095
    -0.250069 -0.126286
    -0.144963 0.108463
    0.084292 -0.633992
    -0.702475 0.365673
    -0.835047 -0.311716
    -1.453352 -0.519852
    -0.364086 1.042085
    0.302683 0.234687
    -1.675283 0.247383
    0.096622 0.410840
    -0.939787 -0.612018
    -1.539781 0.901212
    0.209540 -0.420972
    -1.457259 -0.204413
    -1.719840 -0.458815
    -0.268258 -0.642415
    -0.900845 -0.102725
    -0.208441 0.170843
    0.063417 -0.355907
    -1.921873 0.394360
    -0.849452 0.086123
    -0.975433 -1.141087
    -0.258858 -0.422193
    -0.360179 0.087100
    -1.025483 0.100162
    -0.850063 -0.875210
    -0.054140 -0.824793
    -0.608966 0.412427
    -0.524613 -0.413404
    -1.311380 -0.480178
    -0.940519 -0.634724
    0.360668 0.047304
    -0.436598 -1.131932
    -0.738731 0.196600
    -0.141057 0.865566
    -1.356182 -1.052095
    0.037049 0.124332
    -1.892697 0.044496
    0.091250 0.668661
    -0.110416 -0.674642
    -1.575549 0.472488
    -1.174657 -0.057070
    -1.173559 -0.980804
    -0.747765 0.093936
    -0.952849 -0.798547
    -1.687002 -0.624470
    -1.200903 -1.066256
    -0.080874 0.445021
    -1.573595 -0.470412
    -0.095889 -0.750328
    -0.758629 0.874844
    -0.094424 0.171941
    -1.230567 0.182684
    0.208441 0.505448
    -0.853359 -1.138035
    -1.120334 1.096286
    -0.439161 -0.257149
    -1.358745 -0.958953
    -0.720176 -0.302927
    -0.470901 0.460768
    -1.469344 0.700278
    -0.489822 0.527421
    -0.933805 0.653157
    -0.211249 0.290475
    -0.672079 -0.890469
    -1.266091 1.067598
    -0.751793 -0.676351
    -1.240944 0.926969
    -0.496048 0.794519
    -1.030610 0.420240
    -1.715445 0.702963
    -0.597736 -0.084048
    -0.356883 0.842616
    -1.492904 -0.364696
    -0.796838 0.523637
    -0.152043 -0.054262
    -0.343455 -0.138493
    -0.376415 -0.061831
    -1.027192 -0.837489
    -0.307077 0.309153
    -0.144230 0.792077
    -0.786706 -0.722251
    -0.402783 0.762780
    -0.246651 -0.298532
    -1.789178 -0.394604
    -0.661702 0.114933
    -1.465072 0.394604
    0.085513 -0.666219
    -0.025208 -0.360057
    -0.436598 0.123234
    -0.856655 0.613361
    -1.538682 -0.382397
    -0.281564 1.017304
    -1.160253 0.088809
    -0.461379 -0.561357
    -0.098941 0.742027
    -0.169988 -0.072085
    -0.758385 0.561968
    -0.006531 -0.342601
    0.295236 -0.072695
    -0.160344 0.277413
    -1.806024 0.292062
    -0.392529 0.248604
    -0.924528 -1.032929
    0.074038 -0.478347
    -0.250679 -0.906949
    -0.815638 -0.398022
    -1.606189 -0.820521
    -0.985565 -0.991058
    -0.830775 -0.263619
    -0.478469 1.056368
    -0.229316 1.039766
    -1.035249 -0.988739
    -0.076357 0.634480
    -1.367534 0.440626
    -0.565752 0.961516
    -1.261208 0.640706
    -1.104831 0.681967
    0.168523 -0.517045
    -0.936369 0.737144
    -0.537309 -0.609699
    -1.524155 -0.472365
    -1.007050 -0.636433
    -0.069399 0.198675
    -1.197607 0.651448
    -0.463942 0.588702
    -1.533555 0.137028
    -0.695639 0.807825
    -1.527940 0.005066
    -0.084048 0.343333
    -0.135319 -0.255318
    -1.261452 -0.494339
    -0.103702 -0.389599
    -1.509629 0.542802
    -1.010102 -1.102268
    -1.496811 0.800012
    -0.368847 -0.805139
    -0.376171 -0.600543
    -1.029511 -0.600909
    -1.313700 1.065401
    0.252266 -0.367870
    -0.914029 0.430006
    

    ultime presenti sul file
    0.302072 0.000000
    0.000000 -0.518632
    0.000000 0.000000
    0.075991 0.000000
    -1.616810 0.000000
    0.000000 0.000000
    -0.337107 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.972503 0.000000
    -0.170598 -0.129826
    0.000000 0.000000
    -0.558428 0.000000
    0.000000 0.000000
    -0.740806 0.000000
    0.000000 0.000000
    -1.146336 0.000000
    -1.230323 0.000000
    -0.830409 0.000000
    0.000000 0.615802
    0.000000 0.000000
    -0.377148 0.000000
    -0.119938 0.000000
    0.000000 0.000000
    -1.213111 0.000000
    -1.214087 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.604938 0.503861
    0.000000 0.000000
    0.000000 0.000000
    -0.064028 0.000000
    0.000000 0.000000
    -0.939055 0.000000
    0.000000 0.860561
    -0.487991 0.000000
    0.000000 0.000000
    -1.469832 0.000000
    0.000000 0.000000
    -1.138279 0.000000
    0.000000 0.000000
    -0.694418 0.000000
    -1.073092 0.000000
    -0.950407 0.000000
    -0.747642 0.000000
    -0.135563 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.084902
    0.000000 0.000000
    -1.473983 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.331736 0.000000
    -1.306009 0.000000
    -1.843623 0.000000
    0.181097 0.000000
    -0.747765 0.000000
    0.000000 0.000000
    0.343211 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.676595 0.000000
    -1.397198 0.894986
    0.000000 0.000000
    -0.440870 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.864589 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -1.476547 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -1.307962 0.000000
    -1.280129 0.000000
    -1.422712 0.070864
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.927335 0.000000
    -0.835902 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.122013 -0.170476
    -0.496414 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.957732 0.000000
    0.000000 0.000000
    -1.851070 0.000000
    0.000000 0.000000
    0.179998 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -1.799554 -0.540605
    -0.157537 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.442335 0.000000
    -1.147069 0.000000
    -0.620563 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 -0.772301
    0.000000 0.000000
    0.985565 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.403272 0.000000
    -1.062349 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.296945 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.355052
    -1.280374 0.000000
    0.000000 0.000000
    0.000000 0.135319
    0.000000 0.000000
    -0.860073 0.000000
    0.000000 -0.616291
    0.000000 0.000000
    -1.505356 0.000000
    0.000000 -0.664754
    0.000000 0.000000
    -0.174139 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.047548 0.000000
    0.000000 0.000000
    0.082461 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -0.597613 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -1.038057 0.000000
    -0.965911 0.000000
    0.000000 0.000000
    -0.615070 0.000000
    0.000000 0.000000
    0.000000 0.000000
    0.081484 0.000000
    0.000000 -0.283639
    0.141911 0.000000
    -0.914884 0.000000
    0.000000 0.000000
    0.000000 0.000000
    -1.404401 0.000000
    -1.840205 0.000000
    comunque compilo con Codelite su windows 10
  • Re: Programma con il metodo hit and miss

    Prova con altro compilatore
Devi accedere o registrarti per scrivere nel forum
3 risposte