// arr2.cpp By: Aiman Hanna - ©1993-2006 Aiman Hanna // This program illustrates one of the simple operations that can be done on arrays. // That is, searching an array to find whether it includes some value or not. // Key Points: 1) It is always better to declare a "const int" for the size of // your array. // Notes: Is the 3rd search in the "ArrSearch" function good? // Will this search work fine in all cases? You MUST avoid writing such code. #include void ArrSearch() { // This function creates an array, initializes it, and searches the array for // some value. const int ArrSize = 6; char arr[ ArrSize ]; // "arr" is an array of 6 characters. // Initialize the array. Notice that the array starts always with a "0" index // and it ends with the number of indices - 1, which is 5 in our example. arr[0] = 'a'; arr[1] = 'v'; arr[2] = 'k'; arr[3] = 'p'; arr[4] = 'k'; arr[5] = 's'; // Show out all the values in the array cout << "\n Here are the array values after initialization" << endl; cout << " ===============================================" << endl; for (int i = 0; i < ArrSize; i++) cout << arr[i] << "\t"; char ch; int found = 0; cout << "\n Search # 1. \n ===========" << endl; cout << " Enter the character that you want search for. " << endl; cout << " The search will find all occurrences in the array: " ; cin >> ch; for ( i = 0; i < ArrSize; i++) { if ( arr[i] == ch ) { cout << " The entered character was found in array." << endl; cout << " It was found in element #: " << i + 1 << endl; found = 1; // flag to indicate that the search was successful } } if ( found == 0 ) // the character was not in the array. cout << " The entered character was not found in the array. " << endl; found = 0; // Reset to zero for next search i = 0; // Reset to zero for next search cout << "\n Search # 2. \n ===========" << endl; cout << " Enter another character. " << endl; cout << " This search will find the first occurrence in the array: " ; cin >> ch; while( i < ArrSize ) { if ( arr[i] == ch ) { cout << " The entered character was found first in element #: " << i + 1 << endl; found = 1; break; } i++; } if ( found == 0 ) // the character was not in the array. cout << " The entered character was not found in the array. " << endl; // Notice that the following search is EXTREMELY DANGEROUS. You should // totally avoid searching in such way. Why? i = 0; // Reset to zero for next search cout << "\n Search # 3. \n ===========" << endl; cout << " Enter one more character. " << endl; cout << " This search will find the first character that is bigger than your entry: "; cin >> ch; while( arr[i] <= ch ) { i++; // just move forward as long as the array characters are smaller } cout << " The first element that includes a bigger character than your entry is element : " << i + 1 << " and the value inside this element is: " << arr[i] << endl; i = 0; // Reset to zero for next search cout << "\n Search # 4. Avoids potential problems that search 3 may produce." << endl; cout << " ================================================================" << endl; cout << " Enter one more character. " << endl; cout << " This search will find the first character that is bigger than your entry: "; cin >> ch; while( (i < ArrSize) && (arr[i] <= ch) ) { i++; // just move forward as long as the array characters are smaller // and still in the valid array range } if (i < ArrSize) // A bigger character was found in the array { cout << " The first element that includes a bigger character than your entry is element :" << i + 1 << " and the value inside this element is: " << arr[i] << endl; } else // if i is >= ArrSize, then the array is exhausted before // finding a bigger character cout << " The array does not have any bigger character than your entry. " << endl; } int main() { ArrSearch(); return 0; } // The output of the program /* Here are the array values after initialization =============================================== a v k p k s Search # 1. =========== Enter the character that you want search for. The search will find all occurrences in the array: p The entered character was found in array. It was found in element #: 4 Search # 2. =========== Enter another character. This search will find the first occurrence in the array: k The entered character was found first in element #: 3 Search # 3. =========== Enter one more character. This search will find the first character that is bigger than your entry: z The first element that includes a bigger character than your entry is element : 149 and the value inside this element is: { Search # 4. Avoids potential problems that search 3 may produce. ================================================================ Enter one more character. This search will find the first character that is bigger than your entry: z The array does not have any bigger character than your entry. */ // Run Again /* Here are the array values after initialization =============================================== a v k p k s Search # 1. =========== Enter the character that you want search for. The search will find all occurrences in the array: k The entered character was found in array. It was found in element #: 3 The entered character was found in array. It was found in element #: 5 Search # 2. =========== Enter another character. This search will find the first occurrence in the array: v The entered character was found first in element #: 2 Search # 3. =========== Enter one more character. This search will find the first character that is bigger than your entry: y The first element that includes a bigger character than your entry is element : 9 and the value inside this element is: z Search # 4. Avoids potential problems that search 3 may produce. ================================================================ Enter one more character. This search will find the first character that is bigger than your entry: m The first element that includes a bigger character than your entry is element : 2 and the value inside this element is: v */