probleme avec les pointeurs
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:
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 source 2:
Code:
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;
} |
probleme avec les pointeurs
Bonjour
Tu ne tiens pas compte des remarques que l'ont tes faites, brefs pour faire simple voici un code source que je poste en partant sur la base de quelques éléments de ton code sources tu pourras par la suite comparer au tien pour comprendre ce qui ne marche pas et en plus de ce qui a été mentioner comme remarque précédente, tu pouvais tout aussi bien utiliser les indices du tableau c'est-à-dire tab[i] au lieu de manipuler les pointeurs de la sorte ( mais c'est une question de choix) et ta variable "max" ne sert pas à grand-chose limite à elle ne sert à rien.
à bientôt :aie:
Code:
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
|
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
/***
* Fonction trie selectif
***/
void f_TrieSelection( int *pTab, unsigned int const iSize ){
unsigned int i = 0;
unsigned int j = 0;
unsigned int k = 0;
for( i = 0; i < iSize - 1 ; i++ ){
for( j = i+1; j < iSize; j++ ){
if( *(pTab+i) > *(pTab+j) ){
k = *(pTab+i);
*(pTab+i) = *(pTab+j);
*(pTab+j) = k;
}
}
}
}
/***
* Fonction principale
***/
int main( void ){
int *pTab = NULL;
pTab = (int*)calloc( 10, sizeof( int ) );
srand( time(NULL) );
unsigned int i = 0;
for( i = 0; i < 10; i++ ){
*(pTab+i) = rand()%20;
}
//Affichage du tableau non trier
for( i = 0; i < 10; i++ ){
printf(" %d ", *(pTab+i) );
}
printf("\n");
f_TrieSelection( pTab, 10 );
for( i = 0; i < 10; i++ ){
printf(" %d ", *(pTab+i) );
}
printf("\n");
free( pTab );
return ( 0 );
} |