Chapter 4 arrays and matrix
Chapter 4 Arrays and Matrix
4.1 1D-Array 1. One-dimensional array 1D-array is a limited sequence composed ofn(n20) elements which are of the same data type For example 0123456789 a3527491860|5477|834102 Location of the element Loc(all=loc(aod+
4.1 1D-Array 1. One-dimensional array • 1D-array is a limited sequence composed of n (n0) elements which are of the same data type. • For example: • Location of the element Loc(a[i])=Loc(a[0])+i 35 27 49 18 60 54 77 83 41 02 a 0 1 2 3 4 5 6 7 8 9
4.1 ID-Array Class definition of lD-Array(program 4-1) template class arraylDi public Array ld(int size=0) Array l D(const Array ld& v) Array ldoidelete d element; T& operator l(int const int Sizeoireturn size; some operation
4.1 1D-Array Class definition of 1D-Array(program 4-1) template class Array1D{ public: Array1D(int size=0); Array1D(const Array1D& v); ~Array1D(){delete [] element;} T& operator[](int i)const; int Size(){return size;} //some operation
4.1 1D-Array rivate int size T*element; //Id array
4.1 1D-Array private: int size; T*element; //1D array };
4.1 1D-Array 1)Constructor for ID array Program 4-2 template ArrayID: Arrayld(int sz) f if(sz<o) throw badInitializerso; SIze-SZ element=new t[]
4.1 1D-Array 1)Constructor for 1D array Program 4-2 template Array1D::Array1D(int sz) { if(sz<0) throw BadInitializers(); size=sz; element=new T[sz]; }
4.1 1D-Array Template. Array lD(const Array lD& v) f//copy constructor for ID array Size-v size element-=new T[size]; /get space for(int i=0; i<size; i++)//copy elements elementi=v element[i]
4.1 1D-Array Template Array1D::Array1D(const Array1D& v) {//copy constructor for 1D array size=v.size; element=new T[size]; //get space for(int i=0;i<size;i++) //copy elements element[i]=v.element[i]; }
4.1 1D-Array Overloading the array indexing operator Program 4.3 template T& arrayld:: operator[(int iconst f//return reference to element I if(i=size )throw OutofBoundso return element[1
4.1 1D-Array 2)overloading the array indexing operator Program 4.3 template T& Array1D::operator[](int i)const {//return reference to element I. if(i=size)throw OutOfBounds(); return element[i]; }
4.1 1D-Array 3)Overloading the assignment operator Program 4.4 template Array id& array ld: operator- (const Arrayld& v fif (thisl=&v)//not self-assignment SIze-Vsize delete d element; //free old space element-new T[size]; //get right amout
4.1 1D-Array 3)Overloading the assignment operator Program 4.4 template Array1D& Array1D::operator= (const Array1D& v) {if (this!=&v){//not self-assignment size=v.size; delete [] element; //free old space element=new T[size];//get right amout
4.1 1D-Array for(int 1=0; K<size; 1++)/copy elements elementi=v elementi return *this
4.1 1D-Array for(int i=0;i<size;i++) //copy elements element[i]=v.element[i]; } return *this; }
4.2 2D-Array Two-dimensional arrays are composed of n rows and m columns doo aoi a02… 10a11 2 m Am=|aa21a2…l2 10
4.2 2D-Array Two-dimensional arrays are composed of n rows and m columns. a00 a01 a02……a0 m-1 a10 a11 a12……a1 m-1 a20 a21 a22……a2 m-1 …………. an-10 an-11an-12…..an-1 m-1 A[n][m]=