X
xhunga
I have try a new version of my work.
I have put the sizes of the matrix into the matrix.
A[0][0] = number of rows
A[0][1] = number of columns
The first element of the matrix is A[1][1]
instead of A[0][0].
You can not use the row 0, and the column 0.
I have try with the matrix 1x1, it seems correct.
I have write the add_mR() and mul_mR() with
this type of matrices, it seems correct.
Is it a good idea ?
You can see the code below.
Thank
/* http://groups.yahoo.com/group/mathc/ */
/* ---------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
/* ---------------------------------------- */
#define C1 1
/* -----------------------------------------
Do : print matrix A.
----------------------------------------- */
void p_mR(
double **A
)
{
int r;
int c;
for (r=1; r<A[0][0]; r++)
{
printf("\n");
for (c=1; c<A[0][1]; c++)
printf(" %5.0f ", A[r][c]);
}
printf("\n");
}
/* -----------------------------------------
Do : copy A into B.
------------------------------------------- */
void c_mR(
double **A,
double **B
)
{
int r;
int c;
for (r=1; r<A[0][0]; r++)
for (c=1; c<A[0][1]; c++)
B[r][c] = A[r][c];
}
/* ----------------------------------------
Do : Dynamically allocate a multidimensional array.
(see : FAQ of the comp.lang.c group)
You can keep the array's contents contiguous,
r1 r2 ... rn
xxxx|xxxx|...|xxxx
------------------------------------------- */
double **i_mR(
int r,
int c
)
{
double **A;
int ar;
int ac;
int i;
if(r<1||c<1)
{
printf(" The size of the matrix must be positives
integers.\n\n");
printf(" double **i_mR(); \n\n");
fflush(stdout);
getchar();
exit(EXIT_FAILURE);
}
ar = r + C1;
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=1; i<ar; i++) A = A[0]+i*ac;
A[0][0]=ar;
A[0][1]=ac;
return(A);
}
/* ------------------------------
Do : free the arrays when they are no longer needed
---------------------------- */
void f_mR(
double **A
)
{
free(A[0]);
free(A);
}
/* --------------------------------- MAIN */
int main()
{
int i;
int j;
int n;
int r;
int c;
double **A;
double **B;
/*------------------------- INITIALISATION */
r = 3;
c = 5;
A = i_mR(r,c);
B = i_mR(r,c);
/*-------------------------------- PROGRAM */
for (i=1,n=1; i<A[0][0]; i++)
for (j=1; j<A[0][1]; j++)
A[j]=n++;
printf(" A : \n");
p_mR(A);
printf("\n");
c_mR(A,B);
printf(" B : \n");
p_mR(B);
f_mR(A);
f_mR(B);
printf("\n Press Return to continue");
getchar();
return 0;
}
I have put the sizes of the matrix into the matrix.
A[0][0] = number of rows
A[0][1] = number of columns
The first element of the matrix is A[1][1]
instead of A[0][0].
You can not use the row 0, and the column 0.
I have try with the matrix 1x1, it seems correct.
I have write the add_mR() and mul_mR() with
this type of matrices, it seems correct.
Is it a good idea ?
You can see the code below.
Thank
/* http://groups.yahoo.com/group/mathc/ */
/* ---------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
/* ---------------------------------------- */
#define C1 1
/* -----------------------------------------
Do : print matrix A.
----------------------------------------- */
void p_mR(
double **A
)
{
int r;
int c;
for (r=1; r<A[0][0]; r++)
{
printf("\n");
for (c=1; c<A[0][1]; c++)
printf(" %5.0f ", A[r][c]);
}
printf("\n");
}
/* -----------------------------------------
Do : copy A into B.
------------------------------------------- */
void c_mR(
double **A,
double **B
)
{
int r;
int c;
for (r=1; r<A[0][0]; r++)
for (c=1; c<A[0][1]; c++)
B[r][c] = A[r][c];
}
/* ----------------------------------------
Do : Dynamically allocate a multidimensional array.
(see : FAQ of the comp.lang.c group)
You can keep the array's contents contiguous,
r1 r2 ... rn
xxxx|xxxx|...|xxxx
------------------------------------------- */
double **i_mR(
int r,
int c
)
{
double **A;
int ar;
int ac;
int i;
if(r<1||c<1)
{
printf(" The size of the matrix must be positives
integers.\n\n");
printf(" double **i_mR(); \n\n");
fflush(stdout);
getchar();
exit(EXIT_FAILURE);
}
ar = r + C1;
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=1; i<ar; i++) A = A[0]+i*ac;
A[0][0]=ar;
A[0][1]=ac;
return(A);
}
/* ------------------------------
Do : free the arrays when they are no longer needed
---------------------------- */
void f_mR(
double **A
)
{
free(A[0]);
free(A);
}
/* --------------------------------- MAIN */
int main()
{
int i;
int j;
int n;
int r;
int c;
double **A;
double **B;
/*------------------------- INITIALISATION */
r = 3;
c = 5;
A = i_mR(r,c);
B = i_mR(r,c);
/*-------------------------------- PROGRAM */
for (i=1,n=1; i<A[0][0]; i++)
for (j=1; j<A[0][1]; j++)
A[j]=n++;
printf(" A : \n");
p_mR(A);
printf("\n");
c_mR(A,B);
printf(" B : \n");
p_mR(B);
f_mR(A);
f_mR(B);
printf("\n Press Return to continue");
getchar();
return 0;
}