diff options
Diffstat (limited to 'C++/ArraySearchExample/ArraySearchExample.cpp')
-rw-r--r-- | C++/ArraySearchExample/ArraySearchExample.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/C++/ArraySearchExample/ArraySearchExample.cpp b/C++/ArraySearchExample/ArraySearchExample.cpp new file mode 100644 index 0000000..ce686e9 --- /dev/null +++ b/C++/ArraySearchExample/ArraySearchExample.cpp @@ -0,0 +1,66 @@ +// Name: msglm +// Date: +// Program Name: +// Description: + + +#include <iostream> +#include <string> +#include <iomanip> +using namespace std; + +// Named constants + +const int ARRAY_SIZE = 7; + +int main() { + //Variable Declaration + int list[ARRAY_SIZE] = {45, 12, 27, 12, 45, 12, 38}; + int num; + bool isFound = false; + int count_num = 0; + + //Program Title + cout << "Searching an Array" << endl << endl; + + // Ask the user for input num + cout << "What integer would you like to search for?\n"; + cin >> num; + + for ( int i = 0; i < ARRAY_SIZE; i++ ) { + if (list[i] == num) { + isFound = true; + break; + } + } + + if (isFound) { + cout << "The number " << num << " is in the list " << endl; + } else { + cout << "The number " << num << " is not in the list" << endl; + } + + //Print number of time s a number is found in the list + for ( int i = 0; i < ARRAY_SIZE; i++ ) { + if (list[i] == num) { + count_num++; + } + } + cout << "The number " << num << " is in the list " << count_num << " times"; + + //Print the location where the number is found in the list + cout << "\nThe number is in locations: " << endl; + + for (int i = 0; i < ARRAY_SIZE; i++ ) { + if (list[i] == num) { + cout << "list[" << i << "] " << endl; + } + } +} + + /*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + |