# Algorithms_in_C++: search/linear_search.cpp File Reference

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

Linear search algorithm

Author
  Unknown author
  Ritika Mukherjee

## Function Documentation

## LinearSearch()

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

for assert

for IO operations

[Algorithm implementation for linear search]

Parameters

  [in] | array | array to search in
  [in] | size | length of array
  [in] | key | key value to search for

Returns
  index where the key-value occurs in the array
  -1 if key-value not found

{
for (int i = 0; i < size; ++i)
{
if (array[i] == key)
{
return i;
}
}
/* We reach here only in case element is not present in array, return an invalid entry in that case*/
return -1;
}

## main()

int main | ( | void |  | ) |

Main function.

Returns
  0 on exit

{
int mode = 0;
std::cout << "Choose mode\n";
std::cout << "Self-test mode (1), interactive mode (2): ";
std::cin >> mode;
if (mode == 2)
{
int size = 0;
std::cout << "\nEnter the size of the array [in range 1-30 ]: ";
std::cin >> size;
while (size <= 0 || size > 30){
std::cout << "Size can only be 1-30. Please choose another value: ";
std::cin >> size;
}
int *array = new int[size];
int key = 0;
// Input for the array elements
std::cout << "Enter the array of " << size << " numbers: ";
for (int i = 0; i < size; i++)
{
std::cin >> array[i];
}
std::cout << "\nEnter the number to be searched: ";
std::cin >> key;
int index = LinearSearch(array, size, key);
if (index != -1)
{
std::cout << "Number found at index: " << index << "\n";
}
else
{
std::cout << "Array element not found";
}
delete[] array;
}
else
{
tests(); // run self-test implementations
}
return 0;
}
std::cin
std::cout
tests
static void tests()
Self-test implementations.
Definition: linear_search.cpp:39
LinearSearch
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

{
int size = 4;
int *array = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = i;
}
assert(LinearSearch(array, size, 0) == 0);
assert(LinearSearch(array, size, 1) == 1);
assert(LinearSearch(array, size, 2) == 2);
size = 6;
for (int i = 0; i < size; i++)
{
array[i] = i;
}
assert(LinearSearch(array, size, 3) == 3);
assert(LinearSearch(array, size, 1) == 1);
assert(LinearSearch(array, size, 5) == 5);
std::cout << "All tests have successfully passed!\n";
delete[] array; // free memory up
}
Here is the call graph for this function: