Download presentation
Presentation is loading. Please wait.
Published byDale Green Modified over 8 years ago
1
String as Arrays, Array Sorting C++ Programming Technologies
2
String as Array The string class has been briefly introduced in an earlier chapter. It is a very powerful class to handle and manipulate strings of characters. However, because strings are, in fact, sequences of characters, we can represent them also as plain arrays of elements of a character type. For example, the following array: char foo [20]; - is an array that can store up to 20 elements of type char.
3
Examples By convention, the end of strings represented in character sequences is signaled by a special character: the null character, whose literal value can be written as '\0' (backslash, zero). In this case, the array of 20 elements of type char called foo can be represented storing the character sequences "Hello"and "Merry Christmas" as:
4
Examples Because arrays of characters are ordinary arrays, they follow the same rules as these. For example, to initialize an array of characters with some predetermined sequence of characters, we can do it just like any other array: Therefore, the array of char elements called myword can be initialized with a null-terminated sequence of characters by either one of these two statements: char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' }; char myword[] = "Hello";
5
Example 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 // strings and NTCS: #include using namespace std; int main () { char question1[] = "What is your name? "; string question2 = "Where do you live? "; char answer1 [80]; string answer2; cout << question1; cin >> answer1; cout << question2; cin >> answer2; cout << "Hello, " << answer1; cout << " from " << answer2 << "!\n"; return 0; } What is your name? Homer Where do you live? Greece Hello, Homer from Greece!
6
Sorting Arrays : Bubble Sort Example: Original: 3 4 2 7 6 Pass 1: 3 2 4 6 7 (elements exchanged) Pass 2: 2 3 4 6 7 Pass 3: 2 3 4 6 7 (no changes needed) Pass 4: 2 3 4 6 7 Pass 5: 2 3 4 6 7 Small elements "bubble" to the top (like 2 in this example)
7
Bubble Sort Bubble sorting is the very commonly and widely used sorting technique in C++ programming. It is also known as the exchange sort. It repeatedly visits the elements of an array and compares the two adjacent elements. It visits the array elements and compare the adjacent elements if they are not in the right order then it puts them into the right order. It continues to swap the elements until no more swaps are required, which means the given array is sorted. It works in the following steps described below:
8
Bubble Sort Algorithm int hold; int array[5]; cout<<"Enter 5 numbers: "<<endl; for(int i=0; i<5; i++) { cin>>array[i]; } for(int i=0; i<4; i++) { for(int j=0; j<4; j++) { if(array[j]>array[j+1]) { hold=array[j]; array[j]=array[j+1]; array[j+1]=hold; } cout<<"Sorted Array is: "<<endl; for(int i=0; i<5; i++) { cout<<array[i]<<endl; }
9
THANKS FOR ATTENTION
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.