home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8908.arc
/
ANDERSON.LST
next >
Wrap
File List
|
1989-07-06
|
4KB
|
174 lines
C MULTIDIMENSIONAL ARRAYS AT RUN TIME
by Paul Anderson
[LISTING ONE]
#include <stdio.h>
#include <malloc.h>
char **dim2(row, col, size) /* creates 2D array */
int row, col;
unsigned size;
{
int i;
char **prow, *pdata;
pdata = (char *) calloc(row * col, size);
if (pdata == (char *) NULL) {
fprintf(stderr, "No heap space for data\n");
exit(1);
}
prow = (char **) malloc(row * sizeof (char *));
if (prow == (char **) NULL) {
fprintf(stderr, "No heap space for row pointers\n");
exit(1);
}
for (i = 0; i < row; i++) {
prow[i] = pdata; /* store pointers to rows */
pdata += size * col; /* move to next row */
}
return prow; /* pointer to 2D array */
}
void free2(pa) /* frees 2D heap storage */
char **pa;
{
free(*pa); /* free the data */
free(pa); /* free pointer to row pointers */
}
[LISTING TWO]
#include <stdio.h>
#include <malloc.h>
char ***dim3(grid, row, col, size) /* creates 3D array */
int grid, row, col;
unsigned size;
{
int i;
char ***pgrid, **prow, *pdata;
pdata = (char *) calloc(grid * row * col, size);
if (pdata == (char *) NULL) {
fprintf(stderr, "No heap space for data\n");
exit(1);
}
prow = (char **) malloc(grid * row * sizeof (char *));
if (prow == (char **) NULL) {
fprintf(stderr, "No heap space for row pointers\n");
exit(1);
}
pgrid = (char ***) malloc(grid * sizeof (char **));
if (pgrid == (char ***) NULL) {
fprintf(stderr, "No heap space for grid pointers\n");
exit(1);
}
for (i = 0; i < grid * row; i++) {
prow[i] = pdata; /* store pointers to rows */
pdata += col * size; /* move to next row */
}
for (i = 0; i < grid; i++) {
pgrid[i] = prow; /* store pointers to grid */
prow += row; /* move to next grid */
}
return pgrid; /* pointer to 3D array */
}
void free3(pa) /* frees 3D heap storage */
char ***pa;
{
free(**pa); /* free the data */
free(*pa); /* free the row pointers */
free(pa); /* free the grid pointers */
}
[LISTING THREE]
/* det.c - find determinant of a two-dimensional array of doubles */
#include <stdio.h>
#include <malloc.h>
main()
{
double det();
static double f[4][4] = {
1, 3, 2, 1,
4, 6, 1, 2,
2, 1, 2, 3,
1, 2, 4, 1
};
static double g[5][5] = {
1, 3, 2, 1, 7,
4, 6, 1, 2, 6,
2, 1, 2, 3, 5,
1, 2, 4, 1, 4,
8, 5, 4, 1, 3
};
printf ("determinant of f = %g\n", det(f, 4));
printf ("determinant of g = %g\n", det(g, 5));
}
double det(arg, n) /* calculate determinant for n by n matrix */
char *arg;
int n;
{
register int i, j, k;
double **a; /* this is the array name */
char **sdim2();
double ret; /* determinant */
double x; /* temp */
/* dynamically create 2 dimensional "array" a from arg */
a = (double **) sdim2(arg, n, n, sizeof(double));
/* determinant algorithm using rows and columns */
for (k = 0; k < n - 1; k++)
for (i = k + 1; i < n; i++){
x = a[i][k]/a[k][k];
for (j = k; j < n; j++)
a[i][j] = a[i][j] - x * a[k][j];
}
for (ret = 1, i = 0; i < n; i++)
ret *= a[i][i];
free(a); /* free heap storage */
return ret;
}
char **sdim2(pdata, row, col, size) /* "creates" 2D array */
char *pdata;
int row, col;
unsigned size;
{
int i;
register char **prow;
prow = (char **) malloc(row * sizeof (char *));
if (prow == (char **) NULL) {
fprintf(stderr, "No heap space for row pointers\n");
exit(1);
}
for (i = 0; i < row; i++) {
prow[i] = pdata; /* store pointers to rows */
pdata += size * col; /* move to next row */
}
return prow; /* pointer to 2D array */
}