Lec 1_Part 2 Array PDF
Document Details
Uploaded by MindBlowingMarsh339
Benha University
Tags
Summary
This is a lecture on data structures and algorithms. It covers arrays, including simple array operations, multidimensional arrays, and dynamic memory allocation, along with C++ code examples.
Full Transcript
Data Structures and Algorithms Part 2 - Array 1 Data Structures and Algorithms Part 2 Outline Simple array operations: Traversal Search Delete Insert in a specific location Multidimensional arrays: 2-D array 3-D array Dyna...
Data Structures and Algorithms Part 2 - Array 1 Data Structures and Algorithms Part 2 Outline Simple array operations: Traversal Search Delete Insert in a specific location Multidimensional arrays: 2-D array 3-D array Dynamic memory allocation: New operator. Delete operator. 2 Data Structures and Algorithms Part 2 Array An array is a number of data items of the same type arranged contiguously in memory. Each element in an array can be accessed by a name and an index. Array is used when the number of required elements is known in advance. The array is the most commonly used data storage structure; it’s built into most programming languages. 3 Data Structures and Algorithms Part 2 Simple array operations: Traversal, Search, Delete, Insert in a specific location 4 Data Structures and Algorithms Part 2 #include #include using namespace std; class LowArray { private: vector v; // vector holds doubles public: //-------------------------------------------------------------- LowArray(int max) //constructor { v.resize(max); } //size the vector //-------------------------------------------------------------- void setElem(int index, double value) //put element into { v[index] = value; } //array, at index //-------------------------------------------------------------- double getElem(int index) //get element from { return v[index]; } //array, at index //-------------------------------------------------------------- }; //end class LowArray 5 Data Structures and Algorithms Part 2 int main() { LowArray arr(100); //create a LowArray int nElems = 0; //number of items int j, position; double value; //-------------------------------------------------------------- arr.setElem(0, 77); //insert 10 items arr.setElem(1, 99); arr.setElem(2, 44); arr.setElem(3, 55); arr.setElem(4, 22); arr.setElem(5, 88); arr.setElem(6, 11); arr.setElem(7, 00); arr.setElem(8, 66); arr.setElem(9, 33); nElems = 10; //now 10 items in array //-------------------------------------------------------------- 6 Data Structures and Algorithms Part 2 for(j=0; j