Manipuler des tableaux

Portrait de furet

Voici un tout petit code source qui permet de manipuler des tableaux en C.
Le code est simplissime, cela permet d'avoir une mini bibliothèque/aide mémoire pas loin.
Un bon exercice consisterait à reprendre ce code et y ajouter des nouveaux tris...

tableaux.h :

  1. #ifndef TABLEAUX_H
  2. #define TABLEAUX_H
  3. #define TAILLE 10
  4. void afficheTab(long[], int);
  5. long sommeTab(long[] , int);
  6. double moyenneTab(long[], int);
  7. void copieTab(long[], long[], int);
  8. void clearMaxTab(long[], int taille, long valMax);
  9. void ordonnerTab(long[], int);
  10. void swap(long *, long *);
  11. #endif

tableaux.c

  1. #include <stdio.h>
  2. #include "tableaux.h"
  3.  
  4. int main(){
  5. long tab[TAILLE];
  6. long x = 0.0;
  7. int i;
  8. for(i=0; i<10 ; i++) tab[i]=x++;
  9.  
  10. printf("somme = %ld\r\n",sommeTab(tab, TAILLE));
  11. printf("moyenne = %lf\r\n",moyenneTab(tab,TAILLE));
  12.  
  13. long newtab[TAILLE];
  14. copieTab(tab, newtab, TAILLE);
  15. /*afficheTab(newtab, TAILLE);*/
  16.  
  17. clearMaxTab(tab, TAILLE, 2);
  18. /*afficheTab(tab, TAILLE);*/
  19.  
  20. long newtab2[TAILLE];
  21. for(i=0; i<TAILLE; i++){
  22. printf("\nveuillez entrer un nombre entier :");
  23. scanf("%ld",newtab2+i);
  24. }
  25. ordonnerTab(newtab2, TAILLE);
  26. afficheTab(newtab2, TAILLE);
  27.  
  28.  
  29. return 0;
  30. }
  31.  
  32. /*
  33. * Affiche le contenu d'un tableau sous forme décimale
  34. */
  35. void afficheTab(long tab[], int taille){
  36. int i;
  37. for(i=0; i<taille; i++) printf("tab[geshifilter-questionmarkphp]d] = %ld\r\n&quot;,i,tab[i]);&#10;}&#10;&#10;/*&#10;* Fait la somme de tous les éléments d&#039;un tableau&#10;*/&#10;long sommeTab(long tab[], int taille){&#10; long somme=0;&#10; int i=0;&#10; for(i=0 ; i&lt;taille ; i++) somme+= tab[i];&#10; return somme;&#10;}&#10;&#10;/*&#10;* Fait la moyenne algébrique de tous les éléments d&#039;un tableau&#10;*/&#10;double moyenneTab(long tab[], int taille){&#10; double somme = (double)sommeTab(tab, taille);&#10; return somme/(double)taille;&#10;}&#10;&#10;/*&#10;* Copie le contenu de tab1 dans tab2&#10;* tab2 et tab1 doivent être de taille inférieure à taille1 donnée en paramètre &#10;*/&#10;void copieTab(long tab1[], long tab2[], int taille1){&#10; int i;&#10; for(i=0; i&lt;taille1; i++) tab2[i]=tab1[i];&#10;}&#10;&#10;/*&#10;* Efface tous les éléments du tableau dont la valeur est &lt;= valMax passée en paramètre&#10;*/&#10;void clearMaxTab(long tab[], int taille, long valMax){&#10; int i;&#10; for(i=0; i&lt;taille; i++)&#10; if(tab[i]&gt;valMax) tab[i]=0;&#10;}&#10;&#10;/*&#10;* Range les éléments d&#039;un tableau par ordre croissant&#10;*/&#10;void ordonnerTab(long tab[], int taille){&#10; int j=0;&#10; int i;&#10; while(j&lt;taille){&#10; for(i=0; i&lt;taille; i++)&#10; if ( tab[i] &lt; tab[i+1] ) swap( (tab+i) , (tab+(i+1)) );&#10; j++;&#10; }&#10; &#10;}&#10;&#10;/*&#10;* Echange deux éleménts&#10;*/&#10;void swap(long *ptr1, long *ptr2){&#10; long temp;&#10; temp = *ptr1;&#10; *ptr1 = *ptr2;&#10; *ptr2 = temp;&#10;&#10;}&#10;&#10;

[/geshifilter-questionmarkphp]