1 / 6

Travaux dirigés 4

Travaux dirigés 4. Les tableaux. i,place. int maxel (a[],n,max). L’élément maximal d’un tableau et sa place. int maxel( float a[], int n, float *max) { int i,place = 0; *max = a[0]; for (i=1; i<n;i++) if (a[i] > *max) { *max = a[i]; place = i; } return place; }.

chip
Download Presentation

Travaux dirigés 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Travaux dirigés 4 Les tableaux B.Shishedjiev - Informatique II

  2. i,place int maxel (a[],n,max) L’élément maximal d’un tableau et sa place int maxel( float a[], int n, float *max) { int i,place = 0; *max = a[0]; for (i=1; i<n;i++) if (a[i] > *max) { *max = a[i]; place = i; } return place; } • max = a[0] • i=1, place=0 • max = -1e30 • i=0,place =-1 non i<n oui oui a[i]<0 && a[i]>max a[i]>max int maxel( float a[], int n, float *max) { int i,place = -1; *max = -1e-30; for (i=0; i<n;i++) if (a[i] <0 && a[i] > *max){ *max = a[i]; place = i; } return place; } non • max=a[i] • place=i • i+=1 • return place fin B.Shishedjiev - Informatique II

  3. La moyenne Faire une fonction qui calcule la moyenne des éléments positifs qui se trouvent au dessus du diagonale principal d'une matrice avec M lignes et M colonnes float moyenne2(float a[][MAXCOL], int m){ int i,j; float s = 0; int compt = 0; for (i=0; i<m;i++) for (j=0; j<m; j++) if (i<j && a[i][j] > 0){ s+=a[i][j]; compt++; } if (compt) return s/compt; else return 0; return s; } for (i=0; i<m;i++) for (j=i+1; j<m; j++) if (a[i][j] > 0){ s+=a[i][j]; compt++; } B.Shishedjiev - Informatique II

  4. k,pl,max void trisel(a[],n) Tri par sélection 5 9 2 6 3 k=n non k>1 oui pl=maxel(a,k,max) oui plk-1 non echange(a[pl]¸,a[k-1] k-=1 fin B.Shishedjiev - Informatique II

  5. Tri à la boule 5 9 2 6 3 void triboule(float a[], int n) { int i, ech; do { ech = 0; // pas d<echanges encore for (i =1; i<n; i++) if (a[i-1] > a[i]){ echange(a+i-1, a+i); ech = 1; //on a eu des echanges } }while (ech); } B.Shishedjiev - Informatique II

  6. Tri par insertion 5 9 2 6 3 piv void triins(float a[], int n) { int i, k; float piv; for (i =1; i<n; i++){ piv = a[i]; //l'element pour inserer for (k =i-1; k>=0 && piv < a[k]; k--) a[k+1] = a[k]; a[k+1]=piv; //insertion } } B.Shishedjiev - Informatique II

More Related