Partage
  • Partager sur Facebook
  • Partager sur Twitter

Probleme de tableau

Sujet résolu
    28 novembre 2008 à 12:12:52

    Bonjour,

    j'ai un petit problème avec ce programme simple mais je ne comprends pas l'erreur. Quelqu'un peut-il m'aider?

    //HEADER
    
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
            //VARIABLES
            
            /*Variables declaration*/
            double a, b, c, d, p[20], result[20][2], x[20];
            int        i;
    
            //PROCESSING
            
            /*Formula presentation*/
            printf("The formula is: P = ax^3 + bx^2 + cx + d\n");
    
            /*Appropriation of a*/
            printf("\nValue of a: ");
            scanf("%lf", &a);
    
            /*Appropriation of b*/
            printf("Value of b: ");
            scanf("%lf", &b);
    
            /*Appropriation of c*/
            printf("Value of c: ");
            scanf("%lf", &c);
            
            /*Appropriation of d*/
            printf("Value of d: ");
            scanf("%lf", &d);
    
            /*Appropriation of x[0]*/
            printf("\nFirst value of the range of evaluation: ");
            scanf("%lf", &x[0]);
    
            /*Loop FOR*/
            for (i=0; i<20; i++)
            {
                    x[i] = x[0]+i;
    
                    /*Polynomial evaluation*/
                    p[i]=d+x[i]*(c+x[i]*(b+a*x[i]));
    
                    /*Result table filling*/
                    result[i][1]=x[i];
                    result[i][2]=p[i];
            }
            
            /*Polynomial evaluation and solution display*/
            printf ("\n\n");
            for (i=0; i<20; i++)
                    printf ("For x = %.2f -> P = %.2f\n", result[i][1], result[i][2]);
    
            printf ("\n");
                    
            /*Wait for the user to press any key to continue*/
            system ("pause");
    
                    return 0;
    }
    




    et avec ce code, j'obtiens l'erreur

    Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted.

    lorsque je souhaite quitter le programme.

    Quelqu'un pourrait m'expliquer cette erreur?

    Merci
    • Partager sur Facebook
    • Partager sur Twitter
      28 novembre 2008 à 12:16:40

      tu dois purger stdin après chaque scanf
      • Partager sur Facebook
      • Partager sur Twitter
        28 novembre 2008 à 12:17:12

        Salut,

        Ton probleme vient probablement de ca :

        /*Result table filling*/
        result[i][1]=x[i];
        result[i][2]=p[i];
        


        Tu essayes d'acceder a une case qui depasse la taille de ton tableau, ceci devrait mieux marcher:

        /*Result table filling*/
        result[i][0]=x[i];
        result[i][1]=p[i];
        


        (L'erreur est aussi presente lors de l'affichage)
        • Partager sur Facebook
        • Partager sur Twitter
          28 novembre 2008 à 12:21:49

          Merci,

          Je n'arrivai pas à voir l'erreur. Maintenant ca marche parfaitement.

          Bonne journée
          • Partager sur Facebook
          • Partager sur Twitter

          Probleme de tableau

          × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
          × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
          • Editeur
          • Markdown