-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path50_array_search.cpp
More file actions
60 lines (55 loc) · 1.9 KB
/
50_array_search.cpp
File metadata and controls
60 lines (55 loc) · 1.9 KB
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
/*Goal: practice searching an array in C++
**There is an array of integers. The length of the arrays to be searched
**varies. A user will enter a postive integer and the program will search the array.
**If the value is in the array, the program will return the location of
**the element. If the value is not in the array, the user will be notified
**that the value is not in the array. To exit the program the user will enter -1.
*/
#include <iostream>
#include <stdio.h>
int
main ()
{
int searchKey = 0;
int searchArray[10] =
{ 324, 4567, 6789, 5421345, 7, 65, 8965, 12, 342, 485 };
int location = 0;
while (1)
{
std::cout << "Enter an integer ('-1' to quit): ";
scanf ("%d", &searchKey);
std::cout << searchKey << "\n";
if (searchKey == -1)
{
break;
}
for (int i = 0; i < 10; i++)
{
if (searchKey == searchArray[i])
{
location = i;
break;
}
location = -1;
}
if (location != -1)
{
std::
cout << searchKey << " is at location " << location <<
" in the array.\n";
}
else
{
std::cout << searchKey << " is not in the array.\n";
}
}
return 0;
}
/*
Enter an integer ('-1' to quit): 56
56
56 is not in the array.
Enter an integer ('-1' to quit): 65
65
65 is at location 5 in the array.
*/