diff --git a/search/linear_search.cpp b/search/linear_search.cpp index e9f24bc35..b69cd4243 100644 --- a/search/linear_search.cpp +++ b/search/linear_search.cpp @@ -7,8 +7,8 @@ * @author [Ritika Mukherjee](https://github.com/ritikaa17) */ -#include /// for IO operations -#include /// for assert +#include /// for IO operations +#include /// for assert /** * \brief [Algorithm implementation for linear search] @@ -18,11 +18,12 @@ * \returns index where the key-value occurs in the array * \returns -1 if key-value not found */ -int LinearSearch(int *array, int size, int key) +int LinearSearch(int *array, int size, int key) { - for (int i = 0; i < size; ++i) + for (int i = 0; i < size; ++i) { - if (array[i] == key) { + if (array[i] == key) + { return i; } } @@ -35,10 +36,12 @@ int LinearSearch(int *array, int size, int key) * @brief Self-test implementations * @returns void */ -static void tests() { +static void tests() +{ int size = 4; int *array = new int[size]; - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { array[i] = i; } @@ -47,7 +50,8 @@ static void tests() { assert(LinearSearch(array, size, 2) == 2); size = 6; - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { array[i] = i; } @@ -63,7 +67,8 @@ static void tests() { * @brief Main function * @returns 0 on exit */ -int main() { +int main() +{ int mode = 0; std::cout << "Choose mode\n"; @@ -71,13 +76,14 @@ int main() { std::cin >> mode; - if (mode == 2) { + if (mode == 2) + { int size = 0; - std::cout << "\nEnter the size of the array: "; + std::cout << "\nEnter the size of the array [in range 1-30 ]: "; std::cin >> size; - while ((size <= 1) || (size >= 30)) { - std::cout << "Size cannot be less than zero. Please choose another value: "; + while (size <= 0 || size > 30){ + std::cout << "Size can only be 1-30. Please choose another value: "; std::cin >> size; } @@ -86,7 +92,8 @@ int main() { // Input for the array elements std::cout << "Enter the array of " << size << " numbers: "; - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { std::cin >> array[i]; } @@ -97,15 +104,16 @@ int main() { 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 + else + { + tests(); // run self-test implementations } return 0; }