Tri de tableaux
DAD
-
Mahmah Messages postés 497 Statut Membre -
Mahmah Messages postés 497 Statut Membre -
Bonjour, on nous a de mander d ecrire un programme pour trier un tableau par ordre croissant on utilison une methde basse sur les indices. voila mon programme mais il m affiche une erreue de segmentetion.
SOS
VOILA MON PROGRAMME:#include<stdio.h>
/************************************************code complet*****************************************************************************/
//decalration du type struct
struct meteo
{
//On identifie chaque "type meteo" par son temps min, son temps max, son hygrometrie et la hauteur de sa pluie.
float tempmin;
float tempmax;
int hygro;
float pluis;
};
/*la saisie du tableau releve meteo*/
void saisie (int n ,struct meteo t[])
{
//n taille de t param non modifie .
//ne retourne rien .
//t est un tableau constitué d element de type structe.
printf("donnez la taille de votre tableau\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%f",&t[i].tempmin);
scanf("%f",&t[i].tempmax);
scanf("%d",&t[i].hygro);
scanf("%f",&t[i].pluis);
}
}
/*l affichge du tableau releve meteo*/
void affiche(int n ,struct meteo t[])
{
//ne retourne rien.
printf("la taille du tableau:%d\n",n);
for(int i=0;i<n;i++)
{
printf("%f\n",t[i].tempmin);
printf("%f\n",t[i].tempmax);
printf("%d\n",t[i].hygro);
printf("%f\n",t[i].pluis);
}
}
/*******************************************************************main************************************************************************************/
int main()
{
int n; /*nombre de valeurs à trier*/
float tin[n]; /*le tableau retourne*/
int entiere ; /*la valeur entiiere à retournee*/
struct meteo t[n]; /*le tableau saisie par l utilisateur*/
saisie(n,t);
affiche(n,t);
int entree = 0;
int i,
j,
b;
int entier = 0;
int a;
int suivant[n];
suivant[0] = -1;
for(i=1; i<n ; i++)
{
if(t[i].tempmin < t[entier].tempmin)
{
tin[i] =entier;
entier = i;
}
else
{
j = entier;
while ( j != -1 && t[j].tempmin > t[i].tempmin )
{
int i;
int j;
int param;
if(tin[j]==i)
{ param = j;
j = tin[j];
}
if(j=-1)
{
tin[i] = -1;
tin[param] = i;
}
else
{
tin[param] = i;
tin[i] = j;
}
}
int i=0;
while(i=!-1)
{ printf("%f\n",&t[i].tempmin);
j=tin[j];
}
}
}
return 0;
}
SOS
VOILA MON PROGRAMME:#include<stdio.h>
/************************************************code complet*****************************************************************************/
//decalration du type struct
struct meteo
{
//On identifie chaque "type meteo" par son temps min, son temps max, son hygrometrie et la hauteur de sa pluie.
float tempmin;
float tempmax;
int hygro;
float pluis;
};
/*la saisie du tableau releve meteo*/
void saisie (int n ,struct meteo t[])
{
//n taille de t param non modifie .
//ne retourne rien .
//t est un tableau constitué d element de type structe.
printf("donnez la taille de votre tableau\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%f",&t[i].tempmin);
scanf("%f",&t[i].tempmax);
scanf("%d",&t[i].hygro);
scanf("%f",&t[i].pluis);
}
}
/*l affichge du tableau releve meteo*/
void affiche(int n ,struct meteo t[])
{
//ne retourne rien.
printf("la taille du tableau:%d\n",n);
for(int i=0;i<n;i++)
{
printf("%f\n",t[i].tempmin);
printf("%f\n",t[i].tempmax);
printf("%d\n",t[i].hygro);
printf("%f\n",t[i].pluis);
}
}
/*******************************************************************main************************************************************************************/
int main()
{
int n; /*nombre de valeurs à trier*/
float tin[n]; /*le tableau retourne*/
int entiere ; /*la valeur entiiere à retournee*/
struct meteo t[n]; /*le tableau saisie par l utilisateur*/
saisie(n,t);
affiche(n,t);
int entree = 0;
int i,
j,
b;
int entier = 0;
int a;
int suivant[n];
suivant[0] = -1;
for(i=1; i<n ; i++)
{
if(t[i].tempmin < t[entier].tempmin)
{
tin[i] =entier;
entier = i;
}
else
{
j = entier;
while ( j != -1 && t[j].tempmin > t[i].tempmin )
{
int i;
int j;
int param;
if(tin[j]==i)
{ param = j;
j = tin[j];
}
if(j=-1)
{
tin[i] = -1;
tin[param] = i;
}
else
{
tin[param] = i;
tin[i] = j;
}
}
int i=0;
while(i=!-1)
{ printf("%f\n",&t[i].tempmin);
j=tin[j];
}
}
}
return 0;
}
A voir également:
- Tri de tableaux
- Comment faire un tri personnalisé sur excel - Guide
- Logiciel tri photo - Guide
- Tableaux croisés dynamiques - Guide
- Fusionner deux tableaux excel - Guide
- Les tableaux word - Guide
2 réponses
Bonjour,
Sans appel.
Compiling...
main.cpp
main.cpp(65) : error C2057: expected constant expression
main.cpp(65) : error C2466: cannot allocate an array of constant size 0
main.cpp(65) : error C2133: 'tin' : unknown size
main.cpp(67) : error C2057: expected constant expression
main.cpp(67) : error C2466: cannot allocate an array of constant size 0
main.cpp(67) : error C2133: 't' : unknown size
main.cpp(78) : error C2057: expected constant expression
main.cpp(78) : error C2466: cannot allocate an array of constant size 0
main.cpp(78) : error C2133: 'suivant' : unknown size
main.cpp(85) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(105) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
main.cpp(115) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(120) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(121) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(129) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
Les trois premières qui sont au début du main sont déjà suffisantes pour éviter un joli seg fault.
Essaie d'activer un peu plus de retours de la part de ton compilateur, ça peut aider...
M.
Sans appel.
Compiling...
main.cpp
main.cpp(65) : error C2057: expected constant expression
main.cpp(65) : error C2466: cannot allocate an array of constant size 0
main.cpp(65) : error C2133: 'tin' : unknown size
main.cpp(67) : error C2057: expected constant expression
main.cpp(67) : error C2466: cannot allocate an array of constant size 0
main.cpp(67) : error C2133: 't' : unknown size
main.cpp(78) : error C2057: expected constant expression
main.cpp(78) : error C2466: cannot allocate an array of constant size 0
main.cpp(78) : error C2133: 'suivant' : unknown size
main.cpp(85) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(105) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
main.cpp(115) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(120) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(121) : warning C4244: '=' : conversion from 'int' to 'float', possible loss of data
main.cpp(129) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data
---------------------- Done ----------------------
Build: 0 succeeded, 1 failed, 0 skipped
Les trois premières qui sont au début du main sont déjà suffisantes pour éviter un joli seg fault.
Essaie d'activer un peu plus de retours de la part de ton compilateur, ça peut aider...
M.
Correction de tes 2 méthodes :
Le début de ton main :
struct meteo * saisie (int *n)
{
int i, nb;
struct meteo *t;
printf("donnez la taille de votre tableau\n");
scanf("%d",&nb);
t = (struct meteo *)calloc(nb, sizeof(struct meteo));
for(i=0;i<nb;i++)
{
scanf("%f",&t[i].tempmin);
scanf("%f",&t[i].tempmax);
scanf("%d",&t[i].hygro);
scanf("%f",&t[i].pluis);
}
*n = nb;
return t;
}
void affiche(int n ,struct meteo *t)
{
int i;
printf("la taille du tableau:%d\n",n);
for(i=0;i<n;i++)
{
printf("%f\n",t[i].tempmin);
printf("%f\n",t[i].tempmax);
printf("%d\n",t[i].hygro);
printf("%f\n",t[i].pluis);
}
}
Le début de ton main :
int main()
{
int n; /*nombre de valeurs à trier*/
float *tin; /*le tableau retourne*/
int entiere ; /*la valeur entiiere à retournee*/
struct meteo *tab; /*le tableau saisie par l utilisateur*/
tab = saisie(&n);
affiche(n,tab);
}
j ai pas tres bien compri ou etait l erreure .
int main() { int n; /*nombre de valeurs à trier*/ float tin[n]; /*le tableau retourne*/Le compilateur, le mien en tout cas, veut savoir la taille du tableau qu'il doit mettre dans sa pile. Tous les compilos ne sont pas forcément aussi tatasses mais ici l'erreur semble là.
int main() { const int n = 22; /*nombre de valeurs à trier*/ float tin[n]; /*le tableau retourne*/lui irait par exemple.
int main() { int n; /*nombre de valeurs à trier*/ float *tin = NULL; /*le tableau retourne*/ ... n = 22; tin = (float*) malloc( n * sizeof( float ) ); ... free ( tin );irait aussi.
M.