summaryrefslogtreecommitdiffstats
path: root/C++/ArraySearchExample/ArraySearchExample.cpp
blob: ce686e971fb4c9c9a38f33e5fb9f6079780d1613 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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/>.
	 */