Dynamic 2

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Dynamic 2 as PDF for free.

More details

  • Words: 655
  • Pages: 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"); } }

Related Documents

Dynamic 2
November 2019 16
Dynamic
June 2020 20
Dynamic
June 2020 27
Dynamic
November 2019 49
Html Dynamic
November 2019 18
Dynamic Report
May 2020 8