IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

Problème de tri


Sujet :

C

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut Problème de tri
    Bonsoir,

    Quand je compile je n'ai pas d'erreur, seulement quand le programme s'execute il plante très rapidement, surement avant d'arriver dans le programme de tri. Je ne trouve pas l'erreur...

    Voici le code :

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    #include <stdio.h>
    #include <stdlib.h>
    #define SOMME_MAX 10
     
    void tri_maximum (float * tab, size_t size, int order);
     
    int main(void)
    {
        float c1,c2,resultat;
        int i,j;
        int lim=SOMME_MAX*100;
        int o = 0;
        int k = 0;
        float *t = malloc((SOMME_MAX*SOMME_MAX)*sizeof *t);
        FILE* data=fopen("resultats.txt","w");
        FILE* tri=fopen("tri.txt","w");
     
        printf("\nValeur de la cote 1 :");
        scanf("%f",&c1);
     
        printf("\nValeur de la cote 2 :");
        scanf("%f",&c2);
     
     
     
        for ( i=1 ; i<=lim ; i++)
            {
                  for ( j=1 ; j<=lim ; j++ )
                      {
                            resultat=( i*0.01*(c1-3)+j*0.01*(c2-3) );
     
                            if ( (resultat>0)&&(((i!=0)||(j!=0))&&((i!=0)&&(j!=0))) )
                               {
                                           fprintf(data,"\n| m1=%f | m2=%f | gain.E1=%f | gain.E2=%f | gain.E=%f |", (0.01*i), (0.01*j), ((0.01*i)*(c1-2)-(0.01*j)), ((0.01*j)*(c2-2)-(0.01*i)), ( ((0.01*i)*(c1-2)-(0.01*j))+((0.01*j)*(c2-2)-(0.01*i)) ));
     
                                           t[k]=( ((0.01*i)*(c1-2)-(0.01*j))+((0.01*j)*(c2-2)-(0.01*i)) );
                                           k++;
                               }
                       }
            }
     
     
     
       getchar();
     
     
       tri_maximum (t, k, 1);
     
       for (o = 0; o < k; o++)
       {
          printf("\n%f", t[o]);
          fprintf (tri,"\n%f", t[o]);
     
       }    
     
        fclose(data);
        fclose(tri);
     
    }
     
    void tri_maximum (float * tab, size_t size, int order)
    {
       size_t i = 0;
       size_t j = 0;
     
     
       if (tab != NULL && size > 1)
       {
          /* Parcours du tableau. */
          for (i = size - 1; i > 0; i--)
          {
             /* Recherche du maximum. */
             size_t max = i;
             for (j = 0; j <= i; j++)
             {
                if (order == 0)
                {
                   if (tab[j] > tab[max])
                   {
                      max = j;
                   }
                }
                else
                {
                   if (tab[j] < tab[max])
                   {
                      max = j;
                   }
                }
             }
     
             /* Echange des valeurs du tableau. */
             {
                float tmp = tab[i];
                tab[i] = tab[max];
                tab[max] = tmp;
             }
          }
       }
    }
    Si vous avez une meilleur idée que rien comme moi, je suis preneur !
    Merci d'avance

  2. #2
    Invité(e)
    Invité(e)
    Par défaut
    Bonjour,

    La fonction de tri a l'air de bien fonctionner, je pense que le problème vient du tableau t qui est vide :

    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
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    void tri_maximum(float *tab, size_t size, int order);
     
    int main(void)
    {
        srand(0);
        float tab[10] = { 0, 1, 4, 7, 2, 5, 8, 3, 6, 9 };
        size_t i;
     
        tri_maximum(tab, sizeof tab / sizeof *tab, 1);
     
        for (i = 0; i < sizeof tab / sizeof *tab; i++) {
            printf("\n%f", tab[i]);
        }
        return 0;
    }
     
     
     
    void tri_maximum(float *tab, size_t size, int order)
    {
        size_t i = 0;
        size_t j = 0;
     
        if (tab != NULL && size > 1) {
            /* Parcours du tableau. */
            for (i = size - 1; i > 0; i--) {
                /* Recherche du maximum. */
                size_t max = i;
                for (j = 0; j <= i; j++) {
                    if (order == 0) {
                        if (tab[j] > tab[max]) {
                            max = j;
                        }
                    } else {
                        if (tab[j] < tab[max]) {
                            max = j;
                        }
                    }
                }
     
                /* Echange des valeurs du tableau. */
                {
                    float tmp = tab[i];
                    tab[i] = tab[max];
                    tab[max] = tmp;
                }
            }
        }
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    $./a.out 
     
    9.000000
    8.000000
    7.000000
    6.000000
    5.000000
    4.000000
    3.000000
    2.000000
    1.000000
    0.000000

  3. #3
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    oui, j'avais pas remplacé tab par t, mais même avec ça cela ne marche pas !

  4. #4
    Invité(e)
    Invité(e)
    Par défaut
    Il y a un problème ici :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    int lim=SOMME_MAX*100;
        int o = 0;
        int k = 0;
        float *t = malloc((SOMME_MAX*SOMME_MAX)*sizeof *t);
    /*...*/
        for (i = 1; i <= lim; i++) {
            for (j = 1; j <= lim; j++) {
                 /*...*/
                    t[k] = ...
                    k++;
    Au pire, k vaudra lim², or t n'est que de taille SOMME_MAX². lim > SOMME_MAX : on peut donc dépasser la capacité du tableau.
    Solution : allouer plus de mémoire :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
        int lim = ...;
        float *t = malloc((lim * lim) * sizeof *t);
    Sinon, il faut vérifier que les fichiers sont bien ouverts, que les allocations ont réussi et il faut libérer la mémoire à la fin.
    Dernière modification par Invité(e) ; 15/02/2009 à 10h10.

  5. #5
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    merci au poil !

  6. #6
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    bonsoir,

    j'ai rajouté une boucle au programme, fort de ma première erreur j'ai re multiplié la dimension du tableau pour adapter son allocation mais le programme plante à nouveau. Qu'ai je encore fait ? Le programme plante quand le nombre de boucles a atteins 0,2% du nombre de boucle total à effectuer.

    voici le nouveau code :

    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
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #define SOMME_MAX 10
     
    void tri_maximum (float * tab, size_t size, int order);
     
    int main(void)
    {
     
        float c1,c2,c3,resultat;
        int i,j,k;
        int lim=SOMME_MAX*100;
        float iterations=(lim*lim*lim);
        int o = 0;
        int p = 0;
        int l = 0;
        int m = 0;
        float *t = malloc(iterations*sizeof *t);
        float *u = malloc(iterations*sizeof *u);
        FILE* data=fopen("resultats.txt","w");
        FILE* data1=fopen("info.txt","w");
        FILE* data2=fopen("performance.txt","w");
        FILE* data3=fopen("gain.txt","w");
     
        float init_time=GetTickCount();
     
        //printf("%f",init_time);
        //getchar();
     
     
        printf("\nValeur de la cote 1 : ");
        scanf("%f",&c1);
     
        printf("\nValeur de la cote 2 : ");
        scanf("%f",&c2);
     
        printf("\nValeur de la cote 3 : ");
        scanf("%f",&c3);
     
        system("MODE CON cols=40 lines=10");
     
        for ( i=1 ; i<=lim ; i++)
            {
                  for ( j=1 ; j<=lim ; j++ )
                      {
                             for ( k=1 ; k<=lim ; k++ )
                                 {
                                       float g1=((0.01*i)*(c1-1)-(0.01*(j+k)));
                                       float g2=((0.01*j)*(c2-1)-(0.01*(i+k)));
                                       float g3=((0.01*k)*(c3-1)-(0.01*(i+j)));
     
                                       if ( (g1+g2+g3)>0 )
                                          {
                                                     fprintf(data,"\n| m1=%f | m2=%f | m3=%f | E1.gain=%f | E2.gain=%f | E3.gain=%f | E.gain=%f | rapport=%f |", (0.01*i), (0.01*j), (0.01*k), g1, g2, g3, (g1+g2+g3), (g1+g2+g3)/(0.01*(i+j+k)));
     
                                                      u[l]=((g1+g2+g3)/(0.01*(i+j+k)));                   
                                                      t[l]=(g1+g2+g3);
                                                      //l++;
                                          }
     
                                      float progression=(p/iterations)*100;
                                      float vitesse=1000*p/(GetTickCount()-init_time);
                                      float time_left=(iterations-p)/(3600*vitesse);
                                      fprintf(data1,"\n  %f %f %f\%", vitesse, progression, time_left);
                                      p++;
                                 }
                       }
            }
     
     
     
     
     
     
       tri_maximum (t, l, 1);
       tri_maximum (u, l, 1);
     
     
     
       for (o = 0; o < l; o++)
       {
          //printf("\n%f | %f", t[o],u[o]);
          fprintf (data3,"\n| gain=%f |", t[o]);
          fprintf (data2,"\n| gain=%f |", u[o]);
     
       }    
     
     
        fclose(data);
        fclose(data1);
        fclose(data2);
        fclose(data3);
        free(t);
        free(u);
     
     
    }
     
    void tri_maximum (float * tab, size_t size, int order)
    {
       size_t i = 0;
       size_t j = 0;
     
     
       if (tab != NULL && size > 1)
       {
          /* Parcours du tableau. */
          for (i = size - 1; i > 0; i--)
          {
             /* Recherche du maximum. */
             size_t max = i;
             for (j = 0; j <= i; j++)
             {
                if (order == 0)
                {
                   if (tab[j] > tab[max])
                   {
                      max = j;
                   }
                }
                else
                {
                   if (tab[j] < tab[max])
                   {
                      max = j;
                   }
                }
             }
     
             /* Echange des valeurs du tableau. */
             {
                float tmp = tab[i];
                tab[i] = tab[max];
                tab[max] = tmp;
             }
          }
       }
    }

    Merci d'avance.

  7. #7
    Invité(e)
    Invité(e)
    Par défaut
    Citation Envoyé par mabu Voir le message
    Sinon, il faut vérifier que [...] les allocations ont réussi[...].
    Les allocation mémoires échouent certainement. (on tout cas sur mon processseur 32bits, une allocation de 4Go échoue)

    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
    #include <stdio.h>
    #include <stdlib.h>
    #define SOMME_MAX 10
     
    int main(void)
    {
     
        int lim=SOMME_MAX*100;
        float iterations=(lim*lim*lim);
        float *t = NULL;
        float *u = NULL;
     
     
        printf("taille voulue pour t: %f\n" ,iterations*sizeof *t);
        printf("taille voulue pour u: %f\n" ,iterations*sizeof *u);
     
        t = malloc(iterations*sizeof *t);
        if(NULL == t) {
            perror("malloc");
            exit(EXIT_FAILURE);
        }
     
        u = malloc(iterations*sizeof *u);
        if(NULL == u) {
            free(t);
            perror("malloc");
            exit(EXIT_FAILURE);
        }
     
        free(t);
        free(u);
     
        return EXIT_SUCCESS;
     
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    $ ./a.out 
    taille voulue pour t: 4000000000.000000
    taille voulue pour u: 4000000000.000000
    a.out(20644) malloc: *** vm_allocate(size=4000002048) failed (error code=3)
    a.out(20644) malloc: *** error: can't allocate region
    a.out(20644) malloc: *** set a breakpoint in szone_error to debug
    malloc: Cannot allocate memory

  8. #8
    Membre éclairé
    Homme Profil pro
    amateur
    Inscrit en
    Octobre 2007
    Messages
    731
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : amateur

    Informations forums :
    Inscription : Octobre 2007
    Messages : 731
    Par défaut
    Merci pour les réponses. Actuellement je suis rentré chez moi donc j'ai pris mon e-pc, je doute que ce soit un processeur 64bits. Ou s'il est la carte mère ne l'ai surement pas sur ce genre de PC. Quand je rentre ce soir, je retourne sur mon core i7, ça devrait aller mieux .

    Merci encore pour tes réponses.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [MySQL] Problème de tri
    Par pounie dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 22/10/2005, 13h09
  2. Problème de tri avec analyse croisée
    Par drthodt dans le forum Access
    Réponses: 2
    Dernier message: 18/10/2005, 16h23
  3. [TToolBar] Problème de tri
    Par titiyo dans le forum Composants VCL
    Réponses: 6
    Dernier message: 01/09/2004, 09h21
  4. [Collections] Problème de tri
    Par feti2004 dans le forum Collection et Stream
    Réponses: 16
    Dernier message: 03/08/2004, 16h45
  5. problème de tri et optimisatiopn
    Par psyco2604 dans le forum XSL/XSLT/XPATH
    Réponses: 9
    Dernier message: 13/05/2004, 10h44

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo