chore: improve search/linear_search.cpp message (#1998)

* Update linear_search.cpp

Improved code: array size (input) entered by user

* Update search/linear_search.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* fix: apply suggestions from code review

Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Shrutika Kailas Hilale 2022-09-26 20:05:38 +05:30 committed by GitHub
parent fc180b6061
commit 0febbf0314
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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