Notes for class on Friday 9/28/2001 One problem I suspect the students will have will be the creation of dynamic arrays. For the assignment, the students must have a stack of "previous input sources" to be used with the f command. The initial input source is "cin", the other input sources would be of type ifstream that would read from a file. The type "istream" is a base type for all of these, so a stack of istream instances should be used. Since this stack should be fairly small, I would suggest creating a dynamic array to hold the stack instead of a linked list. Initially create the stack to hold ten values. If this stack ever gets full, we can allocate a larger array and copy the values from the previous array over before deallocating it. Data Members: int count, size; istream *stack; Initialization: count = 0; stack = new istream [10]; size = 10; Push: if (count == size) increaseSize(); stack[count] = XXXX; count++; Pop: if (count == 0) error(); count--; XXXX = stack[count]; increaseSize(): istream *temp = stack; stack = new istream [size*2]; for (i = 0; i < size; i++) stack[i] = temp[i]; size = size*2; delete [] temp; The above is the basic ideas for a stack in a dynamic array. For the matrix, we either need a 2-D array or a single D array that we access with the translation of two subscripts. A 2-D array is a bit strange to dynamically allocate, so the single D array with translation of 2 subscripts may be easier. If we want to simulate the 2-D array of size RxC with a single D array, the single D array must have R*C entries. I.E. a 2-D array of 5 rows and 3 columns needs 15 entries (5*3) in a single D array. If we write a function called access(row,col) that wishes to access the position at array[row][col], the subscript in the single D array is determined by (row * numberOfColumns) + col In order to properly dynamically allocate a two D array of size RxC, we would need to following steps: (we will assume it is of type int) Definition: int **twoDArray; // we need a pointer to a pointer First we need to allocate an array of pointer to integer twoDArray = new int* [R]; For each entry (row) in this array we need to allocate C integers for (i = 0; i < R; i++) twoDArray[i] = new int [C]; Now we can access this array using the normal array access notation. i.e. the following will initialize all locations to zero for (i = 0; i < R; i++) for (j = 0; j < C; j++) twoDAraay[i][j] = 0; To deallocate the array, we must first deallocate all rows in the array: for (i = 0; i < R; i++) delete [] twoDArray[i]; Then we must deallocate the array itself: delete [] twoDArray;