slt les amis , pouvez vous m'aider j'ai un probleme avec mon programme; je doit creer une fonction nommee Trie_selection qui prend un tableau quelconque le trie et affiche le resultat
mais mon programme ne marche pas.
voila mon code source1 :
code source 2:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 #include<stdio.h> #include<stdlib.h> void Trie_selection(float *ptr, int Taille) { float *tab; int i,j,max; //double tm; tab=(float *)malloc(Taille*sizeof(float)); if (tab==NULL) { printf(" erreur d'allocation\n"); exit(EXIT_FAILURE); } for (i = 0;i< Taille-1;i++) { max = i; for (j=i+1;j<Taille;j++) { if (*(ptr+i) < *(ptr+j)) { max=j; *(ptr+i) = *(ptr+i)+*(ptr+max); *(ptr+max)= *(ptr+i)-*(ptr+max); *(ptr+i)= *(ptr+i)-*(ptr+max); } } for (i=0;i<Taille;i++) { printf("tab[%d]=%f\n",i+1,*(ptr+i)); } } } int main() { float *tab; int N,i; printf("entrez taille"); scanf("%d", &N); for (i=0;i<N;i++) { printf("tab[%d]=\n",i); scanf("%f",tab+i); } Trie_selection(&tab,N); free(tab); return 0; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 #include <stdio.h> #include <stdlib.h> void Trie_selection(int *ptr, int Taille) { int *tab; int i,j,max; //int tm; tab=(int*)malloc(Taille*sizeof(int)); if (tab==NULL) { printf(" erreur d'allocation\n"); return EXIT_FAILURE; } //ptr = tab; for (i = 0;i< Taille-1;i++) { max = i; for (j=i+1;j<Taille;j++) { if (*(ptr+i)< *(ptr+j)) { max=j; *(ptr+i) = *(ptr+i)+*(ptr+max); *(ptr+max)= *(ptr+i)-*(ptr+max); *(ptr+i)= *(ptr+i)-*(ptr+max); } /*tm= *(ptr+i); *(ptr+i) = *(ptr+max); *(ptr+max)= tm;*/ } for (i=0;i<Taille;i++) { printf("tab[%d]=%d \n",i+1,*(ptr+i)); } } // free(tab); } int main() { int i,N; int *tab; printf("taille du tableau :\n"); scanf("%d",&N); tab=(int*)malloc(N*sizeof(int)); if (tab==NULL) { printf(" erreur d'allocation\n"); return EXIT_FAILURE; } for (i=0;i<N;i++) { printf(" tab[%d]:\n",i); scanf("%d",tab+i); } printf("tableau non trie\n"); for (i=0;i<N;i++) { printf("tab[%d]:%d \n",i,*(tab+i)); } printf("tableau trie\n"); Trie_selection(tab,N); free(tab); return 0; }
Partager