-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequential.cpp
54 lines (44 loc) · 1.07 KB
/
Sequential.cpp
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
#include <iostream>
using namespace std;
void printArray();
int sequentialSearch(int value);
const int arrSize = 10;
int arr[arrSize];
int main()
{
// Filling the array with random numbers...
for (int i = 0; i < arrSize; i++)
arr[i] = rand() % 100;
cout << "Random Array:\n";
printArray();
cout << "\nEnter A Number To Search For: ";
int x;
cin >> x;
cout << "The Number At Index: " << sequentialSearch(x);
return 0;
}
void printArray()
{
for (int i = 0; i < arrSize; i++)
cout << arr[i] << "\t";
cout << "\n";
}
int sequentialSearch(int value)
{
/**
* This for loop iterates over the entire array form the first to the last element.
*/
for (int i = 0; i < arrSize; i++)
{
/**
* If the i-th element of the array is equal to the value, immediately returns the current index i.
*/
if (arr[i] == value)
return i;
}
/**
* If the previous loop iterates over the entire array and does not find the value, return -1.
* (This is a common convention to indicate that the searched value was not found.)
*/
return -1;
}