Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 One-Dimensional Arrays

Similar presentations


Presentation on theme: "Chapter 9 One-Dimensional Arrays"— Presentation transcript:

1 Chapter 9 One-Dimensional Arrays

2 Chapter 9 An array is an indexed data structure that is used to store data elements of the same data type.

3 Chapter 9 A one-dimensional array, or list, is a sequential list of storage locations that contain data elements that are located via indices.

4 Chapter 9 One-Dimensional Array Definition Format element data type array name[SIZE];

5 Chapter 9 Inserting an Array Element Using Direct Assignment array name [index] = element value;

6 Chapter 9 Inserting Array Elements Using a for Loop for(int index=0;index<array size; ++index) assign or read to array[index]

7 Chapter 9 Copying an Array Element Using Direct Assignment variable = array name[index];

8 Chapter 9 Displaying Array Elements Down the Screen Using a for Loop for(int index=0;index<array size; ++index) cout << array[index] << endl;

9 Chapter 9 C-stings are stored in character arrays where the last element of the string is always the ‘\0’ null terminator character.

10 Chapter 9 C-String Definition with a Specified Size char variable identifier[maximum size of string + 1] = “\0”;

11 Chapter 9 C-String Definition without a Specified Size char variable identifier[ ] = "string value";

12 Chapter 9 Reading C-Strings Using getline() cin.getline(array ident., array size, 'delimiting character');

13 Chapter 9 C-String Functions
strcat() string.h Appends one string to another. strcmp() string.h Compares two strings. strlen() string.h Returns length of a string. strcpy() string.h Copies a string.

14 Sequential Search Set found = false.
Chapter 9 Sequential Search Set found = false. Set index = first array index. While (element is not found) AND (index <= last array index) If (array[index] == element) Then Set found = true. Else Increment index. If (found == true) Return index. Return –1. .

15 Set i = second array index.
Chapter 9 Insertion Sort Set i = second array index. While (i <= last array index) Set j = i. While ( (j > first array index) AND (array[j] < array[j – 1]) ) Exchange array[j] and array[j – 1]. Decrement j. Increment i.

16 Chapter 9 teleSearch() BEGIN
If (the telephone book only contains one page) Look for the name on the page. Else Open the book to the middle. If (the name is in the first half) teleSearch(first half of the book for the name). teleSearch(second half of the book for the name). END.

17 Recursive Binary Search (initial algorithm)
Chapter 9 Recursive Binary Search (initial algorithm) binSearch() If (the array has only one element) Determine if this the element. Else Find the midpoint of the array. If (the element is in the first half) binSearch(first half). binSearch(second half).

18 Recursive Binary Search (final algorithm) If (first > last)
Chapter 9 Recursive Binary Search (final algorithm) If (first > last) Return –1. Else Set mid = (first + last) / 2. If (array[mid] == element) Return mid. If (the element is in the first half) binSearch(array, element, first, mid – 1). binSearch(array, element, mid + 1, last).

19 Chapter 9 Method binSearch():Searches a sorted array of integers for a given value. Accepts: An array of integers, an element for which to search, the first index of the array being searched, and the last index of the array being searched. Returns: The array index of the element, if found, or the value –1 if the element is not found. int binSearch(int[] array, int element, int first, int last)

20 Chapter 9

21 Chapter 9 int binSearch(int array [], int element, int first, int last) { int mid; //ARRAY MIDPOINT if (first > last) //IF ELEMENT NOT IN ARRAY return -1; //RETURN -1, ELSE CONTINUE else mid = (first + last) / 2; //FIND MIDPOINT OF ARRAY if (element == array[mid]) //IF ELEMENT IS IN ARRAY[MID] return mid; //RETURN MID else //ELSE SEARCH APPROPRIATE HALF if (element < array[mid]) return binSearch(array, element, first, mid - 1); return binSearch(array, element, mid + 1, last); } //END OUTER ELSE } //END binSearch()

22 Chapter 9 The C++ Standard Template Library, or STL, contains a class called vector that can be used to build and manipulate one-dimensional arrays.

23 Chapter 9 Defining a Vector Example //INTEGER VECTOR, SIZE 10, //INITIALIZED TO 0s vector<int> v1(10,0);

24 STL Vector Class Functions
Chapter 9 STL Vector Class Functions void begin() Places iterator at beginning of the vector. vector data type back() Returns, but does not remove, the last element of the vector. bool empty() Returns true if vector is empty. void end() Places iterator at end of the vector. vector data type front() Returns, but does not remove, the first element of the vector. void pop_back() Removes, but does not return, the last element of the vector.

25 STL Vector Class Functions
Chapter 9 STL Vector Class Functions void begin() Places iterator at beginning of the vector. vector data type back() Returns, but does not remove, the last element of the vector. bool empty() Returns true if vector is empty.

26 STL Vector Class Functions (continued)
Chapter 9 STL Vector Class Functions (continued) void end() Places iterator at end of the vector. vector data type front() Returns, but does not remove, the first element of the vector. void pop_back() Removes, but does not return, the last element of the vector.

27 STL Vector Class Functions (continued)
Chapter 9 STL Vector Class Functions (continued) void push_back(element) Places element at the end of the vector. int size() Returns the number of elements in the vector.


Download ppt "Chapter 9 One-Dimensional Arrays"

Similar presentations


Ads by Google