mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
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:
parent
fc180b6061
commit
0febbf0314
@ -7,8 +7,8 @@
|
||||
* @author [Ritika Mukherjee](https://github.com/ritikaa17)
|
||||
*/
|
||||
|
||||
#include <iostream> /// for IO operations
|
||||
#include <cassert> /// for assert
|
||||
#include <iostream> /// for IO operations
|
||||
#include <cassert> /// 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user