[c] tri
prob
-
lami20j Messages postés 21331 Date d'inscription Statut Modérateur, Contributeur sécurité Dernière intervention -
lami20j Messages postés 21331 Date d'inscription Statut Modérateur, Contributeur sécurité Dernière intervention -
Bonjour,
mon programme demande une série de 20 chiffres maximum et qui les trie en fonction du tri choisi!
mes tri ne fonctionne pas!! on peut m'aider?
merci
mon programme demande une série de 20 chiffres maximum et qui les trie en fonction du tri choisi!
mes tri ne fonctionne pas!! on peut m'aider?
merci
#include <stdio.h> #include <stdlib.h> #define MAX 20 void main (void) { int v[MAX],cpt,i,j,limite,tri,n,temp,chgt,tps; printf("entrez la limite de nombre a introduire\t"); scanf("%d",&limite); for(cpt=0;cpt<limite;cpt++) { printf("Introduire le %d e element du vecteur :\t", cpt+1); scanf("%d",&v[cpt]); } cpt=limite; printf("voici les tris possibles dans ce programme\n\t"); printf("tri par extraction (1)\n\t"); printf("tri par insertion (2)\n\t"); printf("tri bulle (3)\n\t"); do{ printf("\nchoisissez votre tri\n"); scanf("%d",&tri); }while (( tri < 1) || ( tri > 3)); switch( tri ) { case 1: printf("vous avez choisi le tri par extraction\n"); n=0; i=0; while(i<n) { j=i+1; while(j<= n+1) { if(v[i] > v[j]) { v[i]=v[j]; } j=j+1; } i=i+1; } for(i=0;i<10;i++) { printf("%d\t",v[i]); } break; case 2: printf(" vous avez choisi le tri par insertion\n"); i=1; while(i<10) { temp=v[i]; j=i-1; while(j>=0&&v[j]>temp) { v[j+1]=v[j]; j=j-1; } v[j+1]=temp; i++; } for(i=0;i<10;i++) { printf("%d\t",v[i]); } break; case 3: printf("vous avez choisi le tri bulle"); for(i=0;i<limite;i++) while(chgt) { chgt=0; for(i=0;i<(limite-1);i++) if(v[cpt]>v[i+1]) { tps=v[cpt]; v[cpt]=v[cpt+1]; v[cpt+1]=tps; chgt=1; } } for(i=0;i<limite;i++) printf("%d",v[cpt]); break; default: printf("numero invalide choississez un tri de 1 a 3\n\t"); } }
A voir également:
- [c] tri
- Comment faire un tri personnalisé sur excel - Guide
- Logiciel tri photo - Guide
- Tri turf - Télécharger - Sport
- Votre colis est retenu au centre de tri - Accueil - Arnaque
- Wap tri - Télécharger - Divers TV & Vidéo
2 réponses
Salut,
mieux vaux de structurer ton programme :
fonction pour menu
fonction pour affichage
fonction pour tri_extraction
fonction pour tri_insertion
fonction pour tri à bulle
voici le code (à toi d'adapter )
mieux vaux de structurer ton programme :
fonction pour menu
fonction pour affichage
fonction pour tri_extraction
fonction pour tri_insertion
fonction pour tri à bulle
voici le code (à toi d'adapter )
#include<stdio.h> #define TAILLE 5 #define VRAI 1 #define FAUX 0 void tri_extraction (int *v); void tri_insertion (int *v); void tri_bulle (int *v); int menu (); void affiche (int *v); int main () { int v[TAILLE]; int i, choix; for (i = 0; i < TAILLE; ++i){ printf ("Entrez l'élément %d : ", i + 1); scanf ("%d", &v[i]); } printf("Le tableau initial : "); affiche (v); do{ choix = menu (); switch (choix){ case 1: printf ("Tri par extraction\n"); tri_extraction (v); affiche (v); break; case 2: printf ("Tri insertion\n"); tri_insertion (v); affiche (v); break; case 3: printf ("Tri bulle\n"); tri_bulle (v); affiche (v); break; case 4: printf ("Au revoir!\n"); return 0; default: printf ("Option inconnue!\n"); break; } }while (choix > 0 && choix < 4); return 0; } /* affiche le tableau */ void affiche (int *v) { int i; for (i = 0; i < TAILLE; ++i) printf ("%d ", v[i]); printf ("\n"); } /* menu */ int menu () { int choix; printf ("********** MENU **********\n"); printf ("1. tri extraction\n"); printf ("2. tri insertion\n"); printf ("3. tri bulle\n"); printf ("4. Quitter\n"); printf ("\nChoisissez une option : "); scanf ("%d", &choix); return choix; } /* tri par extraction */ void tri_extraction (int *v) { int i, min, j, x; for (i = 0; i < TAILLE - 1; i++){ min = i; for (j = i + 1; j < TAILLE; j++) if (v[j] < v[min]) min = j; if (min != i){ x = v[i]; v[i] = v[min]; v[min] = x; } } } /* tri par insertion */ void tri_insertion (int *v) { int i, j, p, x; for (i = 1; i < TAILLE; i++){ x = v[i]; for (p = 0; v[p] < x; p++); for (j = i - 1; j >= p; j--) v[j + 1] = v[j]; v[p] = x; } } /* tri à bulle */ void tri_bulle (int *v) { int i, j, tmp; int etat = VRAI; i = j = tmp = 0; for (i = 0; (i < TAILLE) && etat; i++){ etat = FAUX; for (j = 1; j < TAILLE - i; j++){ if (v[j] < v[j - 1]){ tmp = v[j - 1]; v[j - 1] = v[j]; v[j] = tmp; etat = VRAI; } } } }
salut ,
J'ai fait un fichier trih.bat dont j'ai copié ton modèle sur bureau et voici le résultat :
J'espère que je vous aide .
A+
J'ai fait un fichier trih.bat dont j'ai copié ton modèle sur bureau et voici le résultat :
Microsoft Windows XP [version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Documents and Settings\HP\Bureau>TRIH.BAT La syntaxe de la commande est incorrecte. C:\Documents and Settings\HP\Bureau>#include <stdio.h> C:\Documents and Settings\HP\Bureau>EXIT
J'espère que je vous aide .
A+