- #include <iostream.h> //cout et cin
- #include <string.h> //strcmp
- #include <stdlib.h> //realloc ,free,exit
-
-
-
- void main()
- {
- char buffer[128];
- char a ='o';
-
- double *p=NULL;
-
- double som=0;
- double moy=0;
-
- int count=0;
- int i=0;
-
-
- cout<<"Saisir les chiffre pour effectuer une moyenne"<<endl;
- cout<<"Finir la saisie par f"<<endl;
- cout<<"Sortir du prog en tapant exit"<<endl;
-
-
- do
- {
- count=0;
- moy=0;
- som=0;
-
- do
- {
-
-
- cout<<"Saisie numero "<<count+1<<": ";
-
- cin.getline(buffer,128);
-
- if(strcmp("f",buffer))
- {
- if ((p= (double*) realloc (p,(count+1)*sizeof(double)))==NULL)
- {
- cout<<"Allocation memoire echouée";
- break;
- }
- p[count]=atoi(buffer); //transforme de char en int
- count++;
- }
- }while(buffer[0]!='f');
-
-
- if(count>0)
- {
- for(i=0;i<count;i++)
- {
- som+=p[i];
- }
- moy=som/count;
-
- }
-
- cout<<"la moyenne est :"<<moy<<endl;
-
-
-
- cout<<"Voulez effectuer une autre saisie ? o/n"<<endl;
- cin>>a;
-
- while(cin.get()!='\n'); //pour vider la memoire tampon
-
- }while(a != 'n');
-
- if(p!=NULL)
- {
- free(p);
- exit(1);
- }
-
- }
-
-
-
-
#include <iostream.h> //cout et cin
#include <string.h> //strcmp
#include <stdlib.h> //realloc ,free,exit
void main()
{
char buffer[128];
char a ='o';
double *p=NULL;
double som=0;
double moy=0;
int count=0;
int i=0;
cout<<"Saisir les chiffre pour effectuer une moyenne"<<endl;
cout<<"Finir la saisie par f"<<endl;
cout<<"Sortir du prog en tapant exit"<<endl;
do
{
count=0;
moy=0;
som=0;
do
{
cout<<"Saisie numero "<<count+1<<": ";
cin.getline(buffer,128);
if(strcmp("f",buffer))
{
if ((p= (double*) realloc (p,(count+1)*sizeof(double)))==NULL)
{
cout<<"Allocation memoire echouée";
break;
}
p[count]=atoi(buffer); //transforme de char en int
count++;
}
}while(buffer[0]!='f');
if(count>0)
{
for(i=0;i<count;i++)
{
som+=p[i];
}
moy=som/count;
}
cout<<"la moyenne est :"<<moy<<endl;
cout<<"Voulez effectuer une autre saisie ? o/n"<<endl;
cin>>a;
while(cin.get()!='\n'); //pour vider la memoire tampon
}while(a != 'n');
if(p!=NULL)
{
free(p);
exit(1);
}
}