Presentation is loading. Please wait.

Presentation is loading. Please wait.

12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays.

Similar presentations


Presentation on theme: "12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays."— Presentation transcript:

1

2 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays

3 One Dimensional Arrays 4 Arrays 4 Sorting Algorithms 4 Searching Algorithms 4 Character Strings  The string Class. 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 2

4 ARRAYS Definition and Initialization Computation and Output Function Arguments 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 3

5 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 4 Definition 4 An array is a data structure for storing a contiguous block of data. 4 All data elements in an array must be of the same type. 4 Individual elements of the array are specified using the array name and an offset. 4 In C++ the offset of the first element is always 0.

6 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 5 Definition and Initialization 4 Syntax: data_type identifier[size] [= initialization list]; Note: size is an integer constant. 4 Example: double m[8]; m[0] m[1] m[2] m[3] m[4] m[5] m[6] m[7] ????????

7 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 6 Initializing Arrays 4 Initializing array elements (initialization=>declaration) char vowels[5] = {'a', 'e', 'i', 'o', 'u'}; bool ansKey[] ={true, true, false, true, false, false}; char word[] = "Hello"; vowels 'a''e''i''o''u' truefalse truefalsetrue ansKey word 'H''e''l' 'o''\0'

8 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 7 Accessing Array Elements 4 Offsets are used to access individual elements of an array. 4 General format: array_identifier[offset] 4 Example for (int i=0; i<=7; ++i) m[i] = double(i) + 0.5; 4 Integer expressions may be used as offsets.

9 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 8 Functions and arrays 4 An array identifier, without subscripts, references the starting address(first element) of the array. 4 In C++, arrays are passed by reference. ie the starting address is passed, no size information. 4 Arrays in C++ to not know their size. 4 Generally we specify an additional parameter representing the number of elements in the array.

10 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 9 Example #include using namespace std; const int MAXSIZE=20; void ReadArr(double a[], int& count, istream& in); int FindMin(const double a[], int count); int main( ) { double darr[MAXSIZE]; int cnt=0, position=0; ReadArr(darr, cnt, cin); position = FindMin(darr, cnt); cout << "The smallest value in the array is " << darr[position] << endl; }

11 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 10 // This function inputs values into an array until EOF or array // limit reached void ReadArray(double a[], int& count, istream& in) { double temp; count = 0; in >> temp; while ( (count < MAXSIZE) && !in.eof() ) { a[count] = temp; ++count; in >> temp; }

12 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 11 // This function returns the offset of the smallest //value in an array int FindMin(const double a[], int size) { int offsetOfMin = 0; for (int i=1; i<size; ++i) { if (a[i] < a[offsetOfMin] ) { offsetOfMin = i; }//end if }//end for return offsetOfMin; }//end FindMin

13 SORTING ALGORITHMS selection sort 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 12

14 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 13 Sorting Algorithms 4 Sorting algorithms arrange the data into either ascending or descending order, based on the values in the array. 4 Sorting algorithms to be discussed –Selection sort

15 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 14 Selection Sort Algorithm 4 Find minimum value, place it in the first position. 4 Find next minimum value, place it in the second position. 4 Continue doing this until you have placed the second to the largest value in the second to the last position.

16 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 15 Practice! 4 Fill in the following table to show how the array is sorted into ascending order using the selection sort. arr[0] arr[1] arr[2] arr[3] arr[4] 2945185136 1845295136 swap min and arr[0] 18 29 45 51 36 18 29 36 51 45 18 29 3645 51

17 SEARCH ALGORITHMS unordered lists ordered lists 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 16

18 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 17 Searching Unordered Lists 4 Simple Sequential Search –Examine each element starting with the first one until: a match is found. end of the list is reached. 4 Sequential search can be implemented as: –a function which returns true if item in the list, false if item is not in the list. –a function which returns the location of the item if found, or –1 if not found.

19 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 18 Searching Ordered Lists 4 Modified Sequential Search: –examine every element in the list until: item is found. list element is larger/smaller than the item you are searching for (search fails). 4 Binary Search –Examine middle element: if element equals item, item found, return location. if element is larger than item ignore bottom of list. if element is smaller than item ignore top of list. –Repeat until: item is found. top and bottom cross over (search failed).

20 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 19 Example of Binary Search for 48 7059564337282214115 arr[top]arr[bot] arr[mid] arr[bot ] arr[top] 4337 arr[top]arr[bot] mid is 5 43 arr[6] arr[mid] mid is 6 mid is 7 7059564337 43 arr[top]arr[bot]

21 CHARACTER STRINGS C style strings functions defined in cstring 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 20

22 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 21 C Style Character Strings 4 A C style strings is defined as a sequence of characters, terminated by the null character. 4 When declaring a character array to store a C style string, memory must be allocated for the null character ('\0'). 4 Literal string constants are enclosed within double quote marks: "a string".

23 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 22 C Style String Input  Recall that the input operator ( >>) skips whitespace. 4 To input strings with embedded whitespace, the getline() function can be used as illustrated: char phrase[SIZE]; cin.getline(phrase, SIZE);  The getline () function reads up to SIZE-1 characters from the input stream and will insert the null character.  getline () is a member function of what class?

24 C STYLE STRING FUNCTIONS 4 The Standard C++ library contains a set of predefined functions that operate on C style strings.  These functions are defined in the header file: cstring 4 Commonly used string functions: –strlen() –strcpy() –strcat() –strcmp() 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 23

25 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 24 Example: C Style Strings #include #include //strcmp(), strcpy(), strcat() uses namespace std; int main(){ char str1[30] = "John", str2[30] = "Johnson"; char phrase[20] = "'s shirt was green", sentence[30]; if (strcmp(str1,str2) < 0) strcpy (sentence, str1);// puts "John" into sentence else strcpy (sentence,str2); // puts "Johnson into sentence strcat(sentence, phrase); cout << "Sentence is: " << sentence << endl; return 0; }

26 THE STRING CLASS functions defined in string 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 25

27 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 26 The string class  The string class implements the concept of a character string.  A string object can increase and decrease its size dynamically.  Numerous operators and functions are defined in the string class.

28 Common Functions Defined in string –size( ) –empty( ) –substr (int start, int len) –c_str() 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 27

29 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 28 Overloaded Operators Defined in string 4 relational operators – == = 4 concatenation –+ += 4 assignment –=–=

30 12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 29 Example: string class #include uses namespace std; int main(){ string str1 = "John", str2 = "Johnson"; string phrase = "'s shirt was green", sentence; if (str1 < str2) sentence = str1;// assign "John" to sentence else sentence = str2; // assign "Johnson to sentence sentence += phrase; // append phrase to sentence cout << "Sentence is: " << sentence << endl; return 0; }


Download ppt "12/15/2015Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter Chapter 6 One-Dimensional Arrays."

Similar presentations


Ads by Google