home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / alb_c10 / chap_11 / ch11_07.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-08  |  6.5 KB  |  240 lines

  1. /*********************************************************************
  2. *  CH11_07.C                          Pointeur dans une structure  *
  3. *             Statistiques liΘes α une sΘrie Sn de n valeurs Xi  *
  4. *                α chacune desquelles est associΘe une frΘquence fi  *
  5. *         moyenne, variance et Θcart-type pour Sn= { Xi, fi}  *
  6. *********************************************************************/
  7.  
  8. #include<stdio.h>
  9. #include<math.h>                /* pour sqrt()                */
  10. #include<alloc.h>               /* pour malloc() et free()  */
  11. #include<process.h>             /* pour exit()                */
  12.  
  13. struct couple
  14. {
  15.     double x, f;                        /*   s= { x, f}             */
  16. };
  17.  
  18. struct statcouple
  19. {
  20.     double sumx, sumf, sumxf, sumx_2f,  /* variables intermΘdiaires */
  21.        ex, ex_2,                    /* espΘrances mathΘmatiques */
  22.        varx, sdx,                   /* variance et Θcart-type   */
  23.        varcx, sdcx;            /* valeurs corrigΘes (n-1)  */
  24.     struct couple *tab;                 /* pointeur sur le tableau  */
  25. };
  26.  
  27. void LitChar( char*);
  28. void LitInt( int*);
  29. void LitDble( double*);
  30.  
  31. void LitTab( struct couple*, const int);
  32. void VerifTab( struct couple*, const int);
  33.  
  34. void LitStat( struct statcouple*, const int);
  35. void EcritStat( const struct statcouple*, const int);
  36.  
  37. main( void)
  38. {
  39.    int n;
  40.    char choix= 'z';
  41.    struct statcouple sc;
  42.  
  43.    printf(" --------------------------------------------------------"
  44.       "--------------------\n"
  45.       "\t\tMoyenne et variance d'une Variable alΘatoire X  \n"
  46.       " --------------------------------------------------------"
  47.       "--------------------\n");
  48.  
  49.    printf("\n Entrez le nombre de couples de "
  50.                       "la sΘrie Sn= { Xi; fi}:  n= ");
  51.    do LitInt( &n);
  52.    while( n< 2);
  53.  
  54.    sc.tab= ( struct couple*) malloc( n* sizeof( struct couple) );
  55.    if( sc.tab== NULL )
  56.      {
  57.           printf(" L'allocation de mΘmoire n'a pu Ωtre faite !");
  58.           exit(1);
  59.      }
  60.  
  61.    LitTab( sc.tab, n);
  62.    LitStat( &sc, n);
  63.    EcritStat( &sc, n);
  64.  
  65.    while( choix != 'F')
  66.      {
  67.     printf("\n\t     ---------------------------------------\n"
  68.     "\t         VΘrification\n"
  69.     "\t         Fin\n"
  70.     "\t     ---------------------------------------\n");
  71.  
  72.     LitChar( &choix);
  73.     if(  ( choix> 96) && ( choix< 123) )   choix-= 32;
  74.  
  75.     switch( choix)
  76.     {
  77.       case 'V':    VerifTab( sc.tab, n);
  78.             LitStat( &sc, n);
  79.             EcritStat( &sc, n);
  80.             break;
  81.  
  82.       case 'F':    break;  
  83.       default:    printf("erreur! composez V ou F\n");
  84.     }
  85.      }
  86.    free( sc.tab);               /* libΘration de la mΘmoire    */
  87.    printf("\t\t***  fin du programme  ***");
  88. }
  89.  
  90. void LitTab(  struct couple *t1, const int n1)
  91. {
  92.     int i;
  93.  
  94.     for( i= 0; i< n1; i++)
  95.       {
  96.           printf(" Entrez le couple n░ %d\t x:  ", i+ 1);
  97.           LitDble( &( t1+ i)->x);
  98.           printf("\t\t\t f:  ");
  99.           LitDble( &( t1+ i)->f);
  100.       }
  101. }
  102.  
  103. void VerifTab( struct couple *t2, const int n2)
  104. {
  105.     char choix1= 'w';
  106.         int i;
  107.  
  108.     for( i= 0; i< n2; i++)
  109.     {
  110.       do
  111.       {
  112.          printf(" Couple n░ %d:   x= %lf , f= %lf"
  113.             "\t Oui ou Non?  ", i+ 1, (t2+ i)->x, (t2+ i)->f);
  114.          LitChar( &choix1);
  115.          if( choix1!= 'O' && choix1!= 'o')
  116.          {
  117.           printf(" Corrigez le couple n░ %d\t\t x:  ", i+ 1);
  118.           LitDble( &( t2+ i)->x);
  119.           printf("\t\t\t\t\t f:  ");
  120.           LitDble( &( t2+ i)->f);
  121.              }
  122.       }
  123.       while( choix1!= 'O' && choix1!= 'o');
  124.     }
  125. }
  126.  
  127. void LitStat( struct statcouple *s0, const int n0)
  128. {
  129.     int i;
  130.     double k, p;
  131.  
  132.     s0->sumx= s0->sumf= s0->sumxf= s0->sumx_2f= 0;
  133.  
  134.     for( i= 0; i< n0; i++)
  135.     {
  136.        k= ( s0->tab+ i)->x;
  137.        p= ( s0->tab+ i)->f;
  138.  
  139.        s0->sumx+= k;
  140.        s0->sumf+= p;
  141.        s0->sumxf+= ( k* p);
  142.        s0->sumx_2f+= ( k* k* p);
  143.     }
  144.  
  145.     s0->ex= s0->sumxf/ s0->sumf;
  146.     s0->ex_2= s0->sumx_2f/ s0->sumf;
  147.     s0->varx= s0->ex_2- s0->ex* s0->ex;
  148.     s0->sdx= sqrt( s0->varx);
  149.     s0->varcx= s0->varx* ( 1.0* n0/ ( n0- 1) );
  150.     s0->sdcx= sqrt( s0->varcx);
  151. }
  152.  
  153. void EcritStat( const struct statcouple *s1, const int n1)
  154. {
  155.     printf("\n nombre de couples: %d"
  156.            "\n Somme( Xi)= %lf , Somme( fi)= %lf"
  157.            "\n Somme( Xi.fi)= %lf , Somme( Xi▓.fi)= %lf"
  158.            "\n Moyenne( Xi): E( Xi)= %lf , E( Xi▓)= %lf"
  159.            "\n Variance( Xi)= %lf , Ecart-type( Xi)= %lf"
  160.            "\n Var corrigΘe( Xi)= %lf"
  161.                   " , Ecart-type corrigΘ( Xi)= %lf",
  162.         n1, s1->sumx, s1->sumf, s1->sumxf, s1->sumx_2f,
  163.         s1->ex, s1->ex_2, s1->varx, s1->sdx,
  164.         s1->varcx, s1->sdcx);
  165. }
  166.  
  167. void LitChar( char *c0)
  168. {
  169.      while( ( *c0= getchar() )== '\n');
  170.     while( getchar() != '\n');  
  171. }
  172.  
  173. void LitInt( int *entier)
  174. {
  175.     double dble;
  176.     do
  177.         {
  178.        while( scanf("%lf", &dble)!= 1)
  179.         while( getchar() != '\n');
  180.        while( getchar() != '\n');
  181.     }
  182.     while( dble< -32768.0 || dble> 32767.0);
  183.         *entier= (int) dble;
  184. }
  185.  
  186. void LitDble( double *reel)
  187. {
  188.     while( scanf("%lf", reel)!= 1)
  189.         while( getchar() != '\n');
  190.     while( getchar() != '\n');
  191. }
  192.  
  193. /*
  194. ----------------------------------------------------------------------------
  195.      Moyenne et variance d'une Variable alΘatoire X  
  196. ----------------------------------------------------------------------------
  197.  
  198.  Entrez le nombre de couples de la sΘrie:  Sn= { Xi; fi}     5
  199.  Entrez le couple n░ 1:     x: 61
  200.              f: 50
  201.  Entrez le couple n░ 1:     x: 64
  202.              f: 180
  203.  Entrez le couple n░ 1:     x: 67
  204.              f: 420
  205.  Entrez le couple n░ 1:     x: 70
  206.              f: 270
  207.  Entrez le couple n░ 1:     x: 73
  208.              f: 80
  209.  nombre de couples: 5
  210.  Somme( Xi)= 335.00 , Somme( fi)= 1000.00
  211.  Somme( Xi.fi)= 67450.00 , Somme( Xi▓.fi)= 4558030.00
  212.  Moyenne( Xi)= E( Xi)= 67.45 , E( Xi▓)= 4558.03
  213.  Variance( Xi)= 8.53 , Ecart-type( Xi)= 2.92
  214.  Var corrigΘe( Xi)= 10.66 , Ecart-type corrigΘ( Xi)= 3.26
  215.         ---------------------------------------
  216.                 VΘrification
  217.                 Fin
  218.         ---------------------------------------
  219.  v
  220.  Couple n░ 1:   x= 61.00 , f= 50.00     Oui ou Non?  o
  221.  Couple n░ 2:   x= 64.00 , f= 180.00     Oui ou Non?  o
  222.  Couple n░ 3:   x= 67.00 , f= 420.00     Oui ou Non?  o
  223.  Couple n░ 4:   x= 70.00 , f= 270.00     Oui ou Non?  o
  224.  Couple n░ 5:   x= 73.00 , f= 80.00     Oui ou Non?  n
  225.  Corrigez le couple n░ 5     x:  73
  226.                  f:  80
  227.  Couple n░ 5:   x= 73.00 , f= 80.00     Oui ou Non?  o
  228.  nombre de couples: 5
  229.  Somme( Xi)= 335.00 , Somme( fi)= 1000.00
  230.  Somme( Xi.fi)= 67450.00 , Somme( Xi▓.fi)= 4558030.00
  231.  Moyenne( Xi)= E( Xi)= 67.45 , E( Xi▓)= 4558.03
  232.  Variance( Xi)= 8.53 , Ecart-type( Xi)= 2.92
  233.  Var corrigΘe( Xi)= 10.66 , Ecart-type corrigΘ( Xi)= 3.26
  234.         ---------------------------------------
  235.                 VΘrification
  236.                 Fin
  237.         ---------------------------------------
  238.  f
  239.         ***  fin du programme  ***
  240. */