// // demonstrates two dimensional row-dominate arrays // internal representation is a continguous block of memory with ascending // addresses corresponding to ascending columns // // for example // // the 2d array : // 0,0 0,1 0,2 // 1,0 1,1 1,2 // 2,0 2,1 2,2 // // can be represented in memory using a 1d array as : // 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 // // calculating the index into the 1d array is done by computing a row,column index relative to zero // and then multiplying the row index by the length of a row (also the number of columns) and adding // the column index // // Written by Maxwell Sayles, 2001, for CPSC331, University of Calgary // #include #include using namespace std; // // 2d array class // class Array2d { public: Array2d (int rowLowIndex, int rowHighIndex, int columnLowIndex, int columnHighIndex); ~Array2d(); int getItem (int row, int column); void setItem (int row, int column, int value); int getRowLowIndex() { return this->rowLowIndex; } int getRowHighIndex() { return this->rowHighIndex; } int getColumnLowIndex() { return this->columnLowIndex; } int getColumnHighIndex() { return this->columnHighIndex; } // returns the number of rows int getRowCount() { return this->rowHighIndex - this->rowLowIndex + 1; } // returns the number of columns int getColumnCount() { return this->columnHighIndex - this->columnLowIndex + 1; } int getArea() { return this->getRowCount() * this->getColumnCount(); } private: int rowLowIndex; int rowHighIndex; int columnLowIndex; int columnHighIndex; int* data; }; // // construct an array with the specified row,column bounds // bounds are inclusive // Array2d::Array2d (int rowLowIndex, int rowHighIndex, int columnLowIndex, int columnHighIndex) { this->rowLowIndex = rowLowIndex; this->rowHighIndex = rowHighIndex; this->columnLowIndex = columnLowIndex; this->columnHighIndex = columnHighIndex; this->data = new int [this->getArea()]; } // // destruct the array // delete the data member Array2d::~Array2d() { delete[] this->data; } // // return the item at the specified row,column // int Array2d::getItem (int row, int column) { int baseRow = row - this->rowLowIndex; int baseColumn = column - this->columnLowIndex; int index = baseRow*this->getColumnCount() + baseColumn; return this->data[index]; } // // set the item at the specified row,column // void Array2d::setItem (int row, int column, int value) { int baseRow = row - this->rowLowIndex; int baseColumn = column - this->columnLowIndex; int index = baseRow*this->getColumnCount() + baseColumn; this->data[index] = value; } // // program entry // int main() { int row = 0; int column = 0; // get bounds of table from user input int rowLowIndex = 0; int rowHighIndex = 0; int columnLowIndex = 0; int columnHighIndex = 0; cout << "Enter the row low index (e.g. 16): "; cin >> rowLowIndex; cout << "Enter the row high index (e.g. 32): "; cin >> rowHighIndex; cout << "Enter the column low index (e.g. 24): "; cin >> columnLowIndex; cout << "Enter the column high index (e.g. 36): "; cin >> columnHighIndex; cout << endl << endl; // create 2d array Array2d table (rowLowIndex, rowHighIndex, columnLowIndex, columnHighIndex); // build multiplication table for (row = table.getRowLowIndex(); row <= table.getRowHighIndex(); row ++) { for (column = table.getColumnLowIndex(); column <= table.getColumnHighIndex(); column ++) { table.setItem (row, column, row*column); } } // print rows across the top cout << " x "; for (column = table.getColumnLowIndex(); column <= table.getColumnHighIndex(); column ++) { cout << setw(4) << column << ' '; } cout << endl << endl; for (row = table.getRowLowIndex(); row <= table.getRowHighIndex(); row ++) { cout << setw(2) << row << " "; for (column = table.getColumnLowIndex(); column <= table.getColumnHighIndex(); column ++) { int value = table.getItem (row, column); cout << setw(4) << value << ' '; } cout << endl; } cout << endl << endl; return 0; }