0% found this document useful (0 votes)
4 views34 pages

1 Arrays

The document provides an overview of arrays, focusing on 1-dimensional and 2-dimensional arrays, including their declaration, representation, and operations. It details the implementation of safe arrays with bounds checking, operator overloading for accessing elements, and input/output operations. Additionally, it explains the memory layout for 2D arrays and various methods for accessing elements in both row-major and column-major formats.

Uploaded by

brosy812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

1 Arrays

The document provides an overview of arrays, focusing on 1-dimensional and 2-dimensional arrays, including their declaration, representation, and operations. It details the implementation of safe arrays with bounds checking, operator overloading for accessing elements, and input/output operations. Additionally, it explains the memory layout for 2D arrays and various methods for accessing elements in both row-major and column-major formats.

Uploaded by

brosy812
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Arrays

Dr. Rajni Bala


1-Dimensional Array
Ø An Array is a container which can hold a fix number of items
and these items should be of the same type.

Ø Element − Each item stored in an array is


called an element.

Ø Index − Each location of an element in an


array has a numerical index, which is used to
identify the element.
Array Representation
Ø Let’s take a look at the 1-d array having 6
elements.
Elements

1 2 3 4 5 6
0 1 2 3 4 5

Index
Array Declaration
Syntax:-
data_type array_name[array_size];

Type of elements Name of Size of the


stored in an array the array array

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&);
}

int &Array1D ::operator[](int i)


{
if (i < lb || i > ub)
throw "Array Index out of bond";
else
return elearr[i - lb];
}
Operator+ overloading
Array1D Array1D ::operator+(const Array1D &obj)
{
if (this->getSize() != obj.getSize())
throw "Unequal size of array is being added";

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;
}

void Array1D ::reSize(int size)


{
if (getSize()==size) return;
delete[] elearr;
this->size = size;
elearr = new int[size];

}
Operator >>, << overloading
istream& operator >>(istream& in,Array1D& obj){
for (int i = 0; i < obj.getSize(); i++)
in>>obj[i];
return in;
}

ostream& operator <<(ostream& out,Array1D& obj) {


out<<"{";
for (int i = 0; i < obj.getSize(); i++){
out<<" "<<obj[i];
if(i<obj.getSize()-1)
out<<",";
}
out<<"} ";
return out;
}
2-Dimensional Arrays
2 Dimensional Array
• A collection of one-dimensional Array
• An array where each element is one-
dimensional array.
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]
2 Dimensional Array
• 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]
Total memory = 5*4 = 20*sizeof(int)
Row Major/Column Major
00 R 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

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

To access A[i][j] , The numbers of rows to be skipped is i, and number of elements to


be skipped in (i+1)th row is j.

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

To access A[i][j] , the numbers of columns to be skipped is j, and number of elements


to be skipped in (j+1)th column is i.

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(const Array2D& m){


rows = m.rows;
cols = m.cols;
element = new int[rows*cols]
for ( int i=0;i<rows*cols;i++)
element[i] = m.element[i];
}

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;
}

ostream& operator<<(ostream& out, const Array2D& m){

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];
}

Array2D::Array2D(const Array2D& m){


rows = m.rows;
cols = m.cols;
element = new int*[rows]
for ( int i=0;i<rowsi++)
element[i] = new int[cols];

for (int i=0;i<rows;i++)


for(int j=0;j<cols;j++)
element[i][j]=m.element[i][j]
Overloading operator() to access
Array2D::~Array2D() {
for (int i=0;i<rows;i++)
delete[] element[i];
delete[] element;
}

int& Array2D::operator()(int i,int j) {


if (i<0 || i>=rows || j<0 || j>=cols) throw “Out of Bound Exception”

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;
}

ostream& operator<<(ostream& out, const Array2D& m){

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);
}

Array2D::Array2D(const Array2D& m){


rows = m.rows;
cols = m.cols;
element = new Array1D[rows];
for ( int i=0;i<rowsi++)
element[i] = m[i];// element[i].operator=(m[i])
}
2D array as array of array of int
3 4
a
4 int
4
int
4

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]

int x = a[2][3]// a.operator[](2).operator[](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;
}

ostream& operator<<(ostream& out, const Array2D& m){

for(int i=0;i<rows;i++)
out<<m[i]

return out;
}

You might also like