Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
linear_search.cpp File Reference

Linear search algorithm More...

#include <iostream>
#include <cassert>
Include dependency graph for linear_search.cpp:

Functions

int LinearSearch (int *array, int size, int key)
 for assert More...
 
static void tests ()
 Self-test implementations. More...
 
int main ()
 Main function. More...
 

Detailed Description

Function Documentation

◆ LinearSearch()

int LinearSearch ( int *  array,
int  size,
int  key 
)

for assert

for IO operations

[Algorithm implementation for linear search]

Parameters
[in]arrayarray to search in
[in]sizelength of array
[in]keykey value to search for
Returns
index where the key-value occurs in the array
-1 if key-value not found
22{
23 for (int i = 0; i < size; ++i)
24 {
25 if (array[i] == key)
26 {
27 return i;
28 }
29 }
30
31 /* We reach here only in case element is not present in array, return an invalid entry in that case*/
32 return -1;
33}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
71{
72 int mode = 0;
73
74 std::cout << "Choose mode\n";
75 std::cout << "Self-test mode (1), interactive mode (2): ";
76
77 std::cin >> mode;
78
79 if (mode == 2)
80 {
81 int size = 0;
82 std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
83 std::cin >> size;
84
85 while (size <= 0 || size > 30){
86 std::cout << "Size can only be 1-30. Please choose another value: ";
87 std::cin >> size;
88 }
89
90 int *array = new int[size];
91 int key = 0;
92
93 // Input for the array elements
94 std::cout << "Enter the array of " << size << " numbers: ";
95 for (int i = 0; i < size; i++)
96 {
97 std::cin >> array[i];
98 }
99
100 std::cout << "\nEnter the number to be searched: ";
101 std::cin >> key;
102
103 int index = LinearSearch(array, size, key);
104 if (index != -1)
105 {
106 std::cout << "Number found at index: " << index << "\n";
107 }
108 else
109 {
110 std::cout << "Array element not found";
111 }
112 delete[] array;
113 }
114 else
115 {
116 tests(); // run self-test implementations
117 }
118 return 0;
119}
static void tests()
Self-test implementations.
Definition: linear_search.cpp:39
int LinearSearch(int *array, int size, int key)
for assert
Definition: linear_search.cpp:21
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
40{
41 int size = 4;
42 int *array = new int[size];
43 for (int i = 0; i < size; i++)
44 {
45 array[i] = i;
46 }
47
48 assert(LinearSearch(array, size, 0) == 0);
49 assert(LinearSearch(array, size, 1) == 1);
50 assert(LinearSearch(array, size, 2) == 2);
51
52 size = 6;
53 for (int i = 0; i < size; i++)
54 {
55 array[i] = i;
56 }
57
58 assert(LinearSearch(array, size, 3) == 3);
59 assert(LinearSearch(array, size, 1) == 1);
60 assert(LinearSearch(array, size, 5) == 5);
61
62 std::cout << "All tests have successfully passed!\n";
63 delete[] array; // free memory up
64}
Here is the call graph for this function: