three-dimensional arrays
Author Message
three-dimensional arrays

hi, i'm a novice c programmer- i'm modifying someone else's program to suit
my needs.  i'm trying to create a 3-d array but when i print out the
contents of the array, i get egregious values.  please help me if you can.
below is the code and troubling output.  thank you.

-Arvind

ok, "dsmatrix" is a function defined below that allocates
space for a p x q matrix.  this function was written by
someone else and it works for my program.  i think
"S_alloc" is just a function like malloc.

what i want to do is write a function that will allocate
space for a three-dimensional matrix (p x q x r).  is
what i've written, tsmatrix, below correct? i'm not
exactly sure if i got the nested for loop correct.

/************************************************************/
double **dsmatrix(r,c)
int r,c;
/* allocate a double matrix with subscript range m[0..r][0..c] */
{
int i;
double **m;
char *S_alloc();
m=(double **) S_alloc((long)(r+1),sizeof(double*));
for(i=0;i<=r;i++) m[i]=(double *) S_alloc((long) (c+1),sizeof(double));
return m;

Quote:
}

/*************************************************************/

/*************************************************************/
double ***tsmatrix(r,c,a)
int r,c,a;
/* allocate a 'triple matrix' with subscript range m[0..r][0..c][0..a] */
{
int i,j;
double ***m;
char *S_alloc();
m=(double ***) S_alloc((long)(r+1),sizeof(double**));
for(i=0;i<=r;i++) {
m[i]=(double **) S_alloc((long)(c+1),sizeof(double*));
for(j=0;j<=c;j++) {
m[i][j]=(double *) S_alloc((long) (a+1),sizeof(double));
}
}
return m;

Quote:
}

/****************************************************/
/* So now, below, I'm testing to see whether I've
** successfully created an n x p x q array and so I
** insert values into "rming2u" and print them **/
/* Note, the below C code is just small excerpt
* from a single routine in my much larger program */

double ***rming2u;

/* the following two lines exist outside of the current routine */
double ***raori3;
raori3=tsmatrix(ndata,MAXSPACE+5,ncov+5);

rming2u=raori3;

/* computing G2(u) matrices */
/* indices j,k,l are all available */

for(i=0;i<4;i++) {
for(j=0;j<3;j++) {
for(k=0;k<2;k++) {
rming2u[i][j][k]=0.;
rming2u[i][j][k]=i*j*k;
}
}
}

for(i=0;i<4;i++) {
for(j=0;j<3;j++) {
for(k=0;k<2;k++) {
(void)printf("rao:ijk rming2u %d %d %d %d \n",i,j,k,
rming2u[i][j][k]);
(void)fflush(stdout);
}
}
}

/*** BUT, these are the values I get below- something is wrong-
***  the nonzero values should not be this large.
***/

rao:ijk rming2u 1 0 0 0
rao:ijk rming2u 1 0 1 0
rao:ijk rming2u 1 1 0 0
rao:ijk rming2u 1 1 1 1072693248
rao:ijk rming2u 1 2 0 0
rao:ijk rming2u 1 2 1 1073741824
rao:ijk rming2u 2 0 0 0
rao:ijk rming2u 2 0 1 0
rao:ijk rming2u 2 1 0 0
rao:ijk rming2u 2 1 1 1073741824
rao:ijk rming2u 2 2 0 0
rao:ijk rming2u 2 2 1 1074790400
rao:ijk rming2u 3 0 0 0
rao:ijk rming2u 3 0 1 0
rao:ijk rming2u 3 1 0 0
rao:ijk rming2u 3 1 1 1074266112
rao:ijk rming2u 3 2 0 0
rao:ijk rming2u 3 2 1 1075314688
--

Sun, 06 Jul 2003 04:02:02 GMT
three-dimensional arrays

Quote:

> i think "S_alloc" is just a function like malloc.

Not quite, as malloc takes only one argument.

Quote:
> is what i've written, tsmatrix, below correct?

I think it is, if S_alloc cannot return NULL.

Quote:
> double ***rming2u;
[...]
>             (void)printf("rao:ijk rming2u %d %d %d %d \n",i,j,k,
>                         rming2u[i][j][k]);

The bug is here.  Print doubles with "%f", not "%d".

How old is this program?
--

Tue, 08 Jul 2003 10:49:13 GMT
three-dimensional arrays

Quote:

> hi, i'm a novice c programmer- i'm modifying someone else's program to suit
> my needs.  i'm trying to create a 3-d array but when i print out the
> contents of the array, i get egregious values.  please help me if you can.
> below is the code and troubling output.  thank you.

[snip]

Quote:
> double ***rming2u;

> /* the following two lines exist outside of the current routine */
>    double ***raori3;
>    raori3=tsmatrix(ndata,MAXSPACE+5,ncov+5);

> rming2u=raori3;

Why do you use hardcoded dimension limit values in the loops, when the
array was allocated using ndata, MAXSPACE+5 and ncov+5?

[snip]

Quote:
>    for(i=0;i<4;i++) {
>       for(j=0;j<3;j++) {
>          for(k=0;k<2;k++) {
>             (void)printf("rao:ijk rming2u %d %d %d %d \n",i,j,k,
>                         rming2u[i][j][k]);
>             (void)fflush(stdout);
>          }
>       }
>    }

You're storing doubles in the 3d array, but printing them out as
integers. Use %f instead of %d for the last formatting parameter of
printf. Since printf is a variadic function, the value
rming2u[i][j][k] does not get converted to integer in the call.

HTH,
AriL
--
Pain and disappointment are inevitable. Misery is optional.
Homepaged at http://www.angelfire.com/or/lukumies
--

Tue, 08 Jul 2003 10:49:17 GMT
three-dimensional arrays
There are several issues with that code, but I think the problem might be
(void)printf("rao:ijk rming2u %d %d %d %d \n",i,j,k,
rming2u[i][j][k]);
where you format rming2u[i][j][k as a decimal int (%d) when in fact it is a
float so printf mis-interprets the bits in it.
In
rming2u[i][j][k]=i*j*k;
you have an implicit cast from int to double

Quote:
> hi, i'm a novice c programmer- i'm modifying someone else's program to
suit
> my needs.  i'm trying to create a 3-d array but when i print out the
> contents of the array, i get egregious values.  please help me if you can.
> below is the code and troubling output.  thank you.

> -Arvind

> ok, "dsmatrix" is a function defined below that allocates
> space for a p x q matrix.  this function was written by
> someone else and it works for my program.  i think
> "S_alloc" is just a function like malloc.

> what i want to do is write a function that will allocate
> space for a three-dimensional matrix (p x q x r).  is
> what i've written, tsmatrix, below correct? i'm not
> exactly sure if i got the nested for loop correct.

> /************************************************************/
> double **dsmatrix(r,c)
> int r,c;
> /* allocate a double matrix with subscript range m[0..r][0..c] */
> {
>    int i;
>    double **m;
>    char *S_alloc();
>    m=(double **) S_alloc((long)(r+1),sizeof(double*));
>    for(i=0;i<=r;i++) m[i]=(double *) S_alloc((long) (c+1),sizeof(double));
>    return m;
> }
> /*************************************************************/

> /*************************************************************/
> double ***tsmatrix(r,c,a)
> int r,c,a;
> /* allocate a 'triple matrix' with subscript range m[0..r][0..c][0..a] */
> {
>    int i,j;
>    double ***m;
>    char *S_alloc();
>    m=(double ***) S_alloc((long)(r+1),sizeof(double**));
>    for(i=0;i<=r;i++) {
>       m[i]=(double **) S_alloc((long)(c+1),sizeof(double*));
>       for(j=0;j<=c;j++) {
>          m[i][j]=(double *) S_alloc((long) (a+1),sizeof(double));
>       }
>    }
>    return m;
> }
> /****************************************************/
> /* So now, below, I'm testing to see whether I've
> ** successfully created an n x p x q array and so I
> ** insert values into "rming2u" and print them **/
> /* Note, the below C code is just small excerpt
> * from a single routine in my much larger program */

> double ***rming2u;

> /* the following two lines exist outside of the current routine */
>    double ***raori3;
>    raori3=tsmatrix(ndata,MAXSPACE+5,ncov+5);

> rming2u=raori3;

> /* computing G2(u) matrices */
> /* indices j,k,l are all available */

>    for(i=0;i<4;i++) {
>       for(j=0;j<3;j++) {
>          for(k=0;k<2;k++) {
>             rming2u[i][j][k]=0.;
>             rming2u[i][j][k]=i*j*k;
>          }
>       }
>    }

>    for(i=0;i<4;i++) {
>       for(j=0;j<3;j++) {
>          for(k=0;k<2;k++) {
>             (void)printf("rao:ijk rming2u %d %d %d %d \n",i,j,k,
>                         rming2u[i][j][k]);
>             (void)fflush(stdout);
>          }
>       }
>    }

> /*** BUT, these are the values I get below- something is wrong-
> ***  the nonzero values should not be this large.
> ***/

> rao:ijk rming2u 1 0 0 0
> rao:ijk rming2u 1 0 1 0
> rao:ijk rming2u 1 1 0 0
> rao:ijk rming2u 1 1 1 1072693248
> rao:ijk rming2u 1 2 0 0
> rao:ijk rming2u 1 2 1 1073741824
> rao:ijk rming2u 2 0 0 0
> rao:ijk rming2u 2 0 1 0
> rao:ijk rming2u 2 1 0 0
> rao:ijk rming2u 2 1 1 1073741824
> rao:ijk rming2u 2 2 0 0
> rao:ijk rming2u 2 2 1 1074790400
> rao:ijk rming2u 3 0 0 0
> rao:ijk rming2u 3 0 1 0
> rao:ijk rming2u 3 1 0 0
> rao:ijk rming2u 3 1 1 1074266112
> rao:ijk rming2u 3 2 0 0
> rao:ijk rming2u 3 2 1 1075314688
> --

--

Tue, 08 Jul 2003 10:49:49 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages