Anda di halaman 1dari 4

DYNAMIC 2-D ARRAYS

1. Introduction
An often required data structure is a "dynamic multi-dimensional arrays" (usually 2-D).
There are two principal approaches:

1. Consider the array as a 1-D array representing the different dimensions in


sequence, for example in the case of a 2-D dynamic the array would comprise a
sequence of rows. The size of the array can then be defined (assuming an integer
array) using a statement of the form: malloc(numRows*numCols*sizeof(int)).
2. Represent the array as an "array of arrays of arrays ...". This would then require us
to malloc space for each individual array in turn.

Both approaches are illustrated below (with two versions for approach 1) using a
dynamic 2-D array example.

2. Approach 1 - Version 1
This approach simulates the 2-D array using a 1-D array. Two dimensional indexes must
therefore be calculated using the identity: (rowIndex*numCols)+colIndex.

#include < stdio.h >


#include < stdlib.h >

void main(void)
{
int numRows, numCols;
int *arrayPtr;
int rowIndex, colIndex;

/* Request for array dimensions. */

printf("Enter values for numRows and numCols\n");


scanf("%d",&numRows);
scanf("%d",&numCols);

/* Create space for array (Malloc returns NULL if no space available).


*/

arrayPtr = (int *) malloc(numRows*numCols*sizeof(int));


if (arrayPtr == NULL) {
printf("Not enough memory\n");
exit(1)
}

/* Initialise# the array with a sequence of numbers derived from the indexes. */

for(rowIndex=0;rowIndex < numRows;rowIndex++) {


for(colIndex=0;colIndex < numCols;colIndex++) {
arrayPtr[(rowIndex*numCols)+colIndex] = (rowIndex*numCols)+colIndex;
}
}

/* Output the result. */

for(rowIndex=0;rowIndex < numRows;rowIndex++)


{
for(colIndex=0;colIndex < numCols;colIndex++) {
printf("%d ",arrayPtr[(rowIndex*numCols)+colIndex]);
}
printf("\n");
}
}

3. Approach 1 - Version 2
The disadvantage of the above approach is that it is not a real 2-D array in the sense that we cannot index into the array using the
standard arrayName[rowIndex][colIndex] approach used with constrained 2-D arrays. One way round this is to use a #define
statement to define INDEX(X,Y) as (X*numRows)+Y so that we can now index in using statements of the form
arrayName[INDEX(rowIndex,colIndex)] --- not quite what we would desire (some practitioners might even consider it to be a
"hack") but close!

#include < stdio.h >


#include < stdlib.h >
#define INDEX(X,Y) (X*numRows)+Y
void main(void)
{
int numRows, numCols;
int *arrayPtr;
int rowIndex, colIndex;
printf("Enter values for numRows and numCols\n");
scanf("%d",&numRows);
scanf("%d",&numCols);
/* Create space (Malloc returns NULL if no space available). */
arrayPtr = (int *) malloc(numRows*numCols*sizeof(int));
if (arrayPtr == NULL)
{
printf("Not enough memory\n");
exit(1);
}

arrayPtr[INDEX(0,0)] = 0;

/* Initialise the array with a sequence of numbers derived from the


indexes. */

for(rowIndex=0;rowIndex < numRows;rowIndex++) {


for(colIndex=0;colIndex < numCols;colIndex++) {
arrayPtr[INDEX(rowIndex,colIndex)] =
(rowIndex*numCols)+colIndex;
}
}

/* Output the result. */

for(rowIndex=0;rowIndex < numRows;rowIndex++) {


for(colIndex=0;colIndex < numCols;colIndex++) {
printf("%d ",arrayPtr[INDEX(rowIndex,colIndex)]);
}
printf("\n");
}
}

4. Approach 2
If we insist on using the standard arrayName[rowIndex][colIndex] 2-D array indexing
approach then we need to do something a bit more complicated --- we need to create an
array of arrays. To do this we must use "a pointer to a pointer" to the start of the array
(i.e. **arrayName), create space for the first array and then create space for each sub
array by iterating (looping) through the first array and so on. Thus:

#include < stdio.h >


#include < stdlib.h >
void main(void)
{
int numRows, numCols;
int **arrayPtr;
int rowIndex, colIndex;
printf("Enter values for numRows and numCols\n");
scanf("%d",&numRows);
scanf("%d",&numCols);
/* Create space for first array (array of pointers to each row). */
arrayPtr = (int **) malloc(numRows*sizeof(int));
if (arrayPtr == NULL) {
printf("Not enough memory\n");
exit(1);
}
/* Loop through first array and create space for according to number of
columns.*/

for(rowIndex=0;rowIndex < numRows;rowIndex++) {


arrayPtr[rowIndex] = malloc(numCols*sizeof(int));
if (arrayPtr[rowIndex] == NULL) {
printf("Not enough memory\n");
exit(1);
}
}

/* Initialise the array with a sequence of numbers derived from the


indexes . */

for(rowIndex=0;rowIndex < numRows;rowIndex++) {


for(colIndex=0;colIndex < numCols;colIndex++) {
arrayPtr[rowIndex][colIndex] =
(rowIndex*numCols)+colIndex;
}
}

/* Output the result. */

for(rowIndex=0;rowIndex < numRows;rowIndex++) {


for(colIndex=0;colIndex < numCols;colIndex++) {
printf("%d ",arrayPtr[rowIndex][colIndex]);
}
printf("\n");
}
}

Anda mungkin juga menyukai