Un probleme de logique dans le tri d'un tab

barca until death Messages postés 12 Statut Membre -  
barca until death Messages postés 12 Statut Membre -
Bonjour,

svp vous pouvez m'aider??
j'ai realisé un tp d'un tableau ce tp contient un menu avec (Initailisation du tableau -Affichage du table-Calcul minimum du table-Calcul maximum du table-Calcul maximum du table -Calcul la somme du table -Calcul la moyenne du table -Le triage du table- Le nbr des elements -Recherche)
voila le code en langage c mais le tri du tableau ca marche pas je n'ai pas su ou est le probleme

#include<stdio.h>
int v,n,i,num;
//*********************INITIALISATION DU TABLEAU****************;
INITIALISATION(int T[n]){
printf("\n\tEntrer la Taille du Tableau :\t");
scanf("%d",&n);
printf("\n\tEntrer les elements du tableau :\n");
for(i=0;i<n;i++){
printf("\tt[%d]=",i);
scanf("%d",&T[i]);
}}

//*************************AFFICHAGE DU TABLEAU**************;
AFFICHAGE(int T[n]){
printf("\nvoila le tableau que vous avez saisie:\t");
for(i=0;i<n;i++){
printf("|%d",T[i]);
}
printf("\n\n\si vous voulez retourner au MENU appuyer sur une Touche");
getch();
system("cls");
}

//************************MAXIMUM DU TABLEAU****************;
int MAX(int T[n]){
int max=T[0];
for(i=0;i<n;i++){
if(T[i]>max){
max=T[i];
}}
return max;
}

//************************MINIMUM DU TABLEAU******************;
int MIN(int T[n]){
int min=T[0];
for(i=0;i<n;i++){
if(T[i]<min){
min=T[i];
}}
//printf ("le min est %d",min);
return min;

}

//**************SOMME DES ELEMENTS DU TABLEAU*****************;
SOMME(int T[n]){
int som=0;
for(i=0;i<n;i++){
som=som+T[i];
}
printf("\nla somme des elements du tableau est:\t%d\n",som);
printf("\n\si vous voulez retourner au MENU appuyer sur une Touche");
getch();
system("cls");
}

//************************MOYENNE DU TABLEAU******************;
float MOYENNE(int T[n]){
int som=0;
float m;
for(i=0;i<n;i++){
som=som+T[i];
}
m=(float)som/n;
return m;
}

//***********************TRIAGE DU TABLEAU********************;
TRIAGE(int T[n]){
int y,x;
do {
x=0;
for(i=0;i<n;i++){
if(T[i]>T[i+1]){
x=x+1;
y=T[i];
T[i]=T[i+1];
T[i+1]=y;
}
printf("\n\t t[%d]=%d",i,T[i]);
}
}while(x!=0);
printf("\n\si vous voulez retourner au MENU appuyer sur une Touche");
getch();
system("cls");
}

//****NOMBRE DES ELEMENTS SUPPERIUR STRECTEMENTS AU MOYNNE********;
NBRSUPMOY(int T[n]){
int k=0,som=0;
float m;
for(i=0;i<n;i++){
som=som+T[i];
}
m=(float)som/n;
for(i=0;i<n;i++){
if(T[i]>m)
k=k+1;
}
printf("\nle nombre des elts qui ont sup strectement a la moy est:\t%d\n",k);
printf("\n\si vous voulez retourner au MENU appuyer sur une Touche");
getch();
system("cls");
}
//***********************recherche********************;

main(){
int i,choix,r,min;
int t[n];
do{
printf("\n");
printf("\t ===============<<< PROJET Du Tableau >>>=============== \n\n\n");
printf("\t ******** \n");
printf("\t|* **************************| MENU |****************************** *|\n");
printf("\t|* *|\n");
printf("\t|* 1 ==> Initailisation du tableau *|\n");
printf("\t|* 2 ==> Affichage du table *|\n");
printf("\t|* 3 ==> Calcul minimum du table *|\n");
printf("\t|* 4 ==> Calcul maximum du table *|\n");
printf("\t|* 5 ==> Calcul la somme du table *|\n");
printf("\t|* 6 ==> Calcul la moyenne du table *|\n");
printf("\t|* 7 ==> Le triage du table *|\n");
printf("\t|* 8 ==> Le nbr des elements *|\n");
printf("\t|* 9 ==> Recherche *|\n");
printf("\t|* **************************************************************** *|\n");
printf("\t==>Entrer votre choix:\t");
scanf("%d",&choix);
system("cls");
switch (choix){
case 1:
INITIALISATION(t);
case 2:
AFFICHAGE(t);
break;
case 3:

printf("\n\nle minimun du tableau est:\t%d",MIN(t));
printf("\n\n\nappuyer sur une touche pour retourne au MENU");
getch();
system("cls");
break;
case 4:
printf("\n\nle maximun du tableau est:\t%d",MAX(t));
printf("\n\n\nappuyer sur une touche pour retourne au MENU");
getch();
system("cls");
break;
case 5:
SOMME(t);
break;
case 6:
printf("\nla moyenne du tableau est:\t%.2f\n",MOYENNE(t));
printf("\n\n\nappuyer sur une touche pour retourne au MENU");
getch();
system("cls");
break;
case 7:
TRIAGE(t);
break;
case 8:
NBRSUPMOY(t);
case 9:
printf("\n \n tapez num da case: ");
scanf("%d",&num);
for(i=0;i<4;i++)
{
if(num==t[i])
{
printf("\n t[%d]==%d",i,t[i]);
v=1;
}
}
if(v==0){printf("introuvable!!");}
getch();break;
}}while(choix!=9);
}

merci d'avance !!!

1 réponse

yoda
 
Bonjour,

voila la correction
//***********************TRIAGE DU TABLEAU********************; 
TRIAGE(int T[n]){ 
int y,x; 
do { 
   x=0; 
   for(i=0;i<n-1;i++){ 
      if(T[i]>T[i+1]){ 
         x=x+1; 
         y=T[i]; 
         T[i]=T[i+1]; 
         T[i+1]=y; 
        } 
	}
}while(x!=0);
for(i=0;i<n;i++){
    printf("\n\t t[%d]=%d",i,T[i]); 
} 
printf("\n\si vous voulez retourner au MENU appuyer sur une Touche"); 
getch(); 
system("cls"); 
} 
//**************************************************************; 
1
barca until death Messages postés 12 Statut Membre
 
merci bcp yoda :)
0