From 80e34d757a263979d813b83a87b8bc097d6287b7 Mon Sep 17 00:00:00 2001 From: Shashikumarezhilarasu Date: Mon, 16 Dec 2024 00:52:22 +0530 Subject: [PATCH 1/2] solved the linear search problem using c --- Solved-Problems/linear_search/linearsearch.c | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 Solved-Problems/linear_search/linearsearch.c diff --git a/Solved-Problems/linear_search/linearsearch.c b/Solved-Problems/linear_search/linearsearch.c new file mode 100644 index 0000000..e763db8 --- /dev/null +++ b/Solved-Problems/linear_search/linearsearch.c @@ -0,0 +1,29 @@ +#include + +int linearSearch(int arr[], int size, int key) { + for (int i = 0; i < size; i++) { + if (arr[i] == key) { + return i; // Return the index if the key is found + } + } + return -1; // Return -1 if the key is not found +} + +int main() { + int arr[] = {10, 20, 30, 40, 50}; + int size = sizeof(arr) / sizeof(arr[0]); + int key; + + printf("Enter the number to search: "); + scanf("%d", &key); + + int result = linearSearch(arr, size, key); + + if (result != -1) { + printf("Element found at index %d\n", result); + } else { + printf("Element not found in the array\n"); + } + + return 0; +} From b4477e9b23b20f6a338a19d880bf74f866079dc1 Mon Sep 17 00:00:00 2001 From: Shashikumar E Date: Mon, 16 Dec 2024 01:22:30 +0530 Subject: [PATCH 2/2] Delete Solved-Problems/linear_search/linearsearch.c --- Solved-Problems/linear_search/linearsearch.c | 29 -------------------- 1 file changed, 29 deletions(-) delete mode 100644 Solved-Problems/linear_search/linearsearch.c diff --git a/Solved-Problems/linear_search/linearsearch.c b/Solved-Problems/linear_search/linearsearch.c deleted file mode 100644 index e763db8..0000000 --- a/Solved-Problems/linear_search/linearsearch.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -int linearSearch(int arr[], int size, int key) { - for (int i = 0; i < size; i++) { - if (arr[i] == key) { - return i; // Return the index if the key is found - } - } - return -1; // Return -1 if the key is not found -} - -int main() { - int arr[] = {10, 20, 30, 40, 50}; - int size = sizeof(arr) / sizeof(arr[0]); - int key; - - printf("Enter the number to search: "); - scanf("%d", &key); - - int result = linearSearch(arr, size, key); - - if (result != -1) { - printf("Element found at index %d\n", result); - } else { - printf("Element not found in the array\n"); - } - - return 0; -}