From 9d53d8857eaa1c9405894a88ca75bc4657e42f35 Mon Sep 17 00:00:00 2001 From: msglm Date: Sat, 14 Jan 2023 05:31:48 -0600 Subject: Inital Commit --- C++/ArraySearchExample/ArraySearchExample.cpp | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 C++/ArraySearchExample/ArraySearchExample.cpp (limited to 'C++/ArraySearchExample/ArraySearchExample.cpp') 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 +#include +#include +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 . + */ + -- cgit v1.2.3