Installer ce fichier dans votre répertoire de travail.

vim2.h
i_mR, f_mR
/* ------------------------------------ */
/*  Save as :   vim2.h                  */
/* ------------------------------------ */

/* ---------------------------------------------------------------------------
 Do    : Dynamically allocate a multidimensional array.

            (see : FAQ of the comp.lang.c group)

        You can keep the array's contents contiguous,

                 r0    r1    r2    ... rn
                 R_000|C_xxx|0_xxx|...|0_xxx


                R =  Number of rows.
                C =  Number of columns.

        The declaration of the sizes into the matrices, it is my work.

        So be careful.

        The first row and the first column are not used.

                      *********************

        The size of the row    of the matrix is into  A[R_SIZE][C0] = A[0][0]
        The size of the column of the matrix is into  A[C_SIZE][C0] = A[1][0]

                      *********************

        The first element of the matrix is z =(A[1][1],A[1][1])

        For a 10x10 matrix the last element is z = (A[10][10],A[10][10])

                      *********************

        If you want to duplicate a matrix A, you must use :

        double **T;

        T = i_duplicate_mR(  A[R_SIZE][C0],
                             A[C_SIZE][C0]);

        f_mR(T);

   -------------------------------------------------------------------------- */
double **i_mR(
int      r,
int      c
)
{
double **A;
int     ar;
int     ac;
int      i;

            if(r<R1||c<C1)
            {
            printf(" The size of the matrix must be positive integers.\n\n");
            printf(" double **i_mR(); \n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

         ar = r + R1;
         ac = c + C1;

         A = malloc(ar * sizeof(*A));
             if(!A)
            {
            printf(" I was unable to allocate the memory you requested.\n\n");
            printf(" double **i_mR(); \n\n");
            printf(" **A = malloc(ar * sizeof(*A));\n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

      A[0] = malloc(ar * ac * sizeof(**A) );
             if(!A[0])
            {
            printf(" I was unable to allocate the memory you requested.\n\n");
            printf(" double **i_mR();\n\n");
            printf(" A[0] = malloc(ar * ac * sizeof(**A) );\n\n");
            fflush(stdout);
            getchar();
            exit(EXIT_FAILURE);
            }

	for(i=R1; i<ar; i++) A[i] = A[0]+i*ac;

/* ----------- Give a value to the zero row and the zero column ------------- */

    A[R_SIZE][C0] = ar;
    A[C_SIZE][C0] = ac;

    for(r=R2; r<A[R_SIZE][C0]; r++)

        A[r][0] = 0.;

    for(c=C1; c<A[C_SIZE][C0]; c++)

        A[0][c] = c;

    m0_mR(A);

return(A);
}
/* ------------------------------------ */   
void f_mR(
double **A
)
{
  if(A)  free(A[0]);

  free(A);
}
/* ------------------------------------ */   
double **i_duplicate_mR(
int      r,
int      c
)
{
return(i_mR(--r,--c));
}
/* ------------------------------------ */

C'est dans ce fichier que se trouvent les fonctions de constructions et de destructions des matrices. L'étude de ce code pourra se faire dans un deuxième temps.

Cet article est issu de Wikibooks. Le texte est sous licence Creative Commons - Attribution - Partage dans les Mêmes. Des conditions supplémentaires peuvent s'appliquer aux fichiers multimédias.