
Patrick B. answered 07/07/21
Math and computer tutor/teacher
#ifndef _DYNAMIC_ARRAY
#define _DYNAMIC_ARRAY
#define DISPLAY_DIRECTION_VERTICAL (0)
#define DISPLAY_DIRECTION_HORIZONTAL (1)
typedef
class DynamicArray
{
private:
int * data;
int size;
int * temp;
public:
DynamicArray();
DynamicArray(int size);
~DynamicArray();
DynamicArray(const DynamicArray& dynamicArray);
int IndexerSetAtIndex(int iIndexPos, int * iNumVal);
int IndexerGetAtIndex(int iIndexPos, int * iNumVal);
int Allocate(int size);
int RedimPreserve(int size);
int Dump(char * strMsg=(char*)0, int direction=DISPLAY_DIRECTION_VERTICAL);
} * TDynamicArray;
#define DYNAMIC_ARRAY_SIZE ( sizeof(DynamicArray))
typedef
class DynamicArray2D
{
private:
int ** data;
int numRows;
int numColumns;
public:
DynamicArray2D( int rows, int columns);
Dynamicarray2D( const DynamicArray2D &);
int IndexerSetAtIndex(int iRow, int iColumn, int * iNumVal);
int IndexerGetAtIndex(int iRow, int iColumn, int * iNumVal);
~DynamicArray2D();
int Dump(char * strMsg=(char*)0);
} * TDynamicArray2D;
#define DYNAMIC_ARRAY_2D_SIZE ( sizeof(DynamicArray2D));
#endif
//cpp file
using namespace std;
#include <iostream>
#ifndef _DYNAMIC_ARRAY
#include "DynamicArray.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
inline DynamicArray::DynamicArray() { data=NULL; size=0; }
DynamicArray::DynamicArray(int size)
{
int N = size*sizeof(int);
this->data = (int*)malloc(N);
this->size = size;
memset(this->data,0,N);
}
DynamicArray::~DynamicArray()
{
if (this->data!=NULL)
{
free(this->data);
}
this->data=NULL;
this->size=0;
}
DynamicArray::DynamicArray(const DynamicArray& dynamicArray)
{
this->size = dynamicArray.size;
int N = this->size*sizeof(int);
this->data = (int*)malloc(N);
memcpy(this->data,dynamicArray.data,N);
}
int DynamicArray::IndexerSetAtIndex( int iIndexPos, int *iNumVal)
{
int iReturn=0;
if (iIndexPos>=0 && iIndexPos<this->size)
{
data[iIndexPos]=*iNumVal;
}
else
{
iReturn=-1;
}
return(iReturn);
}
int DynamicArray::IndexerGetAtIndex(int iIndexPos, int * iNumVal)
{
int iReturn=0;
if (iIndexPos>=0 && iIndexPos<this->size)
{
*iNumVal=data[iIndexPos];
}
else
{
iReturn=-1;
}
return(iReturn);
}
//resident data is erased
int DynamicArray::Allocate(int size)
{
this->size = size;
int N = size*sizeof(int);
if (this->data!=NULL)
{
free(this->data);
}
this->data = (int*)malloc(N);
memset(this->data,0,N);
}
//resident data is preserved. If requested size is LARGER, array is padded with NULL;
// if requested size is SMALLER, the data at the end is LOST
int DynamicArray::RedimPreserve(int size)
{
int N = size * sizeof(int);
this->temp = (int*)malloc(N); //allocates new buffer and intiializes
memset(this->temp,0,N);
memcpy(this->temp,this->data,N); //copy happens here...
this->size = size; //updates the size
free(this->data); //frees the old data
this->data = this->temp; //ownership changes here...
this->temp = NULL;
}
int DynamicArray::Dump(char * strMsg, int direction)
{
if (strMsg!=NULL)
{
cout << "*************************************************" << endl;
cout << strMsg << endl;
cout << "*************************************************" << endl;
}
for (int iLoop=0; iLoop<this->size; iLoop++)
{
cout << data[iLoop];
if (direction==DISPLAY_DIRECTION_VERTICAL)
{
cout << endl;
}
else
{
cout << " ";
}
}
}
DynamicArray2D::DynamicArray2D( int rows, int columns)
{
data = (int**)malloc(rows*sizeof(int*));
this->numRows = rows;
this->numColumns = columns;
for (int rowLoop=0; rowLoop<rows; rowLoop++)
{
int N = columns*sizeof(int);
data[rowLoop] = (int*)malloc(N);
for (int columnLoop=0; columnLoop<columns; columnLoop++)
{
data[rowLoop][columnLoop]=0;
}
}
}
DynamicArray2D:: Dynamicarray2D( const DynamicArray2D & dynamicArray2D)
{
int rows = this->numRows = dynamicArray2D.numRows;
int columns = this->numColumns = dynamicArray2D.numColumns;
data = (int**)malloc(rows*sizeof(int*));
for (int rowLoop=0; rowLoop<rows; rowLoop++)
{
int N = columns*sizeof(int);
data[rowLoop] = (int*)malloc(N);
memset(&data[rowLoop],0,N);
memcpy(&data[rowLoop],dynamicArray2D.data[rowLoop],N);
}
}
int DynamicArray2D::IndexerSetAtIndex(int iRow, int iColumn, int * iNumVal)
{
int iReturn=0;
if (iRow>=0 && iRow<numRows && iColumn>=0 && iColumn<numColumns)
{
data[iRow][iColumn] = *iNumVal;
cout << "just set element at index " << iRow << " :" << iColumn << " to " << *iNumVal << endl;
}
else
{
//one of the index paramters is out of range
iReturn=-1;
}
return(iReturn);
}
int DynamicArray2D::IndexerGetAtIndex(int iRow, int iColumn, int * iNumVal)
{
int iReturn=0;
if (iRow>=0 && iRow<numRows && iColumn>=0 && iColumn<numColumns)
{
*iNumVal = data[iRow][iColumn] ;
}
else
{
//one of the index paramters is out of range
iReturn=-1;
}
return(iReturn);
}
DynamicArray2D::~DynamicArray2D()
{
if (data!=NULL)
{
for (int rowLoop=0; rowLoop<numRows; rowLoop++)
{
if (data[rowLoop]!=NULL)
{
free(data[rowLoop]);
data[rowLoop]=NULL;
}
}
free(data);
data=NULL;
numRows=numColumns=0;
}
}
int DynamicArray2D::Dump(char * strMsg)
{
if (strMsg!=NULL)
{
cout << "*************************************************" << endl;
cout << strMsg << endl;
cout << "*************************************************" << endl;
}
for (int rowLoop=0; rowLoop<numRows; rowLoop++)
{
for (int columnLoop=0; columnLoop<numColumns; columnLoop++)
{
cout << data[rowLoop][columnLoop] << " ";
}
cout << endl;
}
}