1 Arrays
1 Arrays
1 2 3 4 5 6
0 1 2 3 4 5
Index
Array Declaration
Syntax:-
data_type array_name[array_size];
int arr[6 ]; 1 2 3 4 5 6
0 1 2 3 4 5
Safe Arrays
class Array1D
{
int *elearr;
int size;
public:
Array1D(int s=0) //constructor
{
if (s<0) throw “Invalid Size”;
this->size = s;
if (size==0) elearr=null;
else elearr = new int[s];
}
// Functions of the class
int &operator[](int i) { if ( i<0 || i>=size) throw “Invalid Index “; return elearr[i];}
int getSize() const { return size;};
void reSize(int s) { if (s== size) return; delete[] elearr; size=s; elearr = new int[s];}
}
Safe Arrays with lb and ub
class Array1D
{
int *elearr;
int lb, ub;
public:
// Constructor for array with lowerbound and upperbound
Array1D(int lb = 0, int ub = 0)
{
if (lb>ub) throw “invalid bounds”;
this->lb = lb;
this->ub = ub;
elearr = new int[ub - lb + 1];
}
// destructor
~Array1D() { delete[] elearr; }
Safe Arrays with lb and ub
// Functions of the class
int &operator[](int);
Array1D operator+(Array1D &);
Array1D operator-(Array1D &);
Array1D operator*(Array1D &);
Array1D operator/(Array1D &);
int getSize() const;
void reSize(int);
friend istream& operator >>(istream&,Array1D&);
friend ostream& operator <<(istream&,Array1D&);
}
Array1D temp(0,obj.getSize()-1);
for (int i = 0; i < this->getSize(); i++)
{
temp[i] = (*this)[i] + obj[i];
}
return temp;
}
getSize, reSize functions
int Array1D ::getSize() const
{
return ub-lb+1;
}
}
Operator >>, << overloading
istream& operator >>(istream& in,Array1D& obj){
for (int i = 0; i < obj.getSize(); i++)
in>>obj[i];
return in;
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
00 10 20 30 40 01 11 21 31 41 02 12 22 32 42 03 13 23 33 43
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Row Major
• int A[5][4] A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
A[3][0] A[3][1] A[3][2] A[3][3]
A[4][0] A[4][1] A[4][2] A[4][3]
00 01 02 03 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In general for an array of integers A[R][C] , the formula to access i,j element is given
by
k = i*C+j
Column Major
• int A[5][4] A[0][0] A[0][1] A[0][2] A[0][3]
A[1][0] A[1][1] A[1][2] A[1][3]
A[2][0] A[2][1] A[2][2] A[2][3]
A[3][0] A[3][1] A[3][2] A[3][3]
A[4][0] A[4][1] A[4][2] A[4][3]
00 10 20 30 40 01 11 21 31 41 02 12 22 32 42 03 13 23 33 43
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In general for an array of integers A[R][C] , the formula to access i,j element is given by
k = j*R + i
Implementation
class Array2D {
int rows,cols;
int* element;
public :
Array2D(int r=0,int c=0);
Array2D(const Array2D& m);
~Array2D() {
delete [] element;}
int Rows() const {
return rows;}
int Cols() const {
return cols;}
int& operator()(int i,int j) const;
friend istream& operator>>(istream& in,Array2D&);
friend ostream& operator<<(ostream&,const Array2D&);
};
Constructor & Destructor
Array2D::Array2D(int r=0,int c=0){
if (r<= || c<=0) throw “Bad Initializers”;
rows=r;
cols=c;
element = new int[r*c]
}
Array2D::~Array2D() {
delete [] element;
}
Copy Constructor
• Rational r1(r2);//copy constructor
• Void add(Rational r)// copy constructor
• Rational add(Rational r)
• { Rational temp;
• ….....
return temp;
}
Copy Constructor
Array1D a(10);
a(int size=10,int *element=200)
For (int i=0;i<10;i++) a[i]=2*i;
Array1D b(a);
B(size=10;element=900)
B[5]=100
A[5]
Overloading operator() to access
int& Array2D::operator()(int i,int j) {
if (i<0 || i>=rows || j<0 || j>=cols) throw “Out of Bound Exception”
// Row major
k = i*cols + j
// Column major
k = j* rows + i
return(element[k]);
}
Overloading input and output
operators
istream& operator>>(istream& in, Array2D& m) {
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
in>>m(i,j)
return in;
}
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
out<<m(i,j)
return out;
}
2D array as array of array of int
Int **
a
int *200 int
Int *400
400
Int *
int *
int
int
int a[4][4]
Implementation
class Array2D {
int rows,cols;
int** element;
public :
Array2D(int r=0,int c=0);
Array2D(const Array2D& m);
~Array2D() {
delete [] element;}
int Rows() const {
return rows;}
int Cols() const {
return cols;}
int& operator()(int i,int j) const;
friend istream& operator>>(istream& in,Array2D&);
friend ostream& operator<<(ostream&,const Array2D&);
};
2D array as array of array of int
rows cols Int **
a
int * int
Int *
int
Int *
int *
int
int
Array2D a(4,4)
Array1D element[rows];
Constructor & Destructor
Array2D::Array2D(int r=0,int c=0){
if (r<= || c<=0) throw “Bad Initializers”;
rows=r;
cols=c;
element = new int*[r];
for(int i=0;i<r;i++)
element[i] = new int[c];
}
return(element[i][j]);// *(*(element+i)+j)
}
Overloading input and output
operators
istream& operator>>(istream& in, Array2D& m) {
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
in>>m(i,j)
return in;
}
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
out<<m(i,j)
return out;
}
Array2D as an array of Array1D
class Array2D {
int rows, cols;
Array1D *element;
public :
Array2D(int r=0,int c=0);
Array2D(const Array2D& m);
~Array2D() {
delete [] element;}
int Rows() const {
return rows;}
int Cols() const {
return cols;}
Array1D& operator[](int i) const;
friend istream& operator>>(istream& in,Array2D&);
friend ostream& operator<<(ostream&,const Array2D&);
}
2D array as array of array of int
rows cols element
a
size int * int
size Int *
int
size Int *
int
Array2d a(3,4);
Array1D element[rows];
2D array as array of array of int
3 4 element
a
0 null
0 null
0 null
Array2d a(3,4)
Constructor & Destructor
Array2D::Array2D(int r=0,int c=0){
if (r<= || c<=0) throw “Bad Initializers”;
rows=r;
cols=c;
element = new Array1D[rows];
for(int i=0;i<r;i++)
element[i].Resize(cols);
}
int
Array2d a(3,4)
Overloading operator() to access
Array1D& Array2D::operator[](int i) {
if (i<0 || i>=rows) throw “Out of Bound Exception”;
return (element[i])
}
Array2D a(3,5);
Array1D a1=a[2];// a.operator[](2) ()
int x = a1[3]
((a[i])[j]) = a.operator[](i).operator[](j)
Overloading input and output
operators
istream& operator>>(istream& in, Array2D& m) {
for(int i=0;i<rows;i++)
in>>m[i]
return in;
}
for(int i=0;i<rows;i++)
out<<m[i]
return out;
}