how to search integer in c 2b 2b

Solutions on MaxInterview for how to search integer in c 2b 2b by the best coders in the world

showing results for - "how to search integer in c 2b 2b"
Delfina
24 May 2020
1//********************************************************
2// search algorithm for arrays
3//********************************************************
4
5#include <iostream>
6
7using namespace std;
8
9void initializeArray(int list[], int listSize);				
10int seqSearch(int list[], int listLength, int searchItem);		
11void printArray(int list[], int listSize);								
12	
13int main()
14{
15	int maximum = 10;
16	int array[maximum];
17	int result;
18	int input;
19	
20	cout << "Enter number to search in array!" << endl;
21	cin >> input;
22	
23	// set all array elements to 1;
24	initializeArray(array, maximum);
25	cout << "before change" << endl;
26	printArray(array, maximum);
27	
28	// change array element No. 7 to 0
29	array[7] = 1;
30	cout << "after change" << endl;
31	printArray(array, maximum);
32	
33	// search user input in array
34	result = seqSearch(array, maximum, input);
35	
36	if (result = -1)
37		cout << "Item not found!" << endl;
38	else
39		cout << "Item found at position: " << result << "!" << endl;
40	
41	return 0;
42}
43
44int seqSearch(int list[], int listLength, int searchItem)
45{
46	int loc;
47	bool found = false;
48	
49	loc = 0;
50	
51	while (loc < listLength && !found)
52		if (list[loc] == searchItem)
53			found = true;
54		else
55			loc++;
56	
57	if (found)
58		return loc;
59	else
60		return -1;
61}
62
63void initializeArray(int list[], int listSize)
64{
65	int index;
66	for (index = 0; index < listSize; index++)
67		list[index] = 0;
68}
69
70void printArray(int list[], int listSize)
71{
72	int index;
73	for (index = 0; index < listSize; index++)
74		cout << list[index] << endl;
75}
76
77
78
79