70 likes | 198 Vues
This document provides detailed implementations of various functions in C for handling arrays and matrices. It includes methods for finding the maximum element in an array, calculating the average of positive elements above the main diagonal of a matrix, and performing selection and insertion sorts. Each function is explained with its respective logic, parameters, and return values, which are designed for users looking to understand array and matrix operations in C programming. It aims to enhance computational efficiency and develop programming skills.
E N D
Travaux dirigés 4 Les tableaux B.Shishedjiev - Informatique II
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
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
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 plk-1 non echange(a[pl]¸,a[k-1] k-=1 fin B.Shishedjiev - Informatique II
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
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