home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************
- * CH11_07.C Pointeur dans une structure *
- * Statistiques liΘes α une sΘrie Sn de n valeurs Xi *
- * α chacune desquelles est associΘe une frΘquence fi *
- * moyenne, variance et Θcart-type pour Sn= { Xi, fi} *
- *********************************************************************/
-
- #include<stdio.h>
- #include<math.h> /* pour sqrt() */
- #include<alloc.h> /* pour malloc() et free() */
- #include<process.h> /* pour exit() */
-
- struct couple
- {
- double x, f; /* s= { x, f} */
- };
-
- struct statcouple
- {
- double sumx, sumf, sumxf, sumx_2f, /* variables intermΘdiaires */
- ex, ex_2, /* espΘrances mathΘmatiques */
- varx, sdx, /* variance et Θcart-type */
- varcx, sdcx; /* valeurs corrigΘes (n-1) */
- struct couple *tab; /* pointeur sur le tableau */
- };
-
- void LitChar( char*);
- void LitInt( int*);
- void LitDble( double*);
-
- void LitTab( struct couple*, const int);
- void VerifTab( struct couple*, const int);
-
- void LitStat( struct statcouple*, const int);
- void EcritStat( const struct statcouple*, const int);
-
- main( void)
- {
- int n;
- char choix= 'z';
- struct statcouple sc;
-
- printf(" --------------------------------------------------------"
- "--------------------\n"
- "\t\tMoyenne et variance d'une Variable alΘatoire X \n"
- " --------------------------------------------------------"
- "--------------------\n");
-
- printf("\n Entrez le nombre de couples de "
- "la sΘrie Sn= { Xi; fi}: n= ");
- do LitInt( &n);
- while( n< 2);
-
- sc.tab= ( struct couple*) malloc( n* sizeof( struct couple) );
- if( sc.tab== NULL )
- {
- printf(" L'allocation de mΘmoire n'a pu Ωtre faite !");
- exit(1);
- }
-
- LitTab( sc.tab, n);
- LitStat( &sc, n);
- EcritStat( &sc, n);
-
- while( choix != 'F')
- {
- printf("\n\t ---------------------------------------\n"
- "\t VΘrification\n"
- "\t Fin\n"
- "\t ---------------------------------------\n");
-
- LitChar( &choix);
- if( ( choix> 96) && ( choix< 123) ) choix-= 32;
-
- switch( choix)
- {
- case 'V': VerifTab( sc.tab, n);
- LitStat( &sc, n);
- EcritStat( &sc, n);
- break;
-
- case 'F': break;
- default: printf("erreur! composez V ou F\n");
- }
- }
- free( sc.tab); /* libΘration de la mΘmoire */
- printf("\t\t*** fin du programme ***");
- }
-
- void LitTab( struct couple *t1, const int n1)
- {
- int i;
-
- for( i= 0; i< n1; i++)
- {
- printf(" Entrez le couple n░ %d\t x: ", i+ 1);
- LitDble( &( t1+ i)->x);
- printf("\t\t\t f: ");
- LitDble( &( t1+ i)->f);
- }
- }
-
- void VerifTab( struct couple *t2, const int n2)
- {
- char choix1= 'w';
- int i;
-
- for( i= 0; i< n2; i++)
- {
- do
- {
- printf(" Couple n░ %d: x= %lf , f= %lf"
- "\t Oui ou Non? ", i+ 1, (t2+ i)->x, (t2+ i)->f);
- LitChar( &choix1);
- if( choix1!= 'O' && choix1!= 'o')
- {
- printf(" Corrigez le couple n░ %d\t\t x: ", i+ 1);
- LitDble( &( t2+ i)->x);
- printf("\t\t\t\t\t f: ");
- LitDble( &( t2+ i)->f);
- }
- }
- while( choix1!= 'O' && choix1!= 'o');
- }
- }
-
- void LitStat( struct statcouple *s0, const int n0)
- {
- int i;
- double k, p;
-
- s0->sumx= s0->sumf= s0->sumxf= s0->sumx_2f= 0;
-
- for( i= 0; i< n0; i++)
- {
- k= ( s0->tab+ i)->x;
- p= ( s0->tab+ i)->f;
-
- s0->sumx+= k;
- s0->sumf+= p;
- s0->sumxf+= ( k* p);
- s0->sumx_2f+= ( k* k* p);
- }
-
- s0->ex= s0->sumxf/ s0->sumf;
- s0->ex_2= s0->sumx_2f/ s0->sumf;
- s0->varx= s0->ex_2- s0->ex* s0->ex;
- s0->sdx= sqrt( s0->varx);
- s0->varcx= s0->varx* ( 1.0* n0/ ( n0- 1) );
- s0->sdcx= sqrt( s0->varcx);
- }
-
- void EcritStat( const struct statcouple *s1, const int n1)
- {
- printf("\n nombre de couples: %d"
- "\n Somme( Xi)= %lf , Somme( fi)= %lf"
- "\n Somme( Xi.fi)= %lf , Somme( Xi▓.fi)= %lf"
- "\n Moyenne( Xi): E( Xi)= %lf , E( Xi▓)= %lf"
- "\n Variance( Xi)= %lf , Ecart-type( Xi)= %lf"
- "\n Var corrigΘe( Xi)= %lf"
- " , Ecart-type corrigΘ( Xi)= %lf",
- n1, s1->sumx, s1->sumf, s1->sumxf, s1->sumx_2f,
- s1->ex, s1->ex_2, s1->varx, s1->sdx,
- s1->varcx, s1->sdcx);
- }
-
- void LitChar( char *c0)
- {
- while( ( *c0= getchar() )== '\n');
- while( getchar() != '\n');
- }
-
- void LitInt( int *entier)
- {
- double dble;
- do
- {
- while( scanf("%lf", &dble)!= 1)
- while( getchar() != '\n');
- while( getchar() != '\n');
- }
- while( dble< -32768.0 || dble> 32767.0);
- *entier= (int) dble;
- }
-
- void LitDble( double *reel)
- {
- while( scanf("%lf", reel)!= 1)
- while( getchar() != '\n');
- while( getchar() != '\n');
- }
-
- /*
- ----------------------------------------------------------------------------
- Moyenne et variance d'une Variable alΘatoire X
- ----------------------------------------------------------------------------
-
- Entrez le nombre de couples de la sΘrie: Sn= { Xi; fi} 5
- Entrez le couple n░ 1: x: 61
- f: 50
- Entrez le couple n░ 1: x: 64
- f: 180
- Entrez le couple n░ 1: x: 67
- f: 420
- Entrez le couple n░ 1: x: 70
- f: 270
- Entrez le couple n░ 1: x: 73
- f: 80
- nombre de couples: 5
- Somme( Xi)= 335.00 , Somme( fi)= 1000.00
- Somme( Xi.fi)= 67450.00 , Somme( Xi▓.fi)= 4558030.00
- Moyenne( Xi)= E( Xi)= 67.45 , E( Xi▓)= 4558.03
- Variance( Xi)= 8.53 , Ecart-type( Xi)= 2.92
- Var corrigΘe( Xi)= 10.66 , Ecart-type corrigΘ( Xi)= 3.26
- ---------------------------------------
- VΘrification
- Fin
- ---------------------------------------
- v
- Couple n░ 1: x= 61.00 , f= 50.00 Oui ou Non? o
- Couple n░ 2: x= 64.00 , f= 180.00 Oui ou Non? o
- Couple n░ 3: x= 67.00 , f= 420.00 Oui ou Non? o
- Couple n░ 4: x= 70.00 , f= 270.00 Oui ou Non? o
- Couple n░ 5: x= 73.00 , f= 80.00 Oui ou Non? n
- Corrigez le couple n░ 5 x: 73
- f: 80
- Couple n░ 5: x= 73.00 , f= 80.00 Oui ou Non? o
- nombre de couples: 5
- Somme( Xi)= 335.00 , Somme( fi)= 1000.00
- Somme( Xi.fi)= 67450.00 , Somme( Xi▓.fi)= 4558030.00
- Moyenne( Xi)= E( Xi)= 67.45 , E( Xi▓)= 4558.03
- Variance( Xi)= 8.53 , Ecart-type( Xi)= 2.92
- Var corrigΘe( Xi)= 10.66 , Ecart-type corrigΘ( Xi)= 3.26
- ---------------------------------------
- VΘrification
- Fin
- ---------------------------------------
- f
- *** fin du programme ***
- */