From 61c27171d4a149b58dd6c63e1a9f1c0f1617115a Mon Sep 17 00:00:00 2001 From: Naveen Hegde Date: Tue, 26 Dec 2017 16:36:19 +0530 Subject: [PATCH] Fixed Linear Search Linear Search was returning key instead of index. Fixed it. Changed array from size 10 to variable sized. Same changes like the linear search in different directory. --- Linear Search.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/Linear Search.cpp b/Linear Search.cpp index 105ac7ee7..fd381756d 100644 --- a/Linear Search.cpp +++ b/Linear Search.cpp @@ -1,13 +1,13 @@ #include using namespace std; -int LinearSearch(int *array, int key) +int LinearSearch(int *array, int size, int key) { - for (int i = 0; i < 10; ++i) + for (int i = 0; i < size; ++i) { if (array[i]==key) { - return key; + return i; } } @@ -17,19 +17,24 @@ int LinearSearch(int *array, int key) int main() { - int array[10]; + int size; + cout<<"\nEnter the size of the Array : "; + cin >> size; + + int array[size]; int key; //Input array - cout<<"\nEnter the Array of 10 numbers : "; - for (int i = 0; i < 10; i++) + cout<<"\nEnter the Array of " << size << " numbers : "; + for (int i = 0; i < size; i++) { cin>>array[i]; } + cout<<"\nEnter the number to be searched : "; cin>>key; - int index=LinearSearch(array, key); + int index=LinearSearch(array, size, key); if (index!=-1) { cout<<"\nNumber found at index : "<