From 8eaed1d592198635b69375a570bf9a43488b31de Mon Sep 17 00:00:00 2001 From: Vicky <107568580+HungryVicky@users.noreply.github.com> Date: Mon, 3 Oct 2022 11:06:58 +0530 Subject: [PATCH] Create LinearSearchAlgorithm.cpp --- .../Searching/LinearSearchAlgorithm.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 algorithms/CPlusPlus/Searching/LinearSearchAlgorithm.cpp diff --git a/algorithms/CPlusPlus/Searching/LinearSearchAlgorithm.cpp b/algorithms/CPlusPlus/Searching/LinearSearchAlgorithm.cpp new file mode 100644 index 00000000..8e94d4b0 --- /dev/null +++ b/algorithms/CPlusPlus/Searching/LinearSearchAlgorithm.cpp @@ -0,0 +1,16 @@ +#include +int main(){ + + int arr[5]{1,2,3,4,5}; + + int value = [&]() mutable{ //lambda function & symbol gives assess to outside objects's variable + for(int i = 0 ; i<= sizeof(arr) ; i++){ + if(arr[i] == 5) return i; //if the current element 'i' is equal to the value return the index of it. + } + return -1; // it not return -1 + }(); + + if (value != -1) printf("Value Found Index : %d ",value); + else printf("Value Not Found!"); + +}