Programme 4 : Calcul moyenne et écart-type

text/x-c++src calcul_moyenne_ecart.cpp — 4.6 KB

Contenu du fichier

/*****************************************************/
/*                                                   */
/*  Ce programme calcule la moyenne, l'ecart-type et */
/*  la note maximale pour un ensemble de notes.      */
/*                                                   */
/*  Entree : suite de notes (reels)                  */
/*  sortie : moyenne (reel)                          */
/*           ecart_type (reel)                       */
/*           note max  (reel)                        */
/*                                                   */
/*  Auteur : Gabriel Girard                          */
/*                                                   */
/*****************************************************/

#include <iostream>
#include <cmath>

using namespace std;


// constantes globales
const int MAX = 100;


main()
{  
   // fonction utiles
   int lire_note(float []);
   float cal_moy(const float [],int);
   float cal_et(const float [], int, float);
   float trouve_max(const float [], int);
  

   // variables
   float note[MAX];
   float moyenne, ecart_type, note_max;
   int cpt;

   // lecture des notes dans un tableau
   cpt = lire_note(note);
 
   // s'il y a des notes, on fait les calculs
   if (cpt > 0) 
   {
      moyenne = cal_moy(note, cpt);
      ecart_type = cal_et(note, cpt, moyenne);
      note_max = trouve_max(note, cpt);
      cout << "La moyenne est : " << moyenne << endl;
      cout << "L'ecart-type est : " << ecart_type << endl;
      cout << "La note maximale est : " << note_max << endl;
    
  }
   else cout << "Erreur : aucune note n'est entree" << endl;
}





/*****************************************************/
/*                                                   */
/*  Cette fonction lit un ensemble de notes          */
/*                                                   */
/*  Entree :                                         */
/*  Sortie : un tableau de notes                     */
/*           le nombre de notes (retour)             */
/*                                                   */
/*****************************************************/

int lire_note(float tab[])
{
 
   int cpt = 0;
   
   cout << "Entrez les notes (terminez par 0) " << endl;
  
   do
   {  
      cin >> tab[cpt];
      cpt++;
   }
 
   while (cpt < MAX && tab[cpt-1] !=0);
      

  return cpt;
}




/*****************************************************/
/*                                                   */
/*  Cette fonction calcule la moyenne                */
/*                                                   */
/*  Entree : un tableau de notes                     */
/*           le nombre de notes                      */
/*  Sortie : la moyenne (retour)                     */
/*                                                   */
/*****************************************************/

float cal_moy(const float tab[], int nbr)
{
  float total=0;

  for(int i=0; i<nbr ; i++)
      total = total + tab[i];
  
  return ( total / nbr);
}





/*****************************************************/
/*                                                   */
/*  Cette fonction calcule l'ecart-type a partir     */
/*  de la variance.                                  */
/*                                                   */
/*  Entree : un tableau de notes                     */
/*           le nombre de notes                      */
/*           la moyenne                              */
/*  Sortie : l'ecart-type (retour)                   */
/*                                                   */
/*****************************************************/

float cal_et(const float tab[], int nbr, float moy)
{
   float total=0, ecart, variance;

   for(int i=0; i < nbr; i++)
       { ecart = tab[i]-moy;
         total = total + ecart*ecart;
       } 
   
   variance = total / (nbr -1);
   
   return (sqrt(variance));
}



/*****************************************************/
/*                                                   */
/*  Cette fonction trouve la note maximale dans une  */
/*  une liste de notes.                              */
/*                                                   */
/*  Entree : un tableau de notes                     */
/*           le nombre de notes                      */
/*  Sortie : la note maximale (retour)               */
/*                                                   */
/*****************************************************/

float trouve_max(const float tab[], int nb)
{
   float note_max;
   
   note_max = tab[0];
   for(int i=1; i<nb ; i++)
      if (note_max < tab[i])
          note_max = tab[i];

   return note_max;
}