Last active
July 22, 2019 08:59
-
-
Save mgarg1/a0d3488150a5d74d8da5 to your computer and use it in GitHub Desktop.
Double Dimension Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # define NDEBUG | |
# include <assert.h> | |
class array2D{ | |
int rows,cols.**arr; | |
public: | |
// array2D():array2D(0,0,0){} | |
array2D(int r,int c,int val):rows(r),cols(c),arr(nullptr){ | |
arr = new int *[rows]; | |
for (int i = 0; i < rows; ++i) | |
arr[i] = new int[cols]; | |
// memset(arr,0,rows*cols); | |
for(int i = 0; i < rows; ++i){ | |
for(int j = 0; j < cols; ++j){ | |
arr[i][j] = val; | |
} | |
} | |
} | |
int operator()(const int i,const int j){ | |
assert(i < rows && i >= 0 && j < cols && j >= 0); | |
return (arr[i][j]); | |
} | |
~array2D(){ | |
for(int i=0;i<rows;i++){ | |
delete [] arr[i]; | |
} | |
delete [] arr; | |
} | |
}; | |
int main(){ | |
{ | |
array2D arr1(100,100,23); | |
int a = arr1(99,99); | |
assert(a==23); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment