Vous êtes sur la page 1sur 17

RQ:

kol fonction w num mte3ha ahbet taa talka kol chay mrateb lawej bel numero

Sommaoire De Mes fonction :

************************************************************** kol chay testhakou


omour tableau ***********************************

-1 Retourner la somme des éléments (entiers) d'un tableau


/ -18 affichage tableau de structure

-2 Retourner la somme des éléments (entiers) d'un tableau (pointeur)


/ -19 calcul moyenne ( dans un tableau )

-3 Afficher une matrice


/ -20 calcul du pourcentage (dans un tableau )

-4 Afficher un tableau / -21


calcul du variance (dans un tableau )

-5 remplissage d'un tableau simple / -22


ajouter une valeur ala fin du tableau

-6 remplissage du tableau de structure / -23


ajouter une valeau ala fin du tableau ( dans le cas ykolk elle n'existe pas)

-7 remplissage du tableau d'entiers avec pointeur / -24


insertion dune valeur a son indice donne dans un tableau avec tri normale du tab

- 8 Remplir un tableau d'entier dans l'ordre croissant /-25


exemple comment saisir une structure

- 9 Remplir un tableau par des éléments distincts /-26


verification de lexistance dun element dans un tableau w nraj3 position te3ou ken
mawjoud snn -1

***************
- 10 Tri par Séléction
/-27 verification ken mawjoud wale ( nraj3 1 ken mawjoud snn 0)

- 11 Tri a bul /-28 verification te3


7aja mawjouda wela lee bil pointeur exp: (tableau)

- 12 Tri par insertion /-29 fonction


permuter deux entiers ( tableau )
****************
- 13 Recherche Dichotomique (retourne l'indice sinon -1) /-30
modifier une valeur dans un tableau

- 14 Recherche Séquentielle (retourne l'indice sinon -1) /-31


modifier une valeur dans le tab kol wyn nel9aha nbadelha
****************
- 15 Suppression d'un élément x dans un tableau T de n éléments /-32
supprimer la premiere occurence te3 val fy tab

- 16 Inversion des éléments d'un tableau /-33


supprimer toutes les occurences te3 val fy tab
- 17 Elimination de toutes les occurences de x dans un tableau T (pointeurs)
/-39 // initialiser 7aja mil matrice à 0

- -40 // fonction modifier etat


/-44 saisir tableau matrice et affichage (2 eme version)

- -41 supprimer a une position donne /-46 remplir tab


croissant 2 eme version

--42 rechercher de min et max avec pointeur et sans pointeur /-47


ajouter un element en tab 2eme version

- -43 fonction recherche max

***** **************************************************** kol chay testhakou


cote Manipulation des chaines
***********************************************************************************
*************************

-34 // fonction qui cherche un caractere dans une chaine et rend la pos de sa
dernière occurance ken mawjoud w -1 si nn

-35 // fonction qui determine les pos de toutes les occ d'un caractere dans une une
chaine w n7othom fy tab
-36 // fonction qui supprime toutes les occ d'un caractere dans une une chaine
-37 // fonction qui determine le nbre d'occurence d'un caractere dans une chaine
-38 //permuter deux chaines
-45 //permuter deux chaines

***********************************************************************************
************ Les fonctions bel numero
*************************************************

// Retourner la somme des éléments (entiers) d'un tableau


int somme_ele_tab(int T[], int n)
{
int s = 0;
int i;
for (i = 0; i < n; i++)
1
{
s += T[i];
}
return s;
}
***********************************************************************************
***********************************************************************************
*********************************
// Retourner la somme des éléments (entiers) d'un tableau (pointeur)
void somme_ele_tab_pte(int T[], int n, int *s)
{
int i;
for (i = 0; i < n; i++)
2
{
(*s) += T[i];
}
}
***********************************************************************************
***********************************************************************************
************************************
// Afficher une matrice
void affichage_matrice(int l, int c, int M[l][c])
3
{
for (int i = 0; i < l; i++)
{
for (int j = 0; j < c; j++)
{
printf("%d ", M[i][j]);
}
printf("\n");
}
}
***********************************************************************************
***********************************************************************************
****************************
// Afficher un tableau
void affichage_tableau(int n, int T[n])
{
4
for (int i = 0; i < n; i++)
{
printf("%d ", T[i]);
}
printf("\n");
}

***********************************************************************************
***********************************************************************************
*************************

// remplissage d'un tableau simple


5
void remplir(int t[],int n)
{
int i;
for(i=0; i<n; i++)
{
printf("donner t[%d]",i);
scanf("%d",&t[i]);
}

***********************************************************************************
***********************************************************************************
*************************
remplissage du tableau de structure
void remplir_famille(famille TF[], int nF){ // nF notlobha menou fyl main
6
int i;
for(i=0; i<nF; i++)
{
printf("donner le cin du chef de la famille %d \n",i+1);
scanf("%d",&TF[i].cin);
printf("donner le nombre des membres de la famille %d\n",i+1);
scanf("%d",&TF[i].membre);
printf("donner la delegation de la famille %d\n",i+1);
gets(TF[i].deleg);
}
}
***********************************************************************************
***********************************************************************************
****************************

//remplissage du tableau d'entiers avec pointeur


void remplir (int t[], int *pn){
int i;
7
printf("taille du tableau:\n");
scanf("%d",pn);
for(i=0;i<(*pn);i++){
printf("t[%d]",i);
scanf("%d",&t[i]);
}
}

***********************************************************************************
***********************************************************************************
******************************
// Remplir un tableau d'entier dans l'ordre croissant
void Remplir_croissant(int T[], int n)
{
printf("T[0] = ");
8
scanf("%d", &T[0]);
for (int i = 1; i < n; i++)
{
do
{
printf("T[%d] = ", i);
scanf("%d", &T[i]);
} while (T[i] < T[i - 1]);
}
}
***********************************************************************************
***********************************************************************************
*********************************
// Remplir un tableau par des éléments distincts
void Remplir_distinct(int T[], int n)
{
9
bool existe;
printf("T[0] = ");
scanf("%d", &T[0]);
for (int i = 1; i < n; i++)
{
do
{
existe = false;
printf("T[%d] = ", i);
scanf("%d", &T[i]);
for (int j = 0; j < i; j++)
{
if (T[j] == T[i])
{
existe = true;
}
}
} while (existe == true);
}
}
***********************************************************************************
***********************************************************************************
********************************
// Tri par Séléction
void Tri_Selection(int T[], int n)
{
10
int i, j, min, tampon;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i + 1; j < n; j++) // Recherche du minimum en commençant à partir
de l'indice i+1
{
if (T[j] < T[min])
min = j;
}
tampon = T[i];
T[i] = T[min];
T[min] = tampon;
}
}
***********************************************************************************
***********************************************************************************
*************************************
// Tri à Bulles
void Tri_Bulles(int T[], int n)
{
int i, aux;
11
bool permut;
do
{
permut = false; // initialement pas de permutation
for (i = 0; i < n - 1; i++)
{
if (T[i] > T[i + 1])
{ // permutation
aux = T[i];
T[i] = T[i + 1];
T[i + 1] = aux;
permut = true; // on a fait une permutation
}
}
} while (permut == true);
}
***********************************************************************************
***********************************************************************************
************************
// Tri par insertion
void Tri_Insertion(int T[], int n)
{
12
int i, j, tmp;
for (i = 1; i < n; i++)
{
tmp = T[i]; // mémoriser T[i] dans une variable tmp
j = i;
/*décaler vers la droite les éléments de T[0]..T[i-1] qui sont plus grands
que tmp en partant de T[i-1]*/
while (j > 0 && (T[j - 1] > tmp))
{
T[j] = T[j - 1];
j--;
}
T[j] = tmp;
}
}
***********************************************************************************
***********************************************************************************
********************************************
// Recherche Dichotomique (retourne l'indice sinon -1)
int Recherche_Dichotomique(int T[], int n, int x)
{
bool trouve;
13
int debut, fin, milieu;
trouve = false;
debut = 0;
fin = n - 1;
while ((trouve == false) && (debut <= fin))
{
milieu = (debut + fin) / 2;
if (T[milieu] > x)
fin = milieu - 1;
else if (T[milieu] < x)
debut = milieu + 1;
else
trouve = true;
}
if (trouve)
{
return milieu;
}
else
{
return -1;
}
}
***********************************************************************************
***********************************************************************************
*********************************************
// Recherche Séquentielle (retourne l'indice sinon -1)
int Recherche_Sequentielle(int T[], int n, int x)
{
int i;
14
bool trouve;
trouve = false;
i = 0;
while (i <= n && trouve == false)
{
if (T[i] == x)
{
trouve = true;
}
i++;
}
if (trouve == true)
{
return (i - 1);
}
else
{
return -1;
}
}
***********************************************************************************
***********************************************************************************
*************************
// Suppression d'un élément x dans un tableau T de n éléments
void Supprimer(int T[], int n, int x)
{
int p = Recherche_Sequentielle(T, n, x);
if (p != -1)
{
15
for (int i = p; i < n; i++)
{
T[i] = T[i + 1];
}
n--;
}
}
***********************************************************************************
***********************************************************************************
*****************************
// Inversion des éléments d'un tableau
void Inverser(int T[], int n)
{
int j = n - 1, i, aux;
for (i = 0; i < n / 2; i++)
16
{
aux = T[i];
T[i] = T[j];
T[j] = aux;
j--;
}
}
***********************************************************************************
***********************************************************************************
********************************
// Elimination de toutes les occurences de x dans un tableau T (pointeurs)
void Eliminer(int T[], int *n, int x)
{
int i;
17
i = 0;
do
{
if (T[i] == x)
{
for (int j = i; j < (*n) - 1; j++)
{
T[j] = T[j + 1];
}
T[(*n) - 1] = 0;
(*n)--;
}
else
{
i++;
}
} while (i != *n);
}
***********************************************************************************
***********************************************************************************
*********************************
//affichage d'un tableau de structure
void afficher_livres(Livre t_livres[], int nl){
18
int i;
for(i=0;i<nl;i++){
printf("livre num %d: \n code:%d\n nbr d'exemplaire:%d\
n",i+1,t_livres[i].code,t_livres[i].nb_exemp);
}
}
//affichage d'un tableau de structure fy wostou tableau e5er

void afficher(gateau t[],int n)


{
int i,j;
for(i=0;i<n;i++)
{
printf("\nType: %d, Nb ingredient: %d", t[i].type, t[i].nb);
for(j=0;j<t[i].nb ;j++)
{
printf("\nDesignation:%s, Qte: %d, Prix: %.2f ", t[i].t[j].des,
t[i].t[j].qte,t[i].t[j].prix );
}
}
}
***********************************************************************************
***************************************************************************
//calcul du moyenne d'un tab
float moyenne (int t[],int n)
19
{
int i,s=0;
for(i=0; i<n; i++)
s+=t[i];
return (float)s/n;
}
***********************************************************************************
***************************************************************************
// calcul du pourcentage dans un tab
//just nbadel lcond 7asb lcond te3 ex
float pourcentage (int t[],int n)
{
20
int i,s=0;
for(i=0; i<n; i++)
{
if(n7ot lcond elly 7ajty byha)
{
s=s+1;

}
return (float)(s/n)*100;
}
***********************************************************************************
***************************************************************************
//calcul de variance dans un tab
float variance (int t[],int n)
21
{
int i;
float s=0;
float x_bar =moyenne(t,n);
for(i=0; i<=n-1; i++)
{
s+=pow(t[i]-x_bar,2);
}
return s/n;
}

***********************************************************************************
***************************************************************************
// ajout d'une valeur a la fin du tab
int ajouter(int t[],int n,int val)
22
{
t[n]=val;
n++;
return n;
}
***********************************************************************************
***************************************************************************
// ajout d'une valeur a la fin du tab ken mouch mawjouda
int ajouter_adherent (adherent t[],int na){
23
adherent a;
int v;

a=saisir_ad(); // na3mel saisie te3ou


v=verifier(t,na,a.id) ; // nchoufou mawjoud wela lee
if(v==0){ // ken mouch mawjoud nzydou
t[na]=a;
na++;
}

return na;
}

***********************************************************************************
***************************************************************************
//insertion d'une valeur a son indice donne

int insertion(int t[], int n, int val, int ind)


24
{
int i;
if(ind>n)
printf("indice errone");
else
{
for(i=n; i>ind; i--)
t[i]=t[i-1];
t[ind]=val;
n++;
}
return n;
}
***********************************************************************************
***************

// tri du tab
void trier (int t[], int n)
{
int p,i,aux;
do
{

p=0;
for(i=0;i<n-1;i++)
{
if(t[i]>t[i+1])
{
aux=t[i];
t[i]=t[i+1];
t[i+1]=aux;
p=1;
}
}
}while(p==1);
}
***********************************************************************************
***************************************************************************
Rq: ken bch na3mel saisie te3 7aja de type structure hedha exemple a suivre:
adherent saisir_ad (){ //ma3tany chy comme paramètres lezem ndeclary variable
fyl fonction adherent a;
printf("donner le nom de l'adherent\n");
fflush(stdin);
gets(a.nom);
25
printf("donner le prenom de l'adherent\n");
fflush(stdin);
gets(a.prenom);
printf("donner l'identifiant de l'adherent\n");
scanf("%d",&a.id);
printf("donner le plafond de remboursement de l'adherent\n");
fflush(stdin);
scanf("%f",&a.pr);
return a;
}// ou bien
void saisir_livre(Livre* p_livre){
printf("code:\n");
scanf("%d",&(*p_livre).code);
printf("nbr d'exemplaire:\n");
scanf("%d",&(*p_livre).nb_exemp);

}
***********************************************************************************
***************************************************************************
// verification te3 id fy wost tableau wela 2ay 7aja o5ra 7asb lcond w nraja3 lpos
te3ha ken mawjouda si nn nraja3 -1
int verifier(adherent t[],int na,int id){
int i;
while( (i<na) && t[i].id != id)){
i++;
}
26
if (i<na){
test=i;
}else{
test=-1;
}
return test;

}
***********************************************************************************
***************************************************************************
//verification w nraja3 1 ken mawjouda w 0 ken mouch mawjouda
int verifier(adherent t[],int na,int id){
int i=0,test=0;
while( (i<na) && (test==0)){ 27
if(t[i].id==id){
test=1;
}else{
i++;
}
}
return test;

***********************************************************************************
***************************************************************************

//verification te3 7aja mawjouda wela lee bil pointeur exp:


void chercher_livre(Livre t_livres[], int nl, int code_l, int* pos){ 28
int i;
while( (i<nl) && (t_livres[i].code!=code_l)){
i++;
}
if(i<nl){
(*pos)=i;
}else{
(*pos)=-1;
}

}
***********************************************************************************
***************************************************************************
// fonction permuter deux entiers
void permuter(int * px, int *py)
{
29
int aux;
aux=(*px);
(*px)=(*py);
(*py)=aux;
}
***********************************************************************************
***************************************************************************
//modifier une valeur dans le tab
void modifier( int t[], int n, int val, int nouv) 30
{
int pos;
chercher( t,n,val, &pos); // bch nlawej 3al valeur elli bch
nmodyfyha ken mal9ythech nraja3 erreur si nn nbadalha bil valeur nouv
if(pos==-1)
printf("erreur");
else
t[pos]=nouv;
}

***********************************************************************************
***************************************************************************
//modifier une valeur dans le tab kol wyn nel9aha nbadelha
void modifierTT( int t[], int n, int val, int nouv) 31
{
int i;
for(i=0; i<n; i++)
{
if(t[i]==val)
t[i]=nouv;
}
}

***********************************************************************************
***************************************************************************
// supprimer la premiere occurence te3 val fy tab
void supprimer(int t[], int* pn, int val) 32
{
int pos,i;
chercher( t,*pn,val, &pos); //nlawej 3lyha mawjouda wela lee
if(pos==-1)
printf("erreur");
else
for(i=pos; i<(*pn)-1; i++)
t[i]=t[i+1];

(*pn)--;
}

***********************************************************************************
***************************************************************************
// supprimer toutes les occurences te3 val fy tab
void supprimerTT(int t[], int* pn, int val) 33
{
int i,j;
for(i=0; i<*pn; i++)
{
if(t[i]==val)
{
for(j=i; j<(*pn)-1; j++)
t[j]=t[j+1];

(*pn)--;
i--;
}

}
}

***********************************************************************************
***************************************************************************
******fonction avec les chaines*******

34

// fonction qui cherche un caractere dans une chaine et rend la pos de sa dernière
occurance ken mawjoud w -1 si nn
int search_c (char ch[],char c){
int i,ind=0;
int x=strlen(ch);

for(i=0;i<x;i++){
if(ch[i]==c){
ind=i;
}else{
ind=-1;
}
}
return ind;

}
***********************************************************************************
***************************************************************************
// fonction qui determine les pos de toutes les occ d'un caractere dans une une
chaine w n7othom fy tab
int search_all (char ch[],char c, int t[]){
int i ,j=0;
int x= strlen(ch); 35
for(i=0;i<x;i++){
if(ch[i]==c){
t[j]=i;
j++;
}
}
return j;
}
***********************************************************************************
***************************************************************************
// fonction qui supprime toutes les occ d'un caractere dans une une chaine
void supprimer(char ch[],char c){
int j,i; 36
int x=strlen(ch);
for(i=0;i<x;i++){
if(ch[i]==c){
for(j=i;j<x-1;j++){
ch[j]=ch[j+1];
}
i--;
x--;
}
}
ch[x]='\0';
}
***********************************************************************************
***************************************************************************
// fonction qui determine le nbre d'occurence d'un caractere dans une chaine
int nb_occ (char ch[],char c){
int nb=0,i;
int x=strlen(ch); 37
for(i=0;i<x;i++){
if(ch[i]==c){
nb=nb+1;
}
}
return nb;
}
***********************************************************************************
***************************************************************************
//permuter deux chaines
void permuterchaine(char ch1[], char * ch2) 38
{
char aux[20];
strcpy(aux, ch1);
strcpy(ch1, ch2);
strcpy(ch2, aux);
}

***********************************************************************************
***************************************************************************
// initialiser 7aja mil matrice à 0

void initialiser (constat m[3][5]){


int i,j; 39
for(i=0;i<3;i++){
for(j=0;j<5;j++){
m[i][j].refer=0;
}
}
}

// remarque : fyl matrice lezem na3tyh dyma nbre de colonne 9adech w nbre de ligne
mouch obligatoire

***********************************************************************************
***************************************************************************

// fonction modifier etat


40
void modifier_etat_panne (int ref, machine Tm[], int n) {
int i,pos;
pos=rechercherMach ( Tm ,n, ref);
if(pos==-1){
printf("machine introuveable\n");
}else{
for(i=0;i<10;i++){
if( Tm[pos].p[i].etat==1){
Tm[pos].p[i].etat=0;
}else
if(Tm[pos].p[i].etat==0){
Tm[pos].p[i].etat=1;
}
}
}
}
***********************************************************************************
***************************************************************************
// fonction supprimer à une position donnée
void supprimer (famille TF[], int *nF, int pos){ 41
int i,j;
for(i=pos; i<(*nF) ; i++){

TF[i]=TF[i+1];

}
(*nF)--;
i--;
}

***********************************************************************************
***************************************************************************
// recherche de min et max avec pointeur et sans pointeur

void min_max (etudiant t[],int n,int *pmin,int *pmax){ 42


int i ;
*pmin=0;
*pmax=0;
for(i=1;i<n;i++){
if(t[i].age<t[*pmin].age){
*pmin=i;
}else
if(t[i].age>t[*pmax].age){
*pmax=i;
}
}
}
***********************************************************************************
***************************************************************************
// fonction recherche max
int max_membre(famille TF[], int nF){ 43
int max=0 , i;
for(i=1;i<nF;i++){
if(TF[i].membre>TF[max].membre){
max=i;
}
}
return max;
}

***********************************************************************************
***************************************************************************
saisir tableau matrice et affichage
void saisir_matrice(int mat[3][4]){ 44
int i,j;
for(i=0;i<3;i++){
for (j = 0; j < 4; j++)
{
printf("donner mat[%d][%d]",i,j);
scanf("%d",&mat[i][j]);
}

}
// affichage
void affichage_matrice(int mat[3][4]){ version nour
int i,j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("%d \t",mat[i][j] );
}
printf("\n");
}
}

***********************************************************************************
***************************************************************************
// pour initialiser une chaine a vide
ch[0]='\0 ';
45

***********************************************************************************
***************************************************************************
// pour saisir un tableau dont les élément sont dans l'ordre croissant ( saisi
mellouwel tkoun mnadhma lezemha)

void Remplir_croissant(int T[], int n) ///////////////////// 2 version nour


{
int i;
do{
printf("T[0] = ");
scanf("%d", &T[0]);
}while(T[0]<0); 46

for ( i = 1; i < n; i++)


{
do
{
printf("T[%d] = ", i);
scanf("%d", &T[i]);
} while (T[i] < T[i - 1]);
}
}

***********************************************************************************
*********************************************************************************
//// pour ajouter un élément en c:
i=n;
while(i>0 && i>pos){ 47
t[i]=t[i-1]
i--
}
t[pos]=valeur // valeur elly bch yda5elha l'inernaute
n++;
// dyma fyl ajout na3mel décalage à droite

ky yebda tableau fere8 wana bch na3mellou remplissage bil case bil case
n=0;
scanf("%d",&t[n]);
n++;
// nb: ay element nzydou yetzed fy position n 5ater tableau yemchy m 0 ll n-1

Vous aimerez peut-être aussi