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 T> class arraylDi public Array ld(int size=0) Array l D(const Array ld<t>& 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 T> class Array1D{ public: Array1D(int size=0); Array1D(const Array1D<T>& 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<class T> ArrayID<T>: 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<class T> Array1D<T>::Array1D(int sz) { if(sz<0) throw BadInitializers(); size=sz; element=new T[sz]; }